blob: 12b408ff4c00789481f64a381267a713cb77d9d0 [file] [log] [blame]
Elliott Hughesf9f4a432015-08-24 22:57:08 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <math.h>
18
19#include "fpmath.h"
20
Jake Weinsteinefa59d82019-05-10 01:15:08 -050021#include <fenv.h>
22
Elliott Hughesf9f4a432015-08-24 22:57:08 +000023double fabs(double x) {
24#if __arm__
25 // Both Clang and GCC insist on moving r0/r1 into a double register
26 // and using fabs where bit-twiddling would be a better choice.
27 // They get fabsf right, but we need to be careful in fabsl too.
28 IEEEd2bits u;
29 u.d = x;
30 u.bits.sign = 0;
31 return u.d;
32#else
33 return __builtin_fabs(x);
34#endif
35}
36
37float fabsf(float x) {
38 return __builtin_fabsf(x);
39}
40
41#if defined(__LP64__)
42long double fabsl(long double x) { return __builtin_fabsl(x); }
43#else
44long double fabsl(long double x) {
45 // Don't use __builtin_fabs here because of ARM. (See fabs above.)
46 return fabs(x);
47}
48#endif
Elliott Hughes9a1bb702017-11-09 22:40:11 +000049
Jake Weinsteind6b191d2017-12-11 03:27:49 -050050// fma has builtin routines for ARMv7-A NEON, ARMv8, and ARM64
51
52#if defined (__ARM_NEON__) || defined (__aarch64__)
53float fmaf(float x, float y, float z) { return __builtin_fmaf(x, y, z); }
54double fma(double x, double y, double z) { return __builtin_fma(x, y, z); }
55#endif
56
57// ceil/floor/fmax/fmin/nearbyint/rint/round/trunc have builtin routines for ARMv8 and ARM64
58
59#if defined (__AARCH32__) || defined (__aarch64__)
Jake Weinstein1e108e32017-12-11 02:50:04 -050060float ceilf(float x) { return __builtin_ceilf(x); }
61double ceil(double x) { return __builtin_ceil(x); }
62
63float floorf(float x) { return __builtin_floorf(x); }
64double floor(double x) { return __builtin_floor(x); }
65
Elliott Hughes9a1bb702017-11-09 22:40:11 +000066float fmaxf(float x, float y) { return __builtin_fmaxf(x, y); }
67double fmax(double x, double y) { return __builtin_fmax(x, y); }
68
69float fminf(float x, float y) { return __builtin_fminf(x, y); }
70double fmin(double x, double y) { return __builtin_fmin(x, y); }
71
Jake Weinsteinefa59d82019-05-10 01:15:08 -050072float nearbyintf(float x) { return __builtin_nearbyintf(x); }
73double nearbyint(double x) { return __builtin_nearbyint(x); }
74
Jake Weinsteind6b191d2017-12-11 03:27:49 -050075float rintf(float x) { return __builtin_rintf(x); }
76double rint(double x) { return __builtin_rint(x); }
77
78float roundf(float x) { return __builtin_roundf(x); }
79double round(double x) { return __builtin_round(x); }
80
81float truncf(float x) { return __builtin_truncf(x); }
82double trunc(double x) { return __builtin_trunc(x); }
83#endif
84
85#if defined (__aarch64__)
Jake Weinsteinefa59d82019-05-10 01:15:08 -050086// msun s_nearbyint.c defines all floating-point version, so we need to
87// redefine the long double one here. For aarch64, clang/compiler-rt
88// soft-float routines does not use single/double floating-point operation,
89// so it should be safe to call rintl directly.
90long double nearbyintl(long double x) {
91 volatile long double ret;
92 fenv_t env;
93 fegetenv(&env);
94 ret = rintl(x);
95 fesetenv(&env);
96 return (ret);
97}
Elliott Hughes9a1bb702017-11-09 22:40:11 +000098#endif