xref: /aosp_15_r20/external/arm-optimized-routines/math/math_errf.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-precision math error handling.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2017-2020, 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 float
with_errnof(float y,int e)15*412f47f9SXin Li with_errnof (float 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_errnof(x, e) (x)
22*412f47f9SXin Li #endif
23*412f47f9SXin Li 
24*412f47f9SXin Li /* NOINLINE reduces code size.  */
25*412f47f9SXin Li NOINLINE static float
xflowf(uint32_t sign,float y)26*412f47f9SXin Li xflowf (uint32_t sign, float y)
27*412f47f9SXin Li {
28*412f47f9SXin Li   y = eval_as_float (opt_barrier_float (sign ? -y : y) * y);
29*412f47f9SXin Li   return with_errnof (y, ERANGE);
30*412f47f9SXin Li }
31*412f47f9SXin Li 
32*412f47f9SXin Li HIDDEN float
__math_uflowf(uint32_t sign)33*412f47f9SXin Li __math_uflowf (uint32_t sign)
34*412f47f9SXin Li {
35*412f47f9SXin Li   return xflowf (sign, 0x1p-95f);
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 float
__math_may_uflowf(uint32_t sign)42*412f47f9SXin Li __math_may_uflowf (uint32_t sign)
43*412f47f9SXin Li {
44*412f47f9SXin Li   return xflowf (sign, 0x1.4p-75f);
45*412f47f9SXin Li }
46*412f47f9SXin Li #endif
47*412f47f9SXin Li 
48*412f47f9SXin Li HIDDEN float
__math_oflowf(uint32_t sign)49*412f47f9SXin Li __math_oflowf (uint32_t sign)
50*412f47f9SXin Li {
51*412f47f9SXin Li   return xflowf (sign, 0x1p97f);
52*412f47f9SXin Li }
53*412f47f9SXin Li 
54*412f47f9SXin Li HIDDEN float
__math_divzerof(uint32_t sign)55*412f47f9SXin Li __math_divzerof (uint32_t sign)
56*412f47f9SXin Li {
57*412f47f9SXin Li   float y = opt_barrier_float (sign ? -1.0f : 1.0f) / 0.0f;
58*412f47f9SXin Li   return with_errnof (y, ERANGE);
59*412f47f9SXin Li }
60*412f47f9SXin Li 
61*412f47f9SXin Li HIDDEN float
__math_invalidf(float x)62*412f47f9SXin Li __math_invalidf (float x)
63*412f47f9SXin Li {
64*412f47f9SXin Li   float y = (x - x) / (x - x);
65*412f47f9SXin Li   return isnan (x) ? y : with_errnof (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 float
__math_check_uflowf(float y)71*412f47f9SXin Li __math_check_uflowf (float y)
72*412f47f9SXin Li {
73*412f47f9SXin Li   return y == 0.0f ? with_errnof (y, ERANGE) : y;
74*412f47f9SXin Li }
75*412f47f9SXin Li 
76*412f47f9SXin Li HIDDEN float
__math_check_oflowf(float y)77*412f47f9SXin Li __math_check_oflowf (float y)
78*412f47f9SXin Li {
79*412f47f9SXin Li   return isinf (y) ? with_errnof (y, ERANGE) : y;
80*412f47f9SXin Li }
81