Debian Codegraph @

pypy/module/cpyext/src/floatobject.c

raw symbols SCIP
1 #include "Python.h"
2
3 /* this is for the benefit of the pack/unpack routines below */
4
5 typedef enum {
6 unknown_format, ieee_big_endian_format, ieee_little_endian_format
7 } float_format_type;
8
9 static float_format_type double_format, float_format;
10
11 void
12 _PyFloat_InitState()
13 {
14 float_format_type detected_double_format, detected_float_format;
15
16 /* We attempt to determine if this machine is using IEEE
17 floating point formats by peering at the bits of some
18 carefully chosen values. If it looks like we are on an
19 IEEE platform, the float packing/unpacking routines can
20 just copy bits, if not they resort to arithmetic & shifts
21 and masks. The shifts & masks approach works on all finite
22 values, but what happens to infinities, NaNs and signed
23 zeroes on packing is an accident, and attempting to unpack
24 a NaN or an infinity will raise an exception.
25
26 Note that if we're on some whacked-out platform which uses
27 IEEE formats but isn't strictly little-endian or big-
28 endian, we will fall back to the portable shifts & masks
29 method. */
30
31 #if SIZEOF_DOUBLE == 8
32 {
33 double x = 9006104071832581.0;
34 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
35 detected_double_format = ieee_big_endian_format;
36 else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
37 detected_double_format = ieee_little_endian_format;
38 else
39 detected_double_format = unknown_format;
40 }
41 #else
42 detected_double_format = unknown_format;
43 #endif
44
45 #if SIZEOF_FLOAT == 4
46 {
47 float y = 16711938.0;
48 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
49 detected_float_format = ieee_big_endian_format;
50 else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
51 detected_float_format = ieee_little_endian_format;
52 else
53 detected_float_format = unknown_format;
54 }
55 #else
56 detected_float_format = unknown_format;
57 #endif
58
59 double_format = detected_double_format;
60 float_format = detected_float_format;
61 }
62
63
64 /*----------------------------------------------------------------------------
65 * PyFloat_{Pack,Unpack}{2,4,8}. See floatobject.h.
66 * To match the NPY_HALF_ROUND_TIES_TO_EVEN behavior in:
67 * https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/halffloat.c
68 * We use:
69 * bits = (unsigned short)f; Note the truncation
70 * if ((f - bits > 0.5) || (f - bits == 0.5 && bits % 2)) {
71 * bits++;
72 * }
73 */
74
75 int
76 PyFloat_Pack2(double x, char *data, int le)
77 {
78 unsigned char *p = (unsigned char *)data;
79 unsigned char sign;
80 int e;
81 double f;
82 unsigned short bits;
83 int incr = 1;
84
85 if (x == 0.0) {
86 sign = (copysign(1.0, x) == -1.0);
87 e = 0;
88 bits = 0;
89 }
90 else if (Py_IS_INFINITY(x)) {
91 sign = (x < 0.0);
92 e = 0x1f;
93 bits = 0;
94 }
95 else if (Py_IS_NAN(x)) {
96 /* There are 2046 distinct half-precision NaNs (1022 signaling and
97 1024 quiet), but there are only two quiet NaNs that don't arise by
98 quieting a signaling NaN; we get those by setting the topmost bit
99 of the fraction field and clearing all other fraction bits. We
100 choose the one with the appropriate sign. */
101 sign = (copysign(1.0, x) == -1.0);
102 e = 0x1f;
103 bits = 512;
104 }
105 else {
106 sign = (x < 0.0);
107 if (sign) {
108 x = -x;
109 }
110
111 f = frexp(x, &e);
112 if (f < 0.5 || f >= 1.0) {
113 PyErr_SetString(PyExc_SystemError,
114 "frexp() result out of range in PyFloat_Pack2");
115 return -1;
116 }
117
118 /* Normalize f to be in the range [1.0, 2.0) */
119 f *= 2.0;
120 e--;
121
122 if (e >= 16) {
123 goto Overflow;
124 }
125 else if (e < -25) {
126 /* |x| < 2**-25. Underflow to zero. */
127 f = 0.0;
128 e = 0;
129 }
130 else if (e < -14) {
131 /* |x| < 2**-14. Gradual underflow */
132 f = ldexp(f, 14 + e);
133 e = 0;
134 }
135 else /* if (!(e == 0 && f == 0.0)) */ {
136 e += 15;
137 f -= 1.0; /* Get rid of leading 1 */
138 }
139
140 f *= 1024.0; /* 2**10 */
141 /* Round to even */
142 bits = (unsigned short)f; /* Note the truncation */
143 assert(bits < 1024);
144 assert(e < 31);
145 if ((f - bits > 0.5) || ((f - bits == 0.5) && (bits % 2 == 1))) {
146 ++bits;
147 if (bits == 1024) {
148 /* The carry propagated out of a string of 10 1 bits. */
149 bits = 0;
150 ++e;
151 if (e == 31)
152 goto Overflow;
153 }
154 }
155 }
156
157 bits |= (e << 10) | (sign << 15);
158
159 /* Write out result. */
160 if (le) {
161 p += 1;
162 incr = -1;
163 }
164
165 /* First byte */
166 *p = (unsigned char)((bits >> 8) & 0xFF);
167 p += incr;
168
169 /* Second byte */
170 *p = (unsigned char)(bits & 0xFF);
171
172 return 0;
173
174 Overflow:
175 PyErr_SetString(PyExc_OverflowError,
176 "float too large to pack with e format");
177 return -1;
178 }
179
180 int
181 PyFloat_Pack4(double x, char *data, int le)
182 {
183 unsigned char *p = (unsigned char *)data;
184 if (float_format == unknown_format) {
185 unsigned char sign;
186 int e;
187 double f;
188 unsigned int fbits;
189 int incr = 1;
190
191 if (le) {
192 p += 3;
193 incr = -1;
194 }
195
196 if (x < 0) {
197 sign = 1;
198 x = -x;
199 }
200 else
201 sign = 0;
202
203 f = frexp(x, &e);
204
205 /* Normalize f to be in the range [1.0, 2.0) */
206 if (0.5 <= f && f < 1.0) {
207 f *= 2.0;
208 e--;
209 }
210 else if (f == 0.0)
211 e = 0;
212 else {
213 PyErr_SetString(PyExc_SystemError,
214 "frexp() result out of range in PyFloat_Pack4");
215 return -1;
216 }
217
218 if (e >= 128)
219 goto Overflow;
220 else if (e < -126) {
221 /* Gradual underflow */
222 f = ldexp(f, 126 + e);
223 e = 0;
224 }
225 else if (!(e == 0 && f == 0.0)) {
226 e += 127;
227 f -= 1.0; /* Get rid of leading 1 */
228 }
229
230 f *= 8388608.0; /* 2**23 */
231 fbits = (unsigned int)(f + 0.5); /* Round */
232 assert(fbits <= 8388608);
233 if (fbits >> 23) {
234 /* The carry propagated out of a string of 23 1 bits. */
235 fbits = 0;
236 ++e;
237 if (e >= 255)
238 goto Overflow;
239 }
240
241 /* First byte */
242 *p = (sign << 7) | (e >> 1);
243 p += incr;
244
245 /* Second byte */
246 *p = (char) (((e & 1) << 7) | (fbits >> 16));
247 p += incr;
248
249 /* Third byte */
250 *p = (fbits >> 8) & 0xFF;
251 p += incr;
252
253 /* Fourth byte */
254 *p = fbits & 0xFF;
255
256 /* Done */
257 return 0;
258
259 }
260 else {
261 float y = (float)x;
262 int i, incr = 1;
263
264 if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
265 goto Overflow;
266
267 unsigned char s[sizeof(float)];
268 memcpy(s, &y, sizeof(float));
269
270 if ((float_format == ieee_little_endian_format && !le)
271 || (float_format == ieee_big_endian_format && le)) {
272 p += 3;
273 incr = -1;
274 }
275
276 for (i = 0; i < 4; i++) {
277 *p = s[i];
278 p += incr;
279 }
280 return 0;
281 }
282 Overflow:
283 PyErr_SetString(PyExc_OverflowError,
284 "float too large to pack with f format");
285 return -1;
286 }
287
288 int
289 PyFloat_Pack8(double x, char *data, int le)
290 {
291 unsigned char *p = (unsigned char *)data;
292 if (double_format == unknown_format) {
293 unsigned char sign;
294 int e;
295 double f;
296 unsigned int fhi, flo;
297 int incr = 1;
298
299 if (le) {
300 p += 7;
301 incr = -1;
302 }
303
304 if (x < 0) {
305 sign = 1;
306 x = -x;
307 }
308 else
309 sign = 0;
310
311 f = frexp(x, &e);
312
313 /* Normalize f to be in the range [1.0, 2.0) */
314 if (0.5 <= f && f < 1.0) {
315 f *= 2.0;
316 e--;
317 }
318 else if (f == 0.0)
319 e = 0;
320 else {
321 PyErr_SetString(PyExc_SystemError,
322 "frexp() result out of range in PyFloat_Pack8");
323 return -1;
324 }
325
326 if (e >= 1024)
327 goto Overflow;
328 else if (e < -1022) {
329 /* Gradual underflow */
330 f = ldexp(f, 1022 + e);
331 e = 0;
332 }
333 else if (!(e == 0 && f == 0.0)) {
334 e += 1023;
335 f -= 1.0; /* Get rid of leading 1 */
336 }
337
338 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
339 f *= 268435456.0; /* 2**28 */
340 fhi = (unsigned int)f; /* Truncate */
341 assert(fhi < 268435456);
342
343 f -= (double)fhi;
344 f *= 16777216.0; /* 2**24 */
345 flo = (unsigned int)(f + 0.5); /* Round */
346 assert(flo <= 16777216);
347 if (flo >> 24) {
348 /* The carry propagated out of a string of 24 1 bits. */
349 flo = 0;
350 ++fhi;
351 if (fhi >> 28) {
352 /* And it also propagated out of the next 28 bits. */
353 fhi = 0;
354 ++e;
355 if (e >= 2047)
356 goto Overflow;
357 }
358 }
359
360 /* First byte */
361 *p = (sign << 7) | (e >> 4);
362 p += incr;
363
364 /* Second byte */
365 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
366 p += incr;
367
368 /* Third byte */
369 *p = (fhi >> 16) & 0xFF;
370 p += incr;
371
372 /* Fourth byte */
373 *p = (fhi >> 8) & 0xFF;
374 p += incr;
375
376 /* Fifth byte */
377 *p = fhi & 0xFF;
378 p += incr;
379
380 /* Sixth byte */
381 *p = (flo >> 16) & 0xFF;
382 p += incr;
383
384 /* Seventh byte */
385 *p = (flo >> 8) & 0xFF;
386 p += incr;
387
388 /* Eighth byte */
389 *p = flo & 0xFF;
390 /* p += incr; */
391
392 /* Done */
393 return 0;
394
395 Overflow:
396 PyErr_SetString(PyExc_OverflowError,
397 "float too large to pack with d format");
398 return -1;
399 }
400 else {
401 const unsigned char *s = (unsigned char*)&x;
402 int i, incr = 1;
403
404 if ((double_format == ieee_little_endian_format && !le)
405 || (double_format == ieee_big_endian_format && le)) {
406 p += 7;
407 incr = -1;
408 }
409
410 for (i = 0; i < 8; i++) {
411 *p = *s++;
412 p += incr;
413 }
414 return 0;
415 }
416 }
417
418 double
419 PyFloat_Unpack2(const char *data, int le)
420 {
421 unsigned char *p = (unsigned char *)data;
422 unsigned char sign;
423 int e;
424 unsigned int f;
425 double x;
426 int incr = 1;
427
428 if (le) {
429 p += 1;
430 incr = -1;
431 }
432
433 /* First byte */
434 sign = (*p >> 7) & 1;
435 e = (*p & 0x7C) >> 2;
436 f = (*p & 0x03) << 8;
437 p += incr;
438
439 /* Second byte */
440 f |= *p;
441
442 if (e == 0x1f) {
443 #if _PY_SHORT_FLOAT_REPR == 0
444 if (f == 0) {
445 /* Infinity */
446 return sign ? -Py_HUGE_VAL : Py_HUGE_VAL;
447 }
448 else {
449 /* NaN */
450 return sign ? -Py_NAN : Py_NAN;
451 }
452 #else // _PY_SHORT_FLOAT_REPR == 1
453 if (f == 0) {
454 /* Infinity */
455 return _Py_dg_infinity(sign);
456 }
457 else {
458 /* NaN */
459 return _Py_dg_stdnan(sign);
460 }
461 #endif // _PY_SHORT_FLOAT_REPR == 1
462 }
463
464 x = (double)f / 1024.0;
465
466 if (e == 0) {
467 e = -14;
468 }
469 else {
470 x += 1.0;
471 e -= 15;
472 }
473 x = ldexp(x, e);
474
475 if (sign)
476 x = -x;
477
478 return x;
479 }
480
481 double
482 PyFloat_Unpack4(const char *data, int le)
483 {
484 unsigned char *p = (unsigned char *)data;
485 if (float_format == unknown_format) {
486 unsigned char sign;
487 int e;
488 unsigned int f;
489 double x;
490 int incr = 1;
491
492 if (le) {
493 p += 3;
494 incr = -1;
495 }
496
497 /* First byte */
498 sign = (*p >> 7) & 1;
499 e = (*p & 0x7F) << 1;
500 p += incr;
501
502 /* Second byte */
503 e |= (*p >> 7) & 1;
504 f = (*p & 0x7F) << 16;
505 p += incr;
506
507 if (e == 255) {
508 PyErr_SetString(
509 PyExc_ValueError,
510 "can't unpack IEEE 754 special value "
511 "on non-IEEE platform");
512 return -1;
513 }
514
515 /* Third byte */
516 f |= *p << 8;
517 p += incr;
518
519 /* Fourth byte */
520 f |= *p;
521
522 x = (double)f / 8388608.0;
523
524 /* XXX This sadly ignores Inf/NaN issues */
525 if (e == 0)
526 e = -126;
527 else {
528 x += 1.0;
529 e -= 127;
530 }
531 x = ldexp(x, e);
532
533 if (sign)
534 x = -x;
535
536 return x;
537 }
538 else {
539 float x;
540
541 if ((float_format == ieee_little_endian_format && !le)
542 || (float_format == ieee_big_endian_format && le)) {
543 char buf[4];
544 char *d = &buf[3];
545 int i;
546
547 for (i = 0; i < 4; i++) {
548 *d-- = *p++;
549 }
550 memcpy(&x, buf, 4);
551 }
552 else {
553 memcpy(&x, p, 4);
554 }
555
556 return x;
557 }
558 }
559
560 double
561 PyFloat_Unpack8(const char *data, int le)
562 {
563 unsigned char *p = (unsigned char *)data;
564 if (double_format == unknown_format) {
565 unsigned char sign;
566 int e;
567 unsigned int fhi, flo;
568 double x;
569 int incr = 1;
570
571 if (le) {
572 p += 7;
573 incr = -1;
574 }
575
576 /* First byte */
577 sign = (*p >> 7) & 1;
578 e = (*p & 0x7F) << 4;
579
580 p += incr;
581
582 /* Second byte */
583 e |= (*p >> 4) & 0xF;
584 fhi = (*p & 0xF) << 24;
585 p += incr;
586
587 if (e == 2047) {
588 PyErr_SetString(
589 PyExc_ValueError,
590 "can't unpack IEEE 754 special value "
591 "on non-IEEE platform");
592 return -1.0;
593 }
594
595 /* Third byte */
596 fhi |= *p << 16;
597 p += incr;
598
599 /* Fourth byte */
600 fhi |= *p << 8;
601 p += incr;
602
603 /* Fifth byte */
604 fhi |= *p;
605 p += incr;
606
607 /* Sixth byte */
608 flo = *p << 16;
609 p += incr;
610
611 /* Seventh byte */
612 flo |= *p << 8;
613 p += incr;
614
615 /* Eighth byte */
616 flo |= *p;
617
618 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
619 x /= 268435456.0; /* 2**28 */
620
621 if (e == 0)
622 e = -1022;
623 else {
624 x += 1.0;
625 e -= 1023;
626 }
627 x = ldexp(x, e);
628
629 if (sign)
630 x = -x;
631
632 return x;
633 }
634 else {
635 double x;
636
637 if ((double_format == ieee_little_endian_format && !le)
638 || (double_format == ieee_big_endian_format && le)) {
639 char buf[8];
640 char *d = &buf[7];
641 int i;
642
643 for (i = 0; i < 8; i++) {
644 *d-- = *p++;
645 }
646 memcpy(&x, buf, 8);
647 }
648 else {
649 memcpy(&x, p, 8);
650 }
651
652 return x;
653 }
654 }