1/* Double-precision math error handling.
2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <math-barriers.h>
20#include "math_config.h"
21
22#if WANT_ERRNO
23#include <errno.h>
24/* NOINLINE reduces code size and avoids making math functions non-leaf
25 when the error handling is inlined. */
26NOINLINE static double
27with_errno (double y, int e)
28{
29 errno = e;
30 return y;
31}
32#else
33#define with_errno(x, e) (x)
34#endif
35
36/* NOINLINE reduces code size. */
37NOINLINE static double
38xflow (uint32_t sign, double y)
39{
40 y = math_opt_barrier (sign ? -y : y) * y;
41 return with_errno (y, ERANGE);
42}
43
44attribute_hidden double
45__math_uflow (uint32_t sign)
46{
47 return xflow (sign, 0x1p-767);
48}
49
50#if WANT_ERRNO_UFLOW
51/* Underflows to zero in some non-nearest rounding mode, setting errno
52 is valid even if the result is non-zero, but in the subnormal range. */
53attribute_hidden double
54__math_may_uflow (uint32_t sign)
55{
56 return xflow (sign, 0x1.8p-538);
57}
58#endif
59
60attribute_hidden double
61__math_oflow (uint32_t sign)
62{
63 return xflow (sign, 0x1p769);
64}
65
66attribute_hidden double
67__math_divzero (uint32_t sign)
68{
69 double y = math_opt_barrier (sign ? -1.0 : 1.0) / 0.0;
70 return with_errno (y, ERANGE);
71}
72
73attribute_hidden double
74__math_invalid (double x)
75{
76 double y = (x - x) / (x - x);
77 return isnan (x) ? y : with_errno (y, EDOM);
78}
79
80/* Check result and set errno if necessary. */
81
82attribute_hidden double
83__math_check_uflow (double y)
84{
85 return y == 0.0 ? with_errno (y, ERANGE) : y;
86}
87
88attribute_hidden double
89__math_check_oflow (double y)
90{
91 return isinf (y) ? with_errno (y, ERANGE) : y;
92}
93