1 /* d-ctfloat.cc -- D frontend interface to the gcc back-end.
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
20 #include "coretypes.h"
22 #include "dmd/root/ctfloat.h"
23 #include "dmd/target.h"
28 /* Implements the CTFloat interface defined by the frontend.
29 Compile-time floating-pointer helper functions. */
31 /* Return the absolute value of R. */
34 CTFloat::fabs (real_t r
)
37 real_arithmetic (&x
.rv (), ABS_EXPR
, &r
.rv (), NULL
);
38 return x
.normalize ();
41 /* Return the value of R * 2 ^^ EXP. */
44 CTFloat::ldexp (real_t r
, int exp
)
47 real_ldexp (&x
.rv (), &r
.rv (), exp
);
48 return x
.normalize ();
51 /* Return true if longdouble value X is identical to Y. */
54 CTFloat::isIdentical (real_t x
, real_t y
)
56 real_value rx
= x
.rv ();
57 real_value ry
= y
.rv ();
58 return (REAL_VALUE_ISNAN (rx
) && REAL_VALUE_ISNAN (ry
))
59 || real_identical (&rx
, &ry
);
62 /* Return true if real_t value R is NaN. */
65 CTFloat::isNaN (real_t r
)
67 return REAL_VALUE_ISNAN (r
.rv ());
70 /* Same as isNaN, but also check if is signalling. */
73 CTFloat::isSNaN (real_t r
)
75 return REAL_VALUE_ISSIGNALING_NAN (r
.rv ());
78 /* Return true if real_t value is +Inf. */
81 CTFloat::isInfinity (real_t r
)
83 return REAL_VALUE_ISINF (r
.rv ());
86 /* Return a real_t value from string BUFFER rounded to long double mode. */
89 CTFloat::parse (const char *buffer
, bool *overflow
)
92 real_from_string3 (&r
.rv (), buffer
, TYPE_MODE (long_double_type_node
));
94 /* Front-end checks overflow to see if the value is representable. */
95 if (overflow
&& r
== target
.RealProperties
.infinity
)
101 /* Format the real_t value R to string BUFFER as a decimal or hexadecimal,
102 converting the result to uppercase if FMT requests it. */
105 CTFloat::sprint (char *buffer
, char fmt
, real_t r
)
107 if (fmt
== 'a' || fmt
== 'A')
109 /* Converting to a hexadecimal string. */
110 real_to_hexadecimal (buffer
, &r
.rv (), 32, 0, 1);
116 buflen
= strlen (buffer
);
117 for (int i
= 0; i
< buflen
; i
++)
118 buffer
[i
] = TOUPPER (buffer
[i
]);
123 return strlen (buffer
);
131 /* Note: restricting the precision of significant digits to 18. */
132 real_to_decimal (buffer
, &r
.rv (), 32, 18, 1);
133 return strlen (buffer
);
137 /* Return a hash value for real_t value R. */
140 CTFloat::hash (real_t r
)
142 return real_hash (&r
.rv ());