1f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes/*
2f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * Copyright (C) 2015 The Android Open Source Project
3f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes *
4f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * you may not use this file except in compliance with the License.
6f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * You may obtain a copy of the License at
7f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes *
8f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes *
10f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * See the License for the specific language governing permissions and
14f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes * limitations under the License.
15f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes */
16f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes
17f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#include <math.h>
18f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes
19f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#include "fpmath.h"
20f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes
21f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughesdouble fabs(double x) {
22f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#if __arm__
23f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  // Both Clang and GCC insist on moving r0/r1 into a double register
24f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  // and using fabs where bit-twiddling would be a better choice.
25f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  // They get fabsf right, but we need to be careful in fabsl too.
26f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  IEEEd2bits u;
27f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  u.d = x;
28f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  u.bits.sign = 0;
29f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  return u.d;
30f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#else
31f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  return __builtin_fabs(x);
32f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#endif
33f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes}
34f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes
35f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughesfloat fabsf(float x) {
36f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  return __builtin_fabsf(x);
37f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes}
38f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes
39f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#if defined(__LP64__)
40f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hugheslong double fabsl(long double x) { return __builtin_fabsl(x); }
41f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#else
42f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hugheslong double fabsl(long double x) {
43f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  // Don't use __builtin_fabs here because of ARM. (See fabs above.)
44f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes  return fabs(x);
45f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes}
46f9f4a432ee4f56b8cb24b2033d3b1068200a6d30Elliott Hughes#endif
47