Elliott Hughes | f9f4a43 | 2015-08-24 22:57:08 +0000 | [diff] [blame] | 1 | /* |
| 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 Weinstein | efa59d8 | 2019-05-10 01:15:08 -0500 | [diff] [blame] | 21 | #include <fenv.h> |
| 22 | |
Elliott Hughes | f9f4a43 | 2015-08-24 22:57:08 +0000 | [diff] [blame] | 23 | double 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 | |
| 37 | float fabsf(float x) { |
| 38 | return __builtin_fabsf(x); |
| 39 | } |
| 40 | |
| 41 | #if defined(__LP64__) |
| 42 | long double fabsl(long double x) { return __builtin_fabsl(x); } |
| 43 | #else |
| 44 | long 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 Hughes | 9a1bb70 | 2017-11-09 22:40:11 +0000 | [diff] [blame] | 49 | |
Jake Weinstein | d6b191d | 2017-12-11 03:27:49 -0500 | [diff] [blame] | 50 | // fma has builtin routines for ARMv7-A NEON, ARMv8, and ARM64 |
| 51 | |
| 52 | #if defined (__ARM_NEON__) || defined (__aarch64__) |
| 53 | float fmaf(float x, float y, float z) { return __builtin_fmaf(x, y, z); } |
| 54 | double 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 Weinstein | 1e108e3 | 2017-12-11 02:50:04 -0500 | [diff] [blame] | 60 | float ceilf(float x) { return __builtin_ceilf(x); } |
| 61 | double ceil(double x) { return __builtin_ceil(x); } |
| 62 | |
| 63 | float floorf(float x) { return __builtin_floorf(x); } |
| 64 | double floor(double x) { return __builtin_floor(x); } |
| 65 | |
Elliott Hughes | 9a1bb70 | 2017-11-09 22:40:11 +0000 | [diff] [blame] | 66 | float fmaxf(float x, float y) { return __builtin_fmaxf(x, y); } |
| 67 | double fmax(double x, double y) { return __builtin_fmax(x, y); } |
| 68 | |
| 69 | float fminf(float x, float y) { return __builtin_fminf(x, y); } |
| 70 | double fmin(double x, double y) { return __builtin_fmin(x, y); } |
| 71 | |
Jake Weinstein | efa59d8 | 2019-05-10 01:15:08 -0500 | [diff] [blame] | 72 | float nearbyintf(float x) { return __builtin_nearbyintf(x); } |
| 73 | double nearbyint(double x) { return __builtin_nearbyint(x); } |
| 74 | |
Jake Weinstein | d6b191d | 2017-12-11 03:27:49 -0500 | [diff] [blame] | 75 | float rintf(float x) { return __builtin_rintf(x); } |
| 76 | double rint(double x) { return __builtin_rint(x); } |
| 77 | |
| 78 | float roundf(float x) { return __builtin_roundf(x); } |
| 79 | double round(double x) { return __builtin_round(x); } |
| 80 | |
| 81 | float truncf(float x) { return __builtin_truncf(x); } |
| 82 | double trunc(double x) { return __builtin_trunc(x); } |
| 83 | #endif |
| 84 | |
| 85 | #if defined (__aarch64__) |
Jake Weinstein | efa59d8 | 2019-05-10 01:15:08 -0500 | [diff] [blame] | 86 | // 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. |
| 90 | long 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 Hughes | 9a1bb70 | 2017-11-09 22:40:11 +0000 | [diff] [blame] | 98 | #endif |