diff --git a/libparse/ieee754io.c b/libparse/ieee754io.c index c4bbbb1577c56497c5f5bf076c6f5de0f4ea2901..d6d56e7a010c6289d0d636b247ebf8b99639d1df 100644 --- a/libparse/ieee754io.c +++ b/libparse/ieee754io.c @@ -21,86 +21,82 @@ static uint64_t get_byte (unsigned char *, offsets_t, int *); static char * fmt_blong( - unsigned long val, - int cnt - ) + unsigned long val, + int cnt + ) { - char *buf, *s; - int i = cnt; - - val <<= 32 - cnt; - LIB_GETBUF(buf); - s = buf; - - while (i--) - { - if (val & 0x80000000) - { - *s++ = '1'; - } - else - { - *s++ = '0'; - } - val <<= 1; + char *buf, *s; + int i = cnt; + + val <<= 32 - cnt; + LIB_GETBUF(buf); + s = buf; + + while (i--) { + if (val & 0x80000000) { + *s++ = '1'; + } else { + *s++ = '0'; + } + val <<= 1; } - *s = '\0'; - return buf; + *s = '\0'; + return buf; } static char * fmt_flt( - bool sign, - uint64_t ml, - unsigned long ch, + bool sign, + uint64_t ml, + unsigned long ch, int length - ) + ) { - char *buf; + char *buf; - LIB_GETBUF(buf); + LIB_GETBUF(buf); if ( 8 == length ) { - snprintf(buf, LIB_BUFLENGTH, "%c %s %s %s", sign ? '-' : '+', - fmt_blong(ch, 11), - fmt_blong(ml >> 32, 20), - fmt_blong(ml & 0x0FFFFFFFFULL, 32)); + snprintf(buf, LIB_BUFLENGTH, "%c %s %s %s", sign ? '-' : '+', + fmt_blong(ch, 11), + fmt_blong(ml >> 32, 20), + fmt_blong(ml & 0x0FFFFFFFFULL, 32)); } else { - snprintf(buf, LIB_BUFLENGTH, "%c %s %s", sign ? '-' : '+', - fmt_blong(ch, 8), - fmt_blong(ml, 23)); + snprintf(buf, LIB_BUFLENGTH, "%c %s %s", sign ? '-' : '+', + fmt_blong(ch, 8), + fmt_blong(ml, 23)); } - return buf; + return buf; } static char * fmt_hex( - unsigned char *bufp, - int length - ) + unsigned char *bufp, + int length + ) { - char * buf; - char hex[4]; - int i; - - LIB_GETBUF(buf); - buf[0] = '\0'; - for (i = 0; i < length; i++) { - snprintf(hex, sizeof(hex), "%02x", bufp[i]); - strlcat(buf, hex, LIB_BUFLENGTH); - } - - return buf; + char * buf; + char hex[4]; + int i; + + LIB_GETBUF(buf); + buf[0] = '\0'; + for (i = 0; i < length; i++) { + snprintf(hex, sizeof(hex), "%02x", bufp[i]); + strlcat(buf, hex, LIB_BUFLENGTH); + } + + return buf; } #endif static uint64_t get_byte( - unsigned char *bufp, - offsets_t offset, - int *fieldindex - ) + unsigned char *bufp, + offsets_t offset, + int *fieldindex + ) { unsigned char val; @@ -115,157 +111,138 @@ get_byte( */ int fetch_ieee754( - unsigned char **buffpp, - int size, - l_fp *lfpp, - offsets_t offsets - ) + unsigned char **buffpp, + int size, + l_fp *lfpp, + offsets_t offsets + ) { - unsigned char *bufp = *buffpp; - bool sign; - unsigned int bias; /* bias 127 or 1023 */ - unsigned int maxexp; - int mbits; /* length of mantissa, 23 or 52 */ - uint64_t mantissa; /* mantissa, 23 or 52 bits used, +1 */ - unsigned long characteristic; /* biased exponent, 0 to 255 or 2047 */ - int exponent; /* unbiased exponent */ - unsigned int maxexp_lfp; /* maximum exponent that fits in an l_fp */ - unsigned char val; - int fieldindex = 0; /* index into bufp */ - int fudge; /* shift difference of l_fp and IEEE */ - int shift; /* amount to shift IEEE to get l_fp */ - - - *lfpp = 0; /* return zero for all errors: NAN, +INF, -INF, etc. */ - - /* fetch sign byte & first part of characteristic */ - val = get_byte(bufp, offsets, &fieldindex); - - sign = (val & 0x80) != 0; - characteristic = (val & 0x7F); - - /* fetch rest of characteristic and start of mantissa */ - val = get_byte(bufp, offsets, &fieldindex); - - switch (size) - { + unsigned char *bufp = *buffpp; + bool sign; + unsigned int bias; /* bias 127 or 1023 */ + unsigned int maxexp; + int mbits; /* length of mantissa, 23 or 52 */ + uint64_t mantissa; /* mantissa, 23 or 52 bits used, +1 */ + unsigned long characteristic; /* biased exponent, 0 to 255 or 2047 */ + int exponent; /* unbiased exponent */ + unsigned int maxexp_lfp; /* maximum exponent that fits in an l_fp */ + unsigned char val; + int fieldindex = 0; /* index into bufp */ + int fudge; /* shift difference of l_fp and IEEE */ + int shift; /* amount to shift IEEE to get l_fp */ + + + *lfpp = 0; /* return zero for all errors: NAN, +INF, -INF, etc. */ + + /* fetch sign byte & first part of characteristic */ + val = get_byte(bufp, offsets, &fieldindex); + + sign = (val & 0x80) != 0; + characteristic = (val & 0x7F); + + /* fetch rest of characteristic and start of mantissa */ + val = get_byte(bufp, offsets, &fieldindex); + + switch (size) { case IEEE_DOUBLE: - fudge = -20; - maxexp_lfp = 31; - mbits = 52; - bias = 1023; - maxexp = 2047; - characteristic <<= 4; - /* grab lower characteristic bits */ - characteristic |= (val & 0xF0U) >> 4; - - mantissa = (val & 0x0FULL) << 48; - mantissa |= get_byte(bufp, offsets, &fieldindex) << 40; - mantissa |= get_byte(bufp, offsets, &fieldindex) << 32; - - mantissa |= get_byte(bufp, offsets, &fieldindex) << 24; - mantissa |= get_byte(bufp, offsets, &fieldindex) << 16; - mantissa |= get_byte(bufp, offsets, &fieldindex) << 8; - mantissa |= get_byte(bufp, offsets, &fieldindex); - break; + fudge = -20; + maxexp_lfp = 31; + mbits = 52; + bias = 1023; + maxexp = 2047; + characteristic <<= 4; + /* grab lower characteristic bits */ + characteristic |= (val & 0xF0U) >> 4; + + mantissa = (val & 0x0FULL) << 48; + mantissa |= get_byte(bufp, offsets, &fieldindex) << 40; + mantissa |= get_byte(bufp, offsets, &fieldindex) << 32; + + mantissa |= get_byte(bufp, offsets, &fieldindex) << 24; + mantissa |= get_byte(bufp, offsets, &fieldindex) << 16; + mantissa |= get_byte(bufp, offsets, &fieldindex) << 8; + mantissa |= get_byte(bufp, offsets, &fieldindex); + break; case IEEE_SINGLE: - fudge = 9; - maxexp_lfp = 127; - mbits = 23; - bias = 127; - maxexp = 255; - characteristic <<= 1; - /* grab last characteristic bit from 2nd byte */ - characteristic |= (val & 0x80) ? 1U : 0 ; - - mantissa = (val & 0x7FU) << 16; - mantissa |= get_byte(bufp, offsets, &fieldindex) << 8; - mantissa |= get_byte(bufp, offsets, &fieldindex); - break; + fudge = 9; + maxexp_lfp = 127; + mbits = 23; + bias = 127; + maxexp = 255; + characteristic <<= 1; + /* grab last characteristic bit from 2nd byte */ + characteristic |= (val & 0x80) ? 1U : 0 ; + + mantissa = (val & 0x7FU) << 16; + mantissa |= get_byte(bufp, offsets, &fieldindex) << 8; + mantissa |= get_byte(bufp, offsets, &fieldindex); + break; default: - return IEEE_BADCALL; + return IEEE_BADCALL; } - exponent = (int)characteristic - (int)bias; + exponent = (int)characteristic - (int)bias; #ifdef DEBUG_PARSELIB - if ( 1 || debug > 4) { - int length = 8; - if ( IEEE_SINGLE == size ) { - length = 4; - } + if ( debug > 4) { + int length = 8; + if ( IEEE_SINGLE == size ) { + length = 4; + } - printf("\nfetchieee754: FP: %s -> %s\n", fmt_hex(*buffpp, length), - fmt_flt(sign, mantissa, characteristic, length)); - printf("fetchieee754: Exp: %d, mbits %d\n", exponent, mbits); - } + printf("\nfetchieee754: FP: %s -> %s\n", fmt_hex(*buffpp, length), + fmt_flt(sign, mantissa, characteristic, length)); + printf("fetchieee754: Exp: %d, mbits %d\n", exponent, mbits); + } #endif - *buffpp += fieldindex; - - /* - * detect funny numbers - */ - if (characteristic == maxexp) { - /* - * NaN or Infinity - */ - if (mantissa) { - /* - * NaN - */ - return IEEE_NAN; - } - /* - * +Inf or -Inf - */ - return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY; - } - /* - * collect real numbers - */ - - /* - * check for overflows - */ - - if (exponent > (int)maxexp_lfp) { - /* - * sorry an l_fp only so long - * overflow only in respect to NTP-FP representation - */ - return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW; - } + *buffpp += fieldindex; + + /* detect funny numbers */ + if (characteristic == maxexp) { + /* NaN or Infinity */ + if (mantissa) { + /* NaN */ + return IEEE_NAN; + } + /* +Inf or -Inf */ + return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY; + } + + /* check for overflows */ + if (exponent > (int)maxexp_lfp) { + /* + * sorry an l_fp only so long + * overflow only in respect to NTP-FP representation + */ + return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW; + } if (characteristic == 0) { - /* - * de-normalized or tiny number - fits only as 0 - */ - return IEEE_OK; + /* de-normalized or tiny number - fits only as 0 */ + return IEEE_OK; } - /* - * add in implied 1 - */ + /* build the real number */ + + /* add in implied 1 */ mantissa |= 1ULL << mbits; shift = exponent + fudge; if ( 0 == shift ) { - /* no shift */ - *lfpp = mantissa; + /* no shift */ + *lfpp = mantissa; } else if ( 0 > shift ) { - /* right shift */ - *lfpp = mantissa >> -shift; + /* right shift */ + *lfpp = mantissa >> -shift; } else { - /* left shift */ - *lfpp = mantissa << shift; + /* left shift */ + *lfpp = mantissa << shift; } - /* - * adjust for sign - */ + /* adjust for sign */ if (sign) { L_NEG(*lfpp); }