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