1*0928368fSKristof Beyls /*
2*0928368fSKristof Beyls * Single-precision math error handling.
3*0928368fSKristof Beyls *
4*0928368fSKristof Beyls * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*0928368fSKristof Beyls * See https://llvm.org/LICENSE.txt for license information.
6*0928368fSKristof Beyls * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*0928368fSKristof Beyls */
8*0928368fSKristof Beyls
9*0928368fSKristof Beyls #include "math_config.h"
10*0928368fSKristof Beyls
11*0928368fSKristof Beyls #if WANT_ERRNO
12*0928368fSKristof Beyls #include <errno.h>
13*0928368fSKristof Beyls /* NOINLINE reduces code size and avoids making math functions non-leaf
14*0928368fSKristof Beyls when the error handling is inlined. */
15*0928368fSKristof Beyls NOINLINE static float
with_errnof(float y,int e)16*0928368fSKristof Beyls with_errnof (float y, int e)
17*0928368fSKristof Beyls {
18*0928368fSKristof Beyls errno = e;
19*0928368fSKristof Beyls return y;
20*0928368fSKristof Beyls }
21*0928368fSKristof Beyls #else
22*0928368fSKristof Beyls #define with_errnof(x, e) (x)
23*0928368fSKristof Beyls #endif
24*0928368fSKristof Beyls
25*0928368fSKristof Beyls /* NOINLINE reduces code size. */
26*0928368fSKristof Beyls NOINLINE static float
xflowf(uint32_t sign,float y)27*0928368fSKristof Beyls xflowf (uint32_t sign, float y)
28*0928368fSKristof Beyls {
29*0928368fSKristof Beyls y = eval_as_float (opt_barrier_float (sign ? -y : y) * y);
30*0928368fSKristof Beyls return with_errnof (y, ERANGE);
31*0928368fSKristof Beyls }
32*0928368fSKristof Beyls
33*0928368fSKristof Beyls HIDDEN float
__math_uflowf(uint32_t sign)34*0928368fSKristof Beyls __math_uflowf (uint32_t sign)
35*0928368fSKristof Beyls {
36*0928368fSKristof Beyls return xflowf (sign, 0x1p-95f);
37*0928368fSKristof Beyls }
38*0928368fSKristof Beyls
39*0928368fSKristof Beyls #if WANT_ERRNO_UFLOW
40*0928368fSKristof Beyls /* Underflows to zero in some non-nearest rounding mode, setting errno
41*0928368fSKristof Beyls is valid even if the result is non-zero, but in the subnormal range. */
42*0928368fSKristof Beyls HIDDEN float
__math_may_uflowf(uint32_t sign)43*0928368fSKristof Beyls __math_may_uflowf (uint32_t sign)
44*0928368fSKristof Beyls {
45*0928368fSKristof Beyls return xflowf (sign, 0x1.4p-75f);
46*0928368fSKristof Beyls }
47*0928368fSKristof Beyls #endif
48*0928368fSKristof Beyls
49*0928368fSKristof Beyls HIDDEN float
__math_oflowf(uint32_t sign)50*0928368fSKristof Beyls __math_oflowf (uint32_t sign)
51*0928368fSKristof Beyls {
52*0928368fSKristof Beyls return xflowf (sign, 0x1p97f);
53*0928368fSKristof Beyls }
54*0928368fSKristof Beyls
55*0928368fSKristof Beyls HIDDEN float
__math_divzerof(uint32_t sign)56*0928368fSKristof Beyls __math_divzerof (uint32_t sign)
57*0928368fSKristof Beyls {
58*0928368fSKristof Beyls float y = opt_barrier_float (sign ? -1.0f : 1.0f) / 0.0f;
59*0928368fSKristof Beyls return with_errnof (y, ERANGE);
60*0928368fSKristof Beyls }
61*0928368fSKristof Beyls
62*0928368fSKristof Beyls HIDDEN float
__math_invalidf(float x)63*0928368fSKristof Beyls __math_invalidf (float x)
64*0928368fSKristof Beyls {
65*0928368fSKristof Beyls float y = (x - x) / (x - x);
66*0928368fSKristof Beyls return isnan (x) ? y : with_errnof (y, EDOM);
67*0928368fSKristof Beyls }
68