xref: /aosp_15_r20/external/arm-optimized-routines/math/math_err.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision math error handling.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2018, 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 #if WANT_ERRNO_UFLOW
39*412f47f9SXin Li /* Underflows to zero in some non-nearest rounding mode, setting errno
40*412f47f9SXin Li    is valid even if the result is non-zero, but in the subnormal range.  */
41*412f47f9SXin Li HIDDEN double
__math_may_uflow(uint32_t sign)42*412f47f9SXin Li __math_may_uflow (uint32_t sign)
43*412f47f9SXin Li {
44*412f47f9SXin Li   return xflow (sign, 0x1.8p-538);
45*412f47f9SXin Li }
46*412f47f9SXin Li #endif
47*412f47f9SXin Li 
48*412f47f9SXin Li HIDDEN double
__math_oflow(uint32_t sign)49*412f47f9SXin Li __math_oflow (uint32_t sign)
50*412f47f9SXin Li {
51*412f47f9SXin Li   return xflow (sign, 0x1p769);
52*412f47f9SXin Li }
53*412f47f9SXin Li 
54*412f47f9SXin Li HIDDEN double
__math_divzero(uint32_t sign)55*412f47f9SXin Li __math_divzero (uint32_t sign)
56*412f47f9SXin Li {
57*412f47f9SXin Li   double y = opt_barrier_double (sign ? -1.0 : 1.0) / 0.0;
58*412f47f9SXin Li   return with_errno (y, ERANGE);
59*412f47f9SXin Li }
60*412f47f9SXin Li 
61*412f47f9SXin Li HIDDEN double
__math_invalid(double x)62*412f47f9SXin Li __math_invalid (double x)
63*412f47f9SXin Li {
64*412f47f9SXin Li   double y = (x - x) / (x - x);
65*412f47f9SXin Li   return isnan (x) ? y : with_errno (y, EDOM);
66*412f47f9SXin Li }
67*412f47f9SXin Li 
68*412f47f9SXin Li /* Check result and set errno if necessary.  */
69*412f47f9SXin Li 
70*412f47f9SXin Li HIDDEN double
__math_check_uflow(double y)71*412f47f9SXin Li __math_check_uflow (double y)
72*412f47f9SXin Li {
73*412f47f9SXin Li   return y == 0.0 ? with_errno (y, ERANGE) : y;
74*412f47f9SXin Li }
75*412f47f9SXin Li 
76*412f47f9SXin Li HIDDEN double
__math_check_oflow(double y)77*412f47f9SXin Li __math_check_oflow (double y)
78*412f47f9SXin Li {
79*412f47f9SXin Li   return isinf (y) ? with_errno (y, ERANGE) : y;
80*412f47f9SXin Li }
81