1 | /* Single-precision math error handling. |
---|
2 | Copyright (c) 2017 ARM Ltd. All rights reserved. |
---|
3 | |
---|
4 | Redistribution and use in source and binary forms, with or without |
---|
5 | modification, are permitted provided that the following conditions |
---|
6 | are met: |
---|
7 | 1. Redistributions of source code must retain the above copyright |
---|
8 | notice, this list of conditions and the following disclaimer. |
---|
9 | 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | notice, this list of conditions and the following disclaimer in the |
---|
11 | documentation and/or other materials provided with the distribution. |
---|
12 | 3. The name of the company may not be used to endorse or promote |
---|
13 | products derived from this software without specific prior written |
---|
14 | permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS AND ANY EXPRESS OR IMPLIED |
---|
17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
---|
18 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
---|
21 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
---|
26 | |
---|
27 | #include "fdlibm.h" |
---|
28 | #if !__OBSOLETE_MATH |
---|
29 | |
---|
30 | #include "math_config.h" |
---|
31 | |
---|
32 | #if WANT_ERRNO |
---|
33 | #include <errno.h> |
---|
34 | /* NOINLINE reduces code size and avoids making math functions non-leaf |
---|
35 | when the error handling is inlined. */ |
---|
36 | NOINLINE static float |
---|
37 | with_errnof (float y, int e) |
---|
38 | { |
---|
39 | errno = e; |
---|
40 | return y; |
---|
41 | } |
---|
42 | #else |
---|
43 | #define with_errnof(x, e) (x) |
---|
44 | #endif |
---|
45 | |
---|
46 | /* NOINLINE prevents fenv semantics breaking optimizations. */ |
---|
47 | NOINLINE static float |
---|
48 | xflowf (unsigned long sign, float y) |
---|
49 | { |
---|
50 | y = (sign ? -y : y) * y; |
---|
51 | return with_errnof (y, ERANGE); |
---|
52 | } |
---|
53 | |
---|
54 | HIDDEN float |
---|
55 | __math_uflowf (unsigned long sign) |
---|
56 | { |
---|
57 | return xflowf (sign, 0x1p-95f); |
---|
58 | } |
---|
59 | |
---|
60 | #if WANT_ERRNO_UFLOW |
---|
61 | /* Underflows to zero in some non-nearest rounding mode, setting errno |
---|
62 | is valid even if the result is non-zero, but in the subnormal range. */ |
---|
63 | HIDDEN float |
---|
64 | __math_may_uflowf (unsigned long sign) |
---|
65 | { |
---|
66 | return xflowf (sign, 0x1.4p-75f); |
---|
67 | } |
---|
68 | #endif |
---|
69 | |
---|
70 | HIDDEN float |
---|
71 | __math_oflowf (unsigned long sign) |
---|
72 | { |
---|
73 | return xflowf (sign, 0x1p97f); |
---|
74 | } |
---|
75 | |
---|
76 | HIDDEN float |
---|
77 | __math_divzerof (unsigned long sign) |
---|
78 | { |
---|
79 | float y = 0; |
---|
80 | return with_errnof ((sign ? -1 : 1) / y, ERANGE); |
---|
81 | } |
---|
82 | |
---|
83 | HIDDEN float |
---|
84 | __math_invalidf (float x) |
---|
85 | { |
---|
86 | float y = (x - x) / (x - x); |
---|
87 | return isnan (x) ? y : with_errnof (y, EDOM); |
---|
88 | } |
---|
89 | #endif /* !__OBSOLETE_MATH */ |
---|