blob: 1bf9538a1ab1d43ee26a20b8a57d2c129685fcd7 [file] [log] [blame]
Szabolcs Nagy872aa772018-04-27 16:17:24 +01001/*
2 * Double-precision math error handling.
3 *
4 * Copyright (c) 2018, Arm Limited.
Szabolcs Nagy11253b02018-11-12 11:10:57 +00005 * SPDX-License-Identifier: MIT
Szabolcs Nagy872aa772018-04-27 16:17:24 +01006 */
7
8#include "math_config.h"
9
10#if WANT_ERRNO
11#include <errno.h>
12/* NOINLINE reduces code size and avoids making math functions non-leaf
13 when the error handling is inlined. */
14NOINLINE static double
15with_errno (double y, int e)
16{
17 errno = e;
18 return y;
19}
20#else
21#define with_errno(x, e) (x)
22#endif
23
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +010024/* NOINLINE reduces code size. */
Szabolcs Nagy872aa772018-04-27 16:17:24 +010025NOINLINE static double
26xflow (uint32_t sign, double y)
27{
Szabolcs Nagy04884bd2018-12-07 14:58:51 +000028 y = eval_as_double (opt_barrier_double (sign ? -y : y) * y);
Szabolcs Nagy872aa772018-04-27 16:17:24 +010029 return with_errno (y, ERANGE);
30}
31
32HIDDEN double
33__math_uflow (uint32_t sign)
34{
35 return xflow (sign, 0x1p-767);
36}
37
38#if WANT_ERRNO_UFLOW
39/* Underflows to zero in some non-nearest rounding mode, setting errno
40 is valid even if the result is non-zero, but in the subnormal range. */
41HIDDEN double
42__math_may_uflow (uint32_t sign)
43{
44 return xflow (sign, 0x1.8p-538);
45}
46#endif
47
48HIDDEN double
49__math_oflow (uint32_t sign)
50{
51 return xflow (sign, 0x1p769);
52}
53
Szabolcs Nagy872aa772018-04-27 16:17:24 +010054HIDDEN double
55__math_divzero (uint32_t sign)
56{
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +010057 double y = opt_barrier_double (sign ? -1.0 : 1.0) / 0.0;
58 return with_errno (y, ERANGE);
Szabolcs Nagy872aa772018-04-27 16:17:24 +010059}
60
61HIDDEN double
62__math_invalid (double x)
63{
64 double y = (x - x) / (x - x);
65 return isnan (x) ? y : with_errno (y, EDOM);
66}
67
68/* Check result and set errno if necessary. */
69
70HIDDEN double
71__math_check_uflow (double y)
72{
73 return y == 0.0 ? with_errno (y, ERANGE) : y;
74}
75
76HIDDEN double
77__math_check_oflow (double y)
78{
79 return isinf (y) ? with_errno (y, ERANGE) : y;
80}