1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This program 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 of the License, or
7 (at your option) any later version.
9 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <libpspp/float-format.h>
25 #include <libpspp/assertion.h>
26 #include <libpspp/integer-format.h>
27 #include <libpspp/str.h>
32 /* Neutral intermediate representation for binary floating-point numbers. */
37 FINITE, /* Finite number (normalized or denormalized). */
38 INFINITE, /* Positive or negative infinity. */
39 NAN, /* Not a number. */
41 ZERO, /* Positive or negative zero. */
42 MISSING, /* System missing. */
43 LOWEST, /* LOWEST on e.g. missing values. */
44 HIGHEST, /* HIGHEST on e.g. missing values. */
45 RESERVED /* Special Vax representation. */
56 /* class == FINITE: The number has value "fraction *
57 2**exponent", considering bit 63 in fraction to be just
58 right of the decimal point.
60 class == NAN: The fraction holds the significand, with its
61 leftmost bit in bit 63, so that signaling and quiet NaN
62 values can be preserved.
64 Unused for other classes. */
69 static void extract_number (enum float_format, const void *, struct fp *);
70 static void assemble_number (enum float_format, struct fp *, void *);
72 static inline uint16_t get_uint16 (const void *);
73 static inline uint32_t get_uint32 (const void *);
74 static inline uint64_t get_uint64 (const void *);
76 static inline void put_uint16 (uint16_t, void *);
77 static inline void put_uint32 (uint32_t, void *);
78 static inline void put_uint64 (uint64_t, void *);
80 /* Converts SRC from format FROM to format TO, storing the
81 converted value into DST.
82 SRC and DST are permitted to arbitrarily overlap. */
84 float_convert (enum float_format from, const void *src,
85 enum float_format to, void *dst)
89 if ((from == FLOAT_IEEE_SINGLE_LE || from == FLOAT_IEEE_SINGLE_BE)
90 && (to == FLOAT_IEEE_SINGLE_LE || to == FLOAT_IEEE_SINGLE_BE))
91 put_uint32 (bswap_32 (get_uint32 (src)), dst);
92 else if ((from == FLOAT_IEEE_DOUBLE_LE || from == FLOAT_IEEE_DOUBLE_BE)
93 && (to == FLOAT_IEEE_DOUBLE_LE || to == FLOAT_IEEE_DOUBLE_BE))
94 put_uint64 (bswap_64 (get_uint64 (src)), dst);
98 extract_number (from, src, &fp);
99 assemble_number (to, &fp, dst);
105 memmove (dst, src, float_get_size (from));
109 /* Converts SRC from format FROM to a native double and returns
112 float_get_double (enum float_format from, const void *src)
115 float_convert (from, src, FLOAT_NATIVE_DOUBLE, &dst);
119 /* Returns the number of bytes in a number in the given
122 float_get_size (enum float_format format)
126 case FLOAT_IEEE_SINGLE_LE:
127 case FLOAT_IEEE_SINGLE_BE:
132 case FLOAT_IEEE_DOUBLE_LE:
133 case FLOAT_IEEE_DOUBLE_BE:
140 return sizeof (struct fp);
149 /* Attempts to identify the floating point format(s) in which the
150 LENGTH bytes in NUMBER represent the given EXPECTED_VALUE.
151 Returns the number of matches, which may be zero, one, or
152 greater than one. If a positive value is returned, then the
153 most likely candidate format (based on how common the formats
154 are in practice) is stored in *BEST_GUESS. */
156 float_identify (double expected_value, const void *number, size_t length,
157 enum float_format *best_guess)
159 /* Candidates for identification in order of decreasing
161 enum float_format candidates[] =
163 FLOAT_IEEE_SINGLE_LE,
164 FLOAT_IEEE_SINGLE_BE,
165 FLOAT_IEEE_DOUBLE_LE,
166 FLOAT_IEEE_DOUBLE_BE,
173 const size_t candidate_cnt = sizeof candidates / sizeof *candidates;
175 enum float_format *p;
179 for (p = candidates; p < candidates + candidate_cnt; p++)
180 if (float_get_size (*p) == length)
183 assert (sizeof tmp >= float_get_size (*p));
184 float_convert (FLOAT_NATIVE_DOUBLE, &expected_value, *p, tmp);
185 if (!memcmp (tmp, number, length) && match_cnt++ == 0)
191 /* Returns CNT bits in X starting from the given bit OFS. */
192 static inline uint64_t
193 get_bits (uint64_t x, int ofs, int cnt)
195 assert (ofs >= 0 && ofs < 64);
196 assert (cnt > 0 && cnt < 64);
197 assert (ofs + cnt <= 64);
198 return (x >> ofs) & ((UINT64_C(1) << cnt) - 1);
201 /* Returns the 16-bit unsigned integer at P,
202 which need not be aligned. */
203 static inline uint16_t
204 get_uint16 (const void *p)
207 memcpy (&x, p, sizeof x);
211 /* Returns the 32-bit unsigned integer at P,
212 which need not be aligned. */
213 static inline uint32_t
214 get_uint32 (const void *p)
217 memcpy (&x, p, sizeof x);
221 /* Returns the 64-bit unsigned integer at P,
222 which need not be aligned. */
223 static inline uint64_t
224 get_uint64 (const void *p)
227 memcpy (&x, p, sizeof x);
231 /* Stores 16-bit unsigned integer X at P,
232 which need not be aligned. */
234 put_uint16 (uint16_t x, void *p)
236 memcpy (p, &x, sizeof x);
239 /* Stores 32-bit unsigned integer X at P,
240 which need not be aligned. */
242 put_uint32 (uint32_t x, void *p)
244 memcpy (p, &x, sizeof x);
247 /* Stores 64-bit unsigned integer X at P,
248 which need not be aligned. */
250 put_uint64 (uint64_t x, void *p)
252 memcpy (p, &x, sizeof x);
255 /* Returns NATIVE converted to a form that, when stored in
256 memory, will be in little-endian byte order. */
257 static inline uint16_t
258 native_to_le16 (uint16_t native)
260 return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_16 (native);
263 /* Returns NATIVE converted to a form that, when stored in
264 memory, will be in big-endian byte order. */
265 static inline uint16_t
266 native_to_be16 (uint16_t native)
268 return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_16 (native);
271 /* Returns NATIVE converted to a form that, when stored in
272 memory, will be in VAX-endian byte order. */
273 static inline uint16_t
274 native_to_vax16 (uint16_t native)
276 return native_to_le16 (native);
279 /* Returns NATIVE converted to a form that, when stored in
280 memory, will be in little-endian byte order. */
281 static inline uint32_t
282 native_to_le32 (uint32_t native)
284 return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_32 (native);
287 /* Returns NATIVE converted to a form that, when stored in
288 memory, will be in big-endian byte order. */
289 static inline uint32_t
290 native_to_be32 (uint32_t native)
292 return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_32 (native);
295 /* Returns NATIVE converted to a form that, when stored in
296 memory, will be in VAX-endian byte order. */
297 static inline uint32_t
298 native_to_vax32 (uint32_t native)
300 return native_to_be32 (((native & 0xff00ff00) >> 8) |
301 ((native & 0x00ff00ff) << 8));
304 /* Returns NATIVE converted to a form that, when stored in
305 memory, will be in little-endian byte order. */
306 static inline uint64_t
307 native_to_le64 (uint64_t native)
309 return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_64 (native);
312 /* Returns NATIVE converted to a form that, when stored in
313 memory, will be in big-endian byte order. */
314 static inline uint64_t
315 native_to_be64 (uint64_t native)
317 return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_64 (native);
320 /* Returns NATIVE converted to a form that, when stored in
321 memory, will be in VAX-endian byte order. */
322 static inline uint64_t
323 native_to_vax64 (uint64_t native)
325 return native_to_be64 (((native & UINT64_C(0xff00ff0000000000)) >> 40) |
326 ((native & UINT64_C(0x00ff00ff00000000)) >> 24) |
327 ((native & UINT64_C(0x00000000ff00ff00)) << 24) |
328 ((native & UINT64_C(0x0000000000ff00ff)) << 40));
331 /* Given LE, obtained from memory in little-endian format,
332 returns its value. */
333 static inline uint16_t
334 le_to_native16 (uint16_t le)
336 return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_16 (le);
339 /* Given BE, obtained from memory in big-endian format, returns
341 static inline uint16_t
342 be_to_native16 (uint16_t be)
344 return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_16 (be);
347 /* Given VAX, obtained from memory in VAX-endian format, returns
349 static inline uint16_t
350 vax_to_native16 (uint16_t vax)
352 return le_to_native16 (vax);
355 /* Given LE, obtained from memory in little-endian format,
356 returns its value. */
357 static inline uint32_t
358 le_to_native32 (uint32_t le)
360 return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_32 (le);
363 /* Given BE, obtained from memory in big-endian format, returns
365 static inline uint32_t
366 be_to_native32 (uint32_t be)
368 return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_32 (be);
371 /* Given VAX, obtained from memory in VAX-endian format, returns
373 static inline uint32_t
374 vax_to_native32 (uint32_t vax)
376 uint32_t be = be_to_native32 (vax);
377 return ((be & 0xff00ff00) >> 8) | ((be & 0x00ff00ff) << 8);
380 /* Given LE, obtained from memory in little-endian format,
381 returns its value. */
382 static inline uint64_t
383 le_to_native64 (uint64_t le)
385 return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_64 (le);
388 /* Given BE, obtained from memory in big-endian format, returns
390 static inline uint64_t
391 be_to_native64 (uint64_t be)
393 return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_64 (be);
396 /* Given VAX, obtained from memory in VAX-endian format, returns
398 static inline uint64_t
399 vax_to_native64 (uint64_t vax)
401 uint64_t be = be_to_native64 (vax);
402 return (((be & UINT64_C(0xff00ff0000000000)) >> 40) |
403 ((be & UINT64_C(0x00ff00ff00000000)) >> 24) |
404 ((be & UINT64_C(0x00000000ff00ff00)) << 24) |
405 ((be & UINT64_C(0x0000000000ff00ff)) << 40));
408 static void extract_ieee (uint64_t, int exp_bits, int frac_bits, struct fp *);
409 static void extract_vax (uint64_t, int exp_bits, int frac_bits, struct fp *);
410 static void extract_z (uint64_t, int exp_bits, int frac_bits, struct fp *);
411 static void extract_hex (const char *, struct fp *);
413 /* Converts the number at BITS from format TYPE into neutral
416 extract_number (enum float_format type, const void *bits, struct fp *fp)
420 case FLOAT_IEEE_SINGLE_LE:
421 extract_ieee (le_to_native32 (get_uint32 (bits)), 8, 23, fp);
423 case FLOAT_IEEE_SINGLE_BE:
424 extract_ieee (be_to_native32 (get_uint32 (bits)), 8, 23, fp);
426 case FLOAT_IEEE_DOUBLE_LE:
427 extract_ieee (le_to_native64 (get_uint64 (bits)), 11, 52, fp);
429 case FLOAT_IEEE_DOUBLE_BE:
430 extract_ieee (be_to_native64 (get_uint64 (bits)), 11, 52, fp);
434 extract_vax (vax_to_native32 (get_uint32 (bits)), 8, 23, fp);
437 extract_vax (vax_to_native64 (get_uint64 (bits)), 8, 55, fp);
440 extract_vax (vax_to_native64 (get_uint64 (bits)), 11, 52, fp);
444 extract_z (be_to_native32 (get_uint32 (bits)), 7, 24, fp);
447 extract_z (be_to_native64 (get_uint64 (bits)), 7, 56, fp);
451 memcpy (fp, bits, sizeof *fp);
454 extract_hex (bits, fp);
458 assert (!(fp->class == FINITE && fp->fraction == 0));
461 /* Converts the IEEE format number in BITS, which has EXP_BITS of
462 exponent and FRAC_BITS of fraction, into neutral format at
465 extract_ieee (uint64_t bits, int exp_bits, int frac_bits, struct fp *fp)
467 const int bias = (1 << (exp_bits - 1)) - 1;
468 const uint64_t max_raw_frac = (UINT64_C(1) << frac_bits) - 1;
469 const int max_raw_exp = (1 << exp_bits) - 1;
471 const uint64_t raw_frac = get_bits (bits, 0, frac_bits);
472 const int raw_exp = get_bits (bits, frac_bits, exp_bits);
473 const bool raw_sign = get_bits (bits, frac_bits + exp_bits, 1);
475 if (raw_sign && raw_exp == max_raw_exp - 1 && raw_frac == max_raw_frac - 1)
477 else if (raw_exp == max_raw_exp - 1 && raw_frac == max_raw_frac)
478 fp->class = raw_sign ? MISSING : HIGHEST;
479 else if (raw_exp == max_raw_exp)
482 fp->class = INFINITE;
486 fp->fraction = raw_frac << (64 - frac_bits);
489 else if (raw_exp == 0)
494 fp->exponent = 1 - bias;
495 fp->fraction = raw_frac << (64 - frac_bits);
503 fp->exponent = raw_exp - bias + 1;
504 fp->fraction = (raw_frac << (64 - frac_bits - 1)) | (UINT64_C(1) << 63);
507 fp->sign = raw_sign ? NEGATIVE : POSITIVE;
510 /* Converts the VAX format number in BITS, which has EXP_BITS of
511 exponent and FRAC_BITS of fraction, into neutral format at
514 extract_vax (uint64_t bits, int exp_bits, int frac_bits, struct fp *fp)
516 const int bias = 1 << (exp_bits - 1);
517 const uint64_t max_raw_frac = (UINT64_C(1) << frac_bits) - 1;
518 const int max_raw_exp = (1 << exp_bits) - 1;
520 const uint64_t raw_frac = get_bits (bits, 0, frac_bits);
521 const int raw_exp = get_bits (bits, frac_bits, exp_bits);
522 const bool raw_sign = get_bits (bits, frac_bits + exp_bits, 1);
524 if (raw_sign && raw_exp == max_raw_exp && raw_frac == max_raw_frac - 1)
526 else if (raw_exp == max_raw_exp && raw_frac == max_raw_frac)
527 fp->class = raw_sign ? MISSING : HIGHEST;
528 else if (raw_exp == 0)
529 fp->class = raw_sign == 0 ? ZERO : RESERVED;
533 fp->fraction = (raw_frac << (64 - frac_bits - 1)) | (UINT64_C(1) << 63);
534 fp->exponent = raw_exp - bias;
537 fp->sign = raw_sign ? NEGATIVE : POSITIVE;
540 /* Converts the Z architecture format number in BITS, which has
541 EXP_BITS of exponent and FRAC_BITS of fraction, into neutral
544 extract_z (uint64_t bits, int exp_bits, int frac_bits, struct fp *fp)
546 const int bias = 1 << (exp_bits - 1);
547 const uint64_t max_raw_frac = (UINT64_C(1) << frac_bits) - 1;
548 const int max_raw_exp = (1 << exp_bits) - 1;
550 uint64_t raw_frac = get_bits (bits, 0, frac_bits);
551 int raw_exp = get_bits (bits, frac_bits, exp_bits);
552 int raw_sign = get_bits (bits, frac_bits + exp_bits, 1);
554 fp->sign = raw_sign ? NEGATIVE : POSITIVE;
555 if (raw_exp == max_raw_exp && raw_frac == max_raw_frac)
556 fp->class = raw_sign ? MISSING : HIGHEST;
557 else if (raw_sign && raw_exp == max_raw_exp && raw_frac == max_raw_frac - 1)
559 else if (raw_frac != 0)
562 fp->fraction = raw_frac << (64 - frac_bits);
563 fp->exponent = (raw_exp - bias) * 4;
569 /* Returns the integer value of hex digit C. */
573 const char s[] = "0123456789abcdef";
574 const char *cp = strchr (s, tolower ((unsigned char) c));
580 /* Parses a hexadecimal floating point number string at S (useful
581 for testing) into neutral format at FP. */
583 extract_hex (const char *s, struct fp *fp)
593 if (!strcmp (s, "Infinity"))
594 fp->class = INFINITE;
595 else if (!strcmp (s, "Missing"))
597 else if (!strcmp (s, "Lowest"))
599 else if (!strcmp (s, "Highest"))
601 else if (!strcmp (s, "Reserved"))
602 fp->class = RESERVED;
607 if (!memcmp (s, "NaN:", 4))
621 for (; isxdigit ((unsigned char) *s); s++)
624 uint64_t digit = hexit_value (*s);
625 fp->fraction += digit << offset;
629 if (fp->class == FINITE)
631 if (fp->fraction == 0)
636 fp->exponent += strtol (s + 1, &tail, 10);
643 static uint64_t assemble_ieee (struct fp *, int exp_bits, int frac_bits);
644 static uint64_t assemble_vax (struct fp *, int exp_bits, int frac_bits);
645 static uint64_t assemble_z (struct fp *, int exp_bits, int frac_bits);
646 static void assemble_hex (struct fp *, void *);
648 /* Converts the neutral format floating point number in FP into
649 format TYPE at NUMBER. May modify FP as part of the
652 assemble_number (enum float_format type, struct fp *fp, void *number)
656 case FLOAT_IEEE_SINGLE_LE:
657 put_uint32 (native_to_le32 (assemble_ieee (fp, 8, 23)), number);
659 case FLOAT_IEEE_SINGLE_BE:
660 put_uint32 (native_to_be32 (assemble_ieee (fp, 8, 23)), number);
662 case FLOAT_IEEE_DOUBLE_LE:
663 put_uint64 (native_to_le64 (assemble_ieee (fp, 11, 52)), number);
665 case FLOAT_IEEE_DOUBLE_BE:
666 put_uint64 (native_to_be64 (assemble_ieee (fp, 11, 52)), number);
670 put_uint32 (native_to_vax32 (assemble_vax (fp, 8, 23)), number);
673 put_uint64 (native_to_vax64 (assemble_vax (fp, 8, 55)), number);
676 put_uint64 (native_to_vax64 (assemble_vax (fp, 11, 52)), number);
680 put_uint64 (native_to_be32 (assemble_z (fp, 7, 24)), number);
683 put_uint64 (native_to_be64 (assemble_z (fp, 7, 56)), number);
687 memcpy (number, fp, sizeof *fp);
690 assemble_hex (fp, number);
695 /* Rounds off FP's fraction to FRAC_BITS bits of precision.
696 Halfway values are rounded to even. */
698 normalize_and_round_fp (struct fp *fp, int frac_bits)
700 assert (fp->class == FINITE);
701 assert (fp->fraction != 0);
703 /* Make sure that the leading fraction bit is 1. */
704 while (!(fp->fraction & (UINT64_C(1) << 63)))
712 uint64_t last_frac_bit = UINT64_C(1) << (64 - frac_bits);
713 uint64_t decision_bit = last_frac_bit >> 1;
714 if (fp->fraction & decision_bit
715 && (fp->fraction & (decision_bit - 1)
716 || fp->fraction & last_frac_bit))
718 fp->fraction += last_frac_bit;
719 if ((fp->fraction >> 63) == 0)
721 fp->fraction = UINT64_C(1) << 63;
726 /* Mask off all but FRAC_BITS high-order bits.
727 If we rounded up, then these bits no longer have
728 meaningful values. */
729 fp->fraction &= ~(last_frac_bit - 1);
733 /* Converts the neutral format floating point number in FP into
734 IEEE format with EXP_BITS exponent bits and FRAC_BITS fraction
735 bits, and returns the value. */
737 assemble_ieee (struct fp *fp, int exp_bits, int frac_bits)
739 const uint64_t max_raw_frac = (UINT64_C(1) << frac_bits) - 1;
741 const int bias = (1 << (exp_bits - 1)) - 1;
742 const int max_raw_exp = (1 << exp_bits) - 1;
743 const int min_norm_exp = 1 - bias;
744 const int min_denorm_exp = min_norm_exp - frac_bits;
745 const int max_norm_exp = max_raw_exp - 1 - bias;
751 raw_sign = fp->sign != POSITIVE;
756 normalize_and_round_fp (fp, frac_bits + 1);
757 if (fp->exponent - 1 > max_norm_exp)
759 /* Overflow to infinity. */
760 raw_exp = max_raw_exp;
763 else if (fp->exponent - 1 >= min_norm_exp)
766 raw_frac = (fp->fraction << 1) >> (64 - frac_bits);
767 raw_exp = (fp->exponent - 1) + bias;
769 else if (fp->exponent - 1 >= min_denorm_exp)
772 const int denorm_shift = min_norm_exp - fp->exponent;
773 raw_frac = (fp->fraction >> (64 - frac_bits)) >> denorm_shift;
778 /* Underflow to zero. */
786 raw_exp = max_raw_exp;
790 raw_frac = fp->fraction >> (64 - frac_bits);
793 raw_exp = max_raw_exp;
803 raw_exp = max_raw_exp - 1;
804 raw_frac = max_raw_frac;
809 raw_exp = max_raw_exp - 1;
810 raw_frac = max_raw_frac - 1;
815 raw_exp = max_raw_exp - 1;
816 raw_frac = max_raw_frac;
820 /* Convert to what processors commonly treat as signaling NAN. */
821 raw_frac = (UINT64_C(1) << frac_bits) - 1;
822 raw_exp = max_raw_exp;
829 return (((uint64_t) raw_sign << (frac_bits + exp_bits))
830 | ((uint64_t) raw_exp << frac_bits)
834 /* Converts the neutral format floating point number in FP into
835 VAX format with EXP_BITS exponent bits and FRAC_BITS fraction
836 bits, and returns the value. */
838 assemble_vax (struct fp *fp, int exp_bits, int frac_bits)
840 const int max_raw_exp = (1 << exp_bits) - 1;
841 const int bias = 1 << (exp_bits - 1);
842 const int min_finite_exp = 1 - bias;
843 const int max_finite_exp = max_raw_exp - bias;
844 const uint64_t max_raw_frac = (UINT64_C(1) << frac_bits) - 1;
850 raw_sign = fp->sign != POSITIVE;
855 normalize_and_round_fp (fp, frac_bits + 1);
856 if (fp->exponent > max_finite_exp)
858 /* Overflow to reserved operand. */
863 else if (fp->exponent >= min_finite_exp)
866 raw_frac = (fp->fraction << 1) >> (64 - frac_bits);
867 raw_exp = fp->exponent + bias;
871 /* Underflow to zero. */
894 raw_exp = max_finite_exp + bias;
895 raw_frac = max_raw_frac;
900 raw_exp = max_finite_exp + bias;
901 raw_frac = max_raw_frac - 1;
906 raw_exp = max_finite_exp + bias;
907 raw_frac = max_raw_frac;
914 return (((uint64_t) raw_sign << (frac_bits + exp_bits))
915 | ((uint64_t) raw_exp << frac_bits)
919 /* Shift right until the exponent is a multiple of 4.
920 Rounding is not needed, because none of the formats we support
921 has more than 53 bits of precision. That is, we never shift a
922 1-bit off the right end of the fraction. */
924 normalize_hex_fp (struct fp *fp)
926 while (fp->exponent % 4)
933 /* Converts the neutral format floating point number in FP into Z
934 architecture format with EXP_BITS exponent bits and FRAC_BITS
935 fraction bits, and returns the value. */
937 assemble_z (struct fp *fp, int exp_bits, int frac_bits)
939 const int max_raw_exp = (1 << exp_bits) - 1;
940 const int bias = 1 << (exp_bits - 1);
941 const int max_norm_exp = (max_raw_exp - bias) * 4;
942 const int min_norm_exp = -bias * 4;
943 const int min_denorm_exp = min_norm_exp - (frac_bits - 1);
945 const uint64_t max_raw_frac = (UINT64_C(1) << frac_bits) - 1;
951 raw_sign = fp->sign != POSITIVE;
956 normalize_and_round_fp (fp, frac_bits);
957 normalize_hex_fp (fp);
958 if (fp->exponent > max_norm_exp)
960 /* Overflow to largest value. */
961 raw_exp = max_raw_exp;
962 raw_frac = max_raw_frac;
964 else if (fp->exponent >= min_norm_exp)
967 raw_frac = fp->fraction >> (64 - frac_bits);
968 raw_exp = (fp->exponent / 4) + bias;
970 else if (fp->exponent >= min_denorm_exp)
973 const int denorm_shift = min_norm_exp - fp->exponent;
974 raw_frac = (fp->fraction >> (64 - frac_bits)) >> denorm_shift;
979 /* Underflow to zero. */
986 /* Overflow to largest value. */
987 raw_exp = max_raw_exp;
988 raw_frac = max_raw_frac;
994 /* Treat all of these as zero, because Z architecture
995 doesn't have any reserved values. */
1002 raw_exp = max_raw_exp;
1003 raw_frac = max_raw_frac;
1008 raw_exp = max_raw_exp;
1009 raw_frac = max_raw_frac - 1;
1014 raw_exp = max_raw_exp;
1015 raw_frac = max_raw_frac;
1022 return (((uint64_t) raw_sign << (frac_bits + exp_bits))
1023 | ((uint64_t) raw_exp << frac_bits)
1027 /* Converts the neutral format floating point number in FP into a
1028 null-terminated human-readable hex string in OUTPUT. */
1030 assemble_hex (struct fp *fp, void *output)
1035 if (fp->sign == NEGATIVE)
1041 normalize_and_round_fp (fp, 64);
1042 normalize_hex_fp (fp);
1043 assert (fp->fraction != 0);
1046 while (fp->fraction != 0)
1048 *s++ = (fp->fraction >> 60)["0123456789abcdef"];
1051 if (fp->exponent != 0)
1052 sprintf (s, "p%d", fp->exponent);
1056 strcpy (s, "Infinity");
1060 sprintf (s, "NaN:%016"PRIx64, fp->fraction);
1068 strcpy (buffer, "Missing");
1072 strcpy (buffer, "Lowest");
1076 strcpy (buffer, "Highest");
1080 strcpy (s, "Reserved");
1084 strncpy (output, buffer, float_get_size (FLOAT_HEX));