2 * /src/NTP/ntp-4/libntp/ieee754io.c,v 4.8 1999/02/21 12:17:36 kardel RELEASE_19990228_A
4 * $Created: Sun Jul 13 09:12:02 1997 $
6 * Copyright (C) 1997, 1998 by Frank Kardel
15 #include "ntp_stdlib.h"
17 #include "ieee754io.h"
19 static unsigned char get_byte P((unsigned char *, offsets_t, int *));
21 static void put_byte P((unsigned char *, offsets_t, int *, unsigned char));
26 #include "lib_strbuf.h"
68 sprintf(buf, "%c %s %s %s", sign ? '-' : '+',
85 for (i = 0; i < length; i++)
87 sprintf(buf+i*2, "%02x", bufp[i]);
103 val = *(bufp + offset[*fieldindex]);
106 printf("fetchieee754: getbyte(0x%08x, %d) = 0x%02x\n", (unsigned int)(bufp)+offset[*fieldindex], *fieldindex, val);
121 *(bufp + offsets[*fieldindex]) = val;
127 * make conversions to and from external IEEE754 formats and internal
132 unsigned char **buffpp,
138 unsigned char *bufp = *buffpp;
144 u_long mantissa_high;
145 u_long characteristic;
177 val = get_byte(bufp, offsets, &fieldindex); /* fetch sign byte & first part of characteristic */
179 sign = (val & 0x80) != 0;
180 characteristic = (val & 0x7F);
182 val = get_byte(bufp, offsets, &fieldindex); /* fetch rest of characteristic and start of mantissa */
187 characteristic <<= 1;
188 characteristic |= (val & 0x80) != 0; /* grab last characteristic bit */
192 mantissa_low = (val &0x7F) << 16;
193 mantissa_low |= get_byte(bufp, offsets, &fieldindex) << 8;
194 mantissa_low |= get_byte(bufp, offsets, &fieldindex);
198 characteristic <<= 4;
199 characteristic |= (val & 0xF0) >> 4; /* grab lower characteristic bits */
201 mantissa_high = (val & 0x0F) << 16;
202 mantissa_high |= get_byte(bufp, offsets, &fieldindex) << 8;
203 mantissa_high |= get_byte(bufp, offsets, &fieldindex);
205 mantissa_low = get_byte(bufp, offsets, &fieldindex) << 24;
206 mantissa_low |= get_byte(bufp, offsets, &fieldindex) << 16;
207 mantissa_low |= get_byte(bufp, offsets, &fieldindex) << 8;
208 mantissa_low |= get_byte(bufp, offsets, &fieldindex);
220 if (size == IEEE_SINGLE)
224 for (i = 0; i < length; i++)
226 *((unsigned char *)(&f)+i) = *(*buffpp + offsets[i]);
234 for (i = 0; i < length; i++)
236 *((unsigned char *)(&d)+i) = *(*buffpp + offsets[i]);
240 printf("fetchieee754: FP: %s -> %s -> %e(=%s)\n", fmt_hex(*buffpp, length),
241 fmt_flt(sign, mantissa_high, mantissa_low, characteristic),
242 d, fmt_hex((unsigned char *)&d, length));
246 *buffpp += fieldindex;
249 * detect funny numbers
251 if (characteristic == maxexp)
256 if (mantissa_low || mantissa_high)
268 return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY;
274 * collect real numbers
280 * check for overflows
282 exponent = characteristic - bias;
284 if (exponent > 31) /* sorry - hardcoded */
287 * overflow only in respect to NTP-FP representation
289 return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW;
293 int frac_offset; /* where the fraction starts */
295 frac_offset = mbits - exponent;
297 if (characteristic == 0)
300 * de-normalized or tiny number - fits only as 0
307 * adjust for implied 1
310 mantissa_high |= 1 << (mbits - 32);
312 mantissa_low |= 1 << mbits;
315 * take mantissa apart - if only all machine would support
316 * 64 bit operations 8-(
318 if (frac_offset > mbits)
320 lfpp->l_ui = 0; /* only fractional number */
321 frac_offset -= mbits + 1; /* will now contain right shift count - 1*/
324 lfpp->l_uf = mantissa_high << (63 - mbits);
325 lfpp->l_uf |= mantissa_low >> (mbits - 33);
326 lfpp->l_uf >>= frac_offset;
330 lfpp->l_uf = mantissa_low >> frac_offset;
335 if (frac_offset > 32)
338 * must split in high word
340 lfpp->l_ui = mantissa_high >> (frac_offset - 32);
341 lfpp->l_uf = (mantissa_high & ((1 << (frac_offset - 32)) - 1)) << (64 - frac_offset);
342 lfpp->l_uf |= mantissa_low >> (frac_offset - 32);
347 * must split in low word
349 lfpp->l_ui = mantissa_high << (32 - frac_offset);
350 lfpp->l_ui |= (mantissa_low >> frac_offset) & ((1 << (32 - frac_offset)) - 1);
351 lfpp->l_uf = (mantissa_low & ((1 << frac_offset) - 1)) << (32 - frac_offset);
371 unsigned char **bufpp,
382 /*unsigned int maxexp;*/
385 u_long mantissa_low = 0;
386 u_long mantissa_high = 0;
388 u_long characteristic = 0;
423 if (L_ISNEG(&outlfp))
437 if (L_ISZERO(&outlfp))
440 exponent = mantissa_high = mantissa_low = 0; /* true zero */
446 * find number of significant integer bits
452 while (mask && ((outlfp.l_ui & mask) == 0))
461 while (mask && ((outlfp.l_uf & mask) == 0))
474 mantissa_low = (outlfp.l_ui & ((1 << (msb - 32)) - 1)) << (mbits - (msb - 32));
475 mantissa_low |= outlfp.l_uf >> (mbits - (msb - 32));
479 mantissa_low = (outlfp.l_uf << (mbits - msb)) & ((1 << mbits) - 1);
486 mantissa_high = (outlfp.l_ui << (mbits - msb)) & ((1 << (mbits - 32)) - 1);
487 mantissa_high |= outlfp.l_uf >> (32 - (mbits - msb));
488 mantissa_low = (outlfp.l_ui & ((1 << (msb - mbits)) - 1)) << (32 - (msb - mbits));
489 mantissa_low |= outlfp.l_uf >> (msb - mbits);
493 mantissa_high = outlfp.l_uf << (mbits - 32 - msb);
494 mantissa_low = outlfp.l_uf << (mbits - 32);
500 characteristic = exponent + bias;
503 printf("FP: %s\n", fmt_flt(sign, mantissa_high, mantissa_low, characteristic));
510 #if defined(DEBUG) && defined(LIBDEBUG)
516 static offsets_t native_off = { 0, 1, 2, 3, 4, 5, 6, 7 };
523 if (sscanf(argv[1], "%lf", &f) != 1)
525 printf("cannot convert %s to a float\n", argv[1]);
530 printf("double: %s %s\n", fmt_blong(*(unsigned long *)&f, 32), fmt_blong(*(unsigned long *)((char *)(&f)+4), 32));
531 printf("fetch from %f = %d\n", f, fetch_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off));
532 printf("fp [%s %s] = %s\n", fmt_blong(fp.l_ui, 32), fmt_blong(fp.l_uf, 32), mfptoa(fp.l_ui, fp.l_uf, 15));
534 put_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off);
542 * Revision 4.8 1999/02/21 12:17:36 kardel
543 * 4.91f reconcilation
545 * Revision 4.7 1999/02/21 11:26:03 kardel
546 * renamed index to fieldindex to avoid index() name clash
548 * Revision 4.6 1998/11/15 20:27:52 kardel
549 * Release 4.0.73e13 reconcilation
551 * Revision 4.5 1998/08/16 19:01:51 kardel
552 * debug information only compile for LIBDEBUG case
554 * Revision 4.4 1998/08/09 09:39:28 kardel
555 * Release 4.0.73e2 reconcilation
557 * Revision 4.3 1998/06/13 11:56:19 kardel
558 * disabled putbute() for the time being
560 * Revision 4.2 1998/06/12 15:16:58 kardel
561 * ansi2knr compatibility
563 * Revision 4.1 1998/05/24 07:59:56 kardel
564 * conditional debug support
566 * Revision 4.0 1998/04/10 19:46:29 kardel
567 * Start 4.0 release version numbering
569 * Revision 1.1 1998/04/10 19:27:46 kardel
570 * initial NTP VERSION 4 integration of PARSE with GPS166 binary support
572 * Revision 1.1 1997/10/06 21:05:45 kardel
573 * new parse structure