srl_float.h

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 83 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« snappy/csnappy_internal_userspace.h ↑ Summary srl_compress.h »
1 #ifndef SRL_FLOAT_H_
2 #define SRL_FLOAT_H_
3  
4 #include "srl_common.h"
5 #include "srl_protocol.h"
6 #include <float.h>
7 #include <math.h>
8 #include <stdio.h>
9  
10 #ifndef NV_MIN_EXP
11 # ifdef USE_LONG_DOUBLE
12 # define NV_MIN_EXP LDBL_MIN_EXP
13 # else
14 # define NV_MIN_EXP DBL_MIN_EXP
15 # endif
16 #endif
17 #ifndef NV_MAX_EXP
18 # ifdef USE_LONG_DOUBLE
19 # define NV_MAX_EXP LDBL_MAX_EXP
20 # else
21 # define NV_MAX_EXP DBL_MAX_EXP
22 # endif
23 #endif
24  
25 /* C long double is an ABI choice, so its name alone does not identify the
26 * bytes that an encoder writes. These IDs describe the representations that
27 * the native LONG_DOUBLE declaration can distinguish. Binary64 has 53 bits
28 * of precision in 8 bytes; x87 has 64 bits of precision in 10 meaningful
29 * bytes within a 12- or 16-byte object; binary128 has 113 bits in 16 bytes.
30 * IBM double-double stores two binary64 components in 16 bytes.
31 *
32 * The high bit records byte order separately from representation. The
33 * declaration also records storage size because, for example, two x87 ABIs
34 * can agree on precision and byte order while disagreeing on padding size.
35 * Portable FLOAT_128 does not use these IDs: its bytes always have the same
36 * IEEE binary128 representation and little-endian order. */
37  
38 /* Compiler size and precision identify candidate layouts, but they do not
39 * establish byte or component order. The repeating binary fraction -0.1L
40 * supplies nonzero bits in both double-double components, allowing the
41 * byte checks to distinguish those layouts as well. Returning zero means
42 * that no known format ID matches. Explicit native output can still use
43 * opaque storage, whose reader must opt into trusting ABI compatibility. */
44 SRL_STATIC_INLINE U8
45 0 srl_native_long_double_format(void)
46 {
47 0 const long double sample = -0.1L;
48 0 const U8 *b = (const U8 *)&sample;
49 0 U8 format = 0;
50 0 U8 big = 0;
51 if (sizeof(long double) == 8 && LDBL_MANT_DIG == 53) {
52 if (b[0] == 0x9a && b[7] == 0xbf)
53 format = SRL_LD_BINARY64;
54 else if (b[0] == 0xbf && b[7] == 0x9a) {
55 format = SRL_LD_BINARY64;
56 big = 1;
57 }
58 } else if (sizeof(long double) == 16 && LDBL_MANT_DIG == 113) {
59 if (b[0] == 0x9a && b[15] == 0xbf)
60 format = SRL_LD_BINARY128;
61 else if (b[0] == 0xbf && b[15] == 0x9a) {
62 format = SRL_LD_BINARY128;
63 big = 1;
64 }
65 } else if ((sizeof(long double) == 12 || sizeof(long double) == 16)
66 && LDBL_MANT_DIG == 64) {
67 0 ▶ if (b[0] == 0xcd && b[9] == 0xbf)
Branch: 2 branches
BranchTrueFalseTotal
if (b[0] == 0xcd && b[9] == 0xbf) 0 0 0
if (b[0] == 0xcd && b[9] == 0xbf) 0 0 0
68 0 format = SRL_LD_X87;
69 0 ▶ else if (b[0] == 0xbf && b[9] == 0xcd) {
Branch: 2 branches
BranchTrueFalseTotal
else if (b[0] == 0xbf && b[9] == 0xcd) { 0 0 0
else if (b[0] == 0xbf && b[9] == 0xcd) { 0 0 0
70 0 format = SRL_LD_X87;
71 0 big = 1;
72 }
73 } else if (sizeof(long double) == 16
74 && (LDBL_MANT_DIG == 106 || LDBL_MANT_DIG == 107)) {
75 /* The declaration has one endianness bit for the complete value.
76 * These patterns therefore require component order to agree with
77 * byte order; mixed native layouts cannot use this format ID. */
78 if (b[0] == 0x9a && b[7] == 0x3c && b[15] == 0xbf)
79 format = SRL_LD_DOUBLEDOUBLE;
80 else if (b[0] == 0xbf && b[8] == 0x3c && b[15] == 0x9a) {
81 format = SRL_LD_DOUBLEDOUBLE;
82 big = 1;
83 }
84 }
85 0 ▶ return format ? (U8)(format | (big ? SRL_LD_BIG_ENDIAN : 0)) : 0;
Branch: 2 branches
BranchTrueFalseTotal
return format ? (U8)(format | (big ? SRL_LD_BIG_ENDIAN : 0)) : 0; 0 0 0
return format ? (U8)(format | (big ? SRL_LD_BIG_ENDIAN : 0)) : 0; 0 0 0
86 }
87  
88 /* An opaque declaration records raw ABI storage without claiming a known
89 * representation. Its byte-order bit records the producer's integer byte
90 * order as diagnostic information; it cannot describe an unknown layout.
91 * Readers must opt into trusting ABI compatibility before using those bytes. */
92 SRL_STATIC_INLINE U8
93 0 srl_native_long_double_declaration_format(void)
94 {
95 0 U8 format = srl_native_long_double_format();
96 0 ▶ if (format) return format;
Branch: 1 branch
BranchTrueFalseTotal
if (format) return format; 0 0 0
97 #if BYTEORDER == 0x4321 || BYTEORDER == 0x87654321
98 return SRL_LD_OPAQUE | SRL_LD_BIG_ENDIAN;
99 #else
100 0 return SRL_LD_OPAQUE;
101 #endif
102 }
103  
104 SRL_STATIC_INLINE int
105 srl_long_double_format_is_known(U8 format)
106 {
107 switch (format & 127) {
108 case SRL_LD_BINARY64:
109 case SRL_LD_X87:
110 case SRL_LD_BINARY128:
111 case SRL_LD_DOUBLEDOUBLE:
112 return 1;
113 default:
114 return 0;
115 }
116 }
117  
118 SRL_STATIC_INLINE void
119 0 srl_copy_float_little_endian(pTHX_ U8 *dst, const U8 *src)
120 {
121 0 const float sample = 1.0f;
122 0 const U8 *b = (const U8 *)&sample;
123 U8 i;
124 if (sizeof(float) != 4)
125 croak("Unsupported native float width");
126 0 ▶ if (b[3] == 0x3f && b[2] == 0x80)
Branch: 2 branches
BranchTrueFalseTotal
if (b[3] == 0x3f && b[2] == 0x80) 0 0 0
if (b[3] == 0x3f && b[2] == 0x80) 0 0 0
127 0 memcpy(dst, src, 4);
128 0 ▶ else if (b[0] == 0x3f && b[1] == 0x80)
Branch: 2 branches
BranchTrueFalseTotal
else if (b[0] == 0x3f && b[1] == 0x80) 0 0 0
else if (b[0] == 0x3f && b[1] == 0x80) 0 0 0
129 0 ▶ for (i = 0; i < 4; i++) dst[i] = src[3 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) dst[i] = src[3 - i]; 0 0 0
130 else
131 0 croak("Unsupported native float representation");
132 0 }
133  
134 /* Some ABIs store the two words of a double in a different order from
135 * their integer bytes. Inspecting the IEEE representation of 1.0 identifies
136 * that arrangement directly. Each permutation is its own inverse, so the
137 * same helper converts native bytes to little endian and back again. */
138 SRL_STATIC_INLINE void
139 0 srl_copy_double_little_endian(pTHX_ U8 *dst, const U8 *src)
140 {
141 0 const double sample = 1.0;
142 0 const U8 *b = (const U8 *)&sample;
143 U8 i;
144 if (sizeof(double) != 8)
145 croak("Unsupported native double width");
146 0 ▶ if (b[7] == 0x3f && b[6] == 0xf0)
Branch: 2 branches
BranchTrueFalseTotal
if (b[7] == 0x3f && b[6] == 0xf0) 0 0 0
if (b[7] == 0x3f && b[6] == 0xf0) 0 0 0
147 0 memcpy(dst, src, 8);
148 0 ▶ else if (b[0] == 0x3f && b[1] == 0xf0)
Branch: 2 branches
BranchTrueFalseTotal
else if (b[0] == 0x3f && b[1] == 0xf0) 0 0 0
else if (b[0] == 0x3f && b[1] == 0xf0) 0 0 0
149 0 ▶ for (i = 0; i < 8; i++) dst[i] = src[7 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) dst[i] = src[7 - i]; 0 0 0
150 0 ▶ else if (b[3] == 0x3f && b[2] == 0xf0)
Branch: 2 branches
BranchTrueFalseTotal
else if (b[3] == 0x3f && b[2] == 0xf0) 0 0 0
else if (b[3] == 0x3f && b[2] == 0xf0) 0 0 0
151 0 ▶ for (i = 0; i < 8; i++) dst[i] = src[i ^ 4];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) dst[i] = src[i ^ 4]; 0 0 0
152 0 ▶ else if (b[4] == 0x3f && b[5] == 0xf0)
Branch: 2 branches
BranchTrueFalseTotal
else if (b[4] == 0x3f && b[5] == 0xf0) 0 0 0
else if (b[4] == 0x3f && b[5] == 0xf0) 0 0 0
153 0 ▶ for (i = 0; i < 8; i++) dst[i] = src[i ^ 3];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) dst[i] = src[i ^ 3]; 0 0 0
154 else
155 0 croak("Unsupported native double representation");
156 0 }
157  
158 SRL_STATIC_INLINE NV
159 srl_nv_scale(NV value, int exponent)
160 {
161 #ifdef Perl_ldexp
162 return Perl_ldexp(value, exponent);
163 #elif defined(USE_LONG_DOUBLE)
164 return ldexpl(value, exponent);
165 #else
166 return ldexp(value, exponent);
167 #endif
168 }
169  
170 SRL_STATIC_INLINE int
171 srl_binary128_bit(const U8 *bytes, int bit, int normal)
172 {
173 if (bit == 112) return normal;
174 return bit >= 0 && bit < 112 ? ((bytes[bit / 8] >> (bit % 8)) & 1) : 0;
175 }
176  
177 /* A reader must be able to consume portable binary128 even when its Perl
178 * uses double and its compiler provides no binary128 arithmetic. Decode the
179 * significand as bits and round it to the NV precision before applying the
180 * exponent. An intermediate conversion through double would discard bits
181 * that an x87 or binary128 NV could otherwise retain.
182 *
183 * The number of discarded bits also accounts for the target's smallest
184 * subnormal, so nearest/even rounding applies at the underflow boundary as
185 * well as to normal values. Finite overflow becomes signed infinity;
186 * underflow becomes a subnormal or signed zero. This conversion returns a
187 * numeric NV rather than a retained wire bit pattern, so NaNs use NV_NAN
188 * without mapping their payload bits between representations. */
189 SRL_STATIC_INLINE NV
190 srl_binary128_to_nv(const U8 bytes[16])
191 {
192 const int negative = (bytes[15] & 0x80) != 0;
193 const int exponent = ((bytes[15] & 0x7f) << 8) | bytes[14];
194 const int scale = (exponent ? exponent - 16383 : -16382) - 112;
195 int drop = 113 - NV_MANT_DIG;
196 int i, sticky = 0;
197 NV value = 0;
198 if (exponent == 32767) {
199 for (i = 0; i < 14; i++) sticky |= bytes[i];
200 value = sticky ? (NV)NV_NAN : (NV)NV_INF;
201 return negative ? -value : value;
202 }
203 if (drop < 0) drop = 0;
204 if (NV_MIN_EXP - NV_MANT_DIG - scale > drop)
205 drop = NV_MIN_EXP - NV_MANT_DIG - scale;
206 if (drop > 113) return negative ? -(NV)0.0 : (NV)0.0;
207 for (i = 112; i >= drop; i--)
208 value = value * 2 + srl_binary128_bit(bytes, i, exponent != 0);
209 if (drop) {
210 for (i = 0; i < drop - 1; i++)
211 sticky |= srl_binary128_bit(bytes, i, exponent != 0);
212 if (srl_binary128_bit(bytes, drop - 1, exponent != 0)
213 && (sticky || srl_binary128_bit(bytes, drop, exponent != 0)))
214 value += 1;
215 }
216 if (value != 0) {
217 int value_exponent;
218 (void)Perl_frexp(value, &value_exponent);
219 if (value_exponent + scale + drop > NV_MAX_EXP)
220 value = (NV)NV_INF;
221 else
222 value = srl_nv_scale(value, scale + drop);
223 }
224 return negative ? -value : value;
225 }
226  
227 SRL_STATIC_INLINE int
228 srl_nv_signbit(NV value)
229 {
230 #ifdef Perl_signbit
231 return Perl_signbit(value) != 0;
232 #else
233 /* Older Perls do not expose signbit on every C library. Comparisons
234 * handle nonzero finite values and infinities, but cannot distinguish
235 * the two zeros. Formatting preserves that sign and also handles NaN.
236 * Only zero and NaN reach sprintf, so neither their magnitude nor the
237 * current locale can produce an unbounded numeric expansion here. */
238 char text[32];
239 if (value < 0) return 1;
240 if (value > 0) return 0;
241 sprintf(text, "%g", (double)value);
242 return text[0] == '-';
243 #endif
244 }
245  
246 /* Encoding must produce the same binary128 layout for x87, binary128 and
247 * double-double NVs. Arithmetic extraction avoids copying ABI-specific
248 * storage or depending on a compiler binary128 type. The destination is
249 * initialized before any bits are set, so native padding is never emitted.
250 *
251 * A double-double value can have two widely separated nonzero components.
252 * Its usual precision is less than 113 bits, but the span between those
253 * components can exceed a binary128 significand. Retaining a guard bit and
254 * the remaining fraction allows those values to be rounded as well. */
255 SRL_STATIC_INLINE void
256 srl_nv_to_binary128(NV value, U8 bytes[16])
257 {
258 int exponent, encoded_exp, bit, i;
259 NV fraction;
260 int negative = srl_nv_signbit(value);
261 memset(bytes, 0, 16);
262 if (Perl_isnan(value)) {
263 bytes[13] = 0x80; bytes[14] = 0xff; bytes[15] = 0x7f;
264 } else if (Perl_isinf(value)) {
265 bytes[14] = 0xff; bytes[15] = 0x7f;
266 } else if (value != 0) {
267 if (negative) value = -value;
268 fraction = Perl_frexp(value, &exponent);
269 encoded_exp = exponent - 1 + 16383;
270 if (encoded_exp >= 32767) {
271 bytes[14] = 0xff; bytes[15] = 0x7f;
272 } else {
273 bit = 112;
274 if (encoded_exp <= 0) {
275 bit += encoded_exp - 1;
276 encoded_exp = 0;
277 }
278 while (bit >= 0) {
279 fraction *= 2;
280 if (fraction >= 1) {
281 if (bit < 112) bytes[bit / 8] |= (U8)(1 << (bit % 8));
282 fraction -= 1;
283 }
284 bit--;
285 }
286 /* The next bit is the guard bit; the rest of the fraction
287 * distinguishes an exact tie from a value above it. In a tie,
288 * the retained low bit selects the even significand. */
289 fraction *= 2;
290 if (fraction > 1 || (fraction == 1 && (bytes[0] & 1))) {
291 for (i = 0; i < 14; i++)
292 if (++bytes[i]) break;
293 if (i == 14) encoded_exp++;
294 }
295 bytes[14] = (U8)encoded_exp;
296 bytes[15] = (U8)(encoded_exp >> 8);
297 }
298 }
299 if (negative) bytes[15] |= 0x80;
300 }
301 #endif
« snappy/csnappy_internal_userspace.h ↑ Summary srl_compress.h »