Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Double-precision math error handling. |
| 3 | * |
| 4 | * Copyright (c) 2018, Arm Limited. |
Szabolcs Nagy | 11253b0 | 2018-11-12 11:10:57 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: MIT |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "math_config.h" |
| 9 | |
| 10 | #if WANT_ERRNO |
| 11 | #include <errno.h> |
| 12 | /* NOINLINE reduces code size and avoids making math functions non-leaf |
| 13 | when the error handling is inlined. */ |
| 14 | NOINLINE static double |
| 15 | with_errno (double y, int e) |
| 16 | { |
| 17 | errno = e; |
| 18 | return y; |
| 19 | } |
| 20 | #else |
| 21 | #define with_errno(x, e) (x) |
| 22 | #endif |
| 23 | |
Szabolcs Nagy | 5fa69e1 | 2018-06-12 17:18:24 +0100 | [diff] [blame] | 24 | /* NOINLINE reduces code size. */ |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 25 | NOINLINE static double |
| 26 | xflow (uint32_t sign, double y) |
| 27 | { |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 28 | y = eval_as_double (opt_barrier_double (sign ? -y : y) * y); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 29 | return with_errno (y, ERANGE); |
| 30 | } |
| 31 | |
| 32 | HIDDEN double |
| 33 | __math_uflow (uint32_t sign) |
| 34 | { |
| 35 | return xflow (sign, 0x1p-767); |
| 36 | } |
| 37 | |
| 38 | #if WANT_ERRNO_UFLOW |
| 39 | /* Underflows to zero in some non-nearest rounding mode, setting errno |
| 40 | is valid even if the result is non-zero, but in the subnormal range. */ |
| 41 | HIDDEN double |
| 42 | __math_may_uflow (uint32_t sign) |
| 43 | { |
| 44 | return xflow (sign, 0x1.8p-538); |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | HIDDEN double |
| 49 | __math_oflow (uint32_t sign) |
| 50 | { |
| 51 | return xflow (sign, 0x1p769); |
| 52 | } |
| 53 | |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 54 | HIDDEN double |
| 55 | __math_divzero (uint32_t sign) |
| 56 | { |
Szabolcs Nagy | 5fa69e1 | 2018-06-12 17:18:24 +0100 | [diff] [blame] | 57 | double y = opt_barrier_double (sign ? -1.0 : 1.0) / 0.0; |
| 58 | return with_errno (y, ERANGE); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | HIDDEN double |
| 62 | __math_invalid (double x) |
| 63 | { |
| 64 | double y = (x - x) / (x - x); |
| 65 | return isnan (x) ? y : with_errno (y, EDOM); |
| 66 | } |
| 67 | |
| 68 | /* Check result and set errno if necessary. */ |
| 69 | |
| 70 | HIDDEN double |
| 71 | __math_check_uflow (double y) |
| 72 | { |
| 73 | return y == 0.0 ? with_errno (y, ERANGE) : y; |
| 74 | } |
| 75 | |
| 76 | HIDDEN double |
| 77 | __math_check_oflow (double y) |
| 78 | { |
| 79 | return isinf (y) ? with_errno (y, ERANGE) : y; |
| 80 | } |