1
+ #ifndef QUAD_CONSTANTS_HPP
2
+ #define QUAD_CONSTANTS_HPP
3
+
4
+ #include < sleef.h>
5
+ #include < sleefquad.h>
6
+ #include < stdint.h>
7
+ #include < string.h>
8
+
9
+ // Quad precision constants using sleef_q macro
10
+ #define QUAD_PRECISION_ZERO sleef_q (+0x0000000000000LL , 0x0000000000000000ULL , -16383 )
11
+ #define QUAD_PRECISION_ONE sleef_q (+0x1000000000000LL , 0x0000000000000000ULL , 0 )
12
+ #define QUAD_PRECISION_INF sleef_q (+0x1000000000000LL , 0x0000000000000000ULL , 16384 )
13
+ #define QUAD_PRECISION_NINF sleef_q (-0x1000000000000LL , 0x0000000000000000ULL , 16384 )
14
+ #define QUAD_PRECISION_NAN sleef_q (+0x1ffffffffffffLL , 0xffffffffffffffffULL , 16384 )
15
+
16
+ // Additional constants
17
+ #define QUAD_PRECISION_MAX_FINITE SLEEF_QUAD_MAX
18
+ #define QUAD_PRECISION_MIN_FINITE Sleef_negq1 (SLEEF_QUAD_MAX)
19
+ #define QUAD_PRECISION_RADIX sleef_q (+0x1000000000000LL , 0x0000000000000000ULL , 1 ) // 2.0
20
+
21
+ #ifdef SLEEF_QUAD_C
22
+ static const Sleef_quad SMALLEST_SUBNORMAL_VALUE = SLEEF_QUAD_DENORM_MIN;
23
+ #else
24
+ static const union {
25
+ struct {
26
+ #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
27
+ uint64_t h, l;
28
+ #else
29
+ uint64_t l, h;
30
+ #endif
31
+ } parts;
32
+ Sleef_quad value;
33
+ } smallest_subnormal_const = {.parts = {
34
+ #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
35
+ .h = 0x0000000000000000ULL , .l = 0x0000000000000001ULL
36
+ #else
37
+ .l = 0x0000000000000001ULL , .h = 0x0000000000000000ULL
38
+ #endif
39
+ }};
40
+ #define SMALLEST_SUBNORMAL_VALUE (smallest_subnormal_const.value)
41
+ #endif
42
+
43
+ // Integer constants for finfo
44
+ #define QUAD_NMANT 112 // mantissa bits (excluding implicit bit)
45
+ #define QUAD_MIN_EXP -16382 // minimum exponent for normalized numbers
46
+ #define QUAD_MAX_EXP 16384 // maximum exponent
47
+ #define QUAD_DECIMAL_DIGITS 33 // decimal digits of precision
48
+
49
+ typedef enum ConstantResultType {
50
+ CONSTANT_QUAD, // Sleef_quad value
51
+ CONSTANT_INT64, // int64_t value
52
+ CONSTANT_ERROR // Error occurred
53
+ } ConstantResultType;
54
+
55
+ typedef struct ConstantResult {
56
+ ConstantResultType type;
57
+ union {
58
+ Sleef_quad quad_value;
59
+ int64_t int_value;
60
+ } data;
61
+ } ConstantResult;
62
+
63
+
64
+ static inline ConstantResult get_sleef_constant_by_name (const char * constant_name) {
65
+ ConstantResult result;
66
+
67
+ if (strcmp (constant_name, " pi" ) == 0 ) {
68
+ result.type = CONSTANT_QUAD;
69
+ result.data .quad_value = SLEEF_M_PIq;
70
+ }
71
+ else if (strcmp (constant_name, " e" ) == 0 ) {
72
+ result.type = CONSTANT_QUAD;
73
+ result.data .quad_value = SLEEF_M_Eq;
74
+ }
75
+ else if (strcmp (constant_name, " log2e" ) == 0 ) {
76
+ result.type = CONSTANT_QUAD;
77
+ result.data .quad_value = SLEEF_M_LOG2Eq;
78
+ }
79
+ else if (strcmp (constant_name, " log10e" ) == 0 ) {
80
+ result.type = CONSTANT_QUAD;
81
+ result.data .quad_value = SLEEF_M_LOG10Eq;
82
+ }
83
+ else if (strcmp (constant_name, " ln2" ) == 0 ) {
84
+ result.type = CONSTANT_QUAD;
85
+ result.data .quad_value = SLEEF_M_LN2q;
86
+ }
87
+ else if (strcmp (constant_name, " ln10" ) == 0 ) {
88
+ result.type = CONSTANT_QUAD;
89
+ result.data .quad_value = SLEEF_M_LN10q;
90
+ }
91
+ else if (strcmp (constant_name, " max_value" ) == 0 ) {
92
+ result.type = CONSTANT_QUAD;
93
+ result.data .quad_value = SLEEF_QUAD_MAX;
94
+ }
95
+ else if (strcmp (constant_name, " epsilon" ) == 0 ) {
96
+ result.type = CONSTANT_QUAD;
97
+ result.data .quad_value = SLEEF_QUAD_EPSILON;
98
+ }
99
+ else if (strcmp (constant_name, " smallest_normal" ) == 0 ) {
100
+ result.type = CONSTANT_QUAD;
101
+ result.data .quad_value = SLEEF_QUAD_MIN;
102
+ }
103
+ else if (strcmp (constant_name, " smallest_subnormal" ) == 0 ) {
104
+ result.type = CONSTANT_QUAD;
105
+ result.data .quad_value = SMALLEST_SUBNORMAL_VALUE;
106
+ }
107
+ else if (strcmp (constant_name, " bits" ) == 0 ) {
108
+ result.type = CONSTANT_INT64;
109
+ result.data .int_value = sizeof (Sleef_quad) * 8 ;
110
+ }
111
+ else if (strcmp (constant_name, " precision" ) == 0 ) {
112
+ result.type = CONSTANT_INT64;
113
+ // precision = int(-log10(epsilon))
114
+ result.data .int_value =
115
+ Sleef_cast_to_int64q1 (Sleef_negq1 (Sleef_log10q1_u10 (SLEEF_QUAD_EPSILON)));
116
+ }
117
+ else if (strcmp (constant_name, " resolution" ) == 0 ) {
118
+ result.type = CONSTANT_QUAD;
119
+ // precision = int(-log10(epsilon))
120
+ int64_t precision =
121
+ Sleef_cast_to_int64q1 (Sleef_negq1 (Sleef_log10q1_u10 (SLEEF_QUAD_EPSILON)));
122
+ // resolution = 10 ** (-precision)
123
+ result.data .quad_value =
124
+ Sleef_powq1_u10 (Sleef_cast_from_int64q1 (10 ), Sleef_cast_from_int64q1 (-precision));
125
+ }
126
+ else {
127
+ result.type = CONSTANT_ERROR;
128
+ }
129
+
130
+ return result;
131
+ }
132
+
133
+ #endif // QUAD_CONSTANTS_HPP
0 commit comments