[Git][NTPsec/ntpsec][master] 9 commits: ieee754io: remove now pointless, and broken, standalone shim.

Gary E. Miller gitlab at mg.gitlab.com
Wed Apr 5 02:58:08 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
f18a1134 by Gary E. Miller at 2017-04-04T17:32:53-07:00
ieee754io: remove now pointless, and broken, standalone shim.

No need with working tests.

- - - - -
00c6e914 by Gary E. Miller at 2017-04-04T17:35:27-07:00
ieee754io: remove ancient change history.

- - - - -
860bc33b by Gary E. Miller at 2017-04-04T17:38:55-07:00
ieee754io: remove unused code

- - - - -
9aaa580c by Gary E. Miller at 2017-04-04T17:47:37-07:00
ieee754io: code motion, remove redundant switch.  No fuctional change.

- - - - -
3885328e by Gary E. Miller at 2017-04-04T18:00:35-07:00
ieee754io: change some implicit casts to explicit.

- - - - -
c11f79eb by Gary E. Miller at 2017-04-04T18:11:17-07:00
ieee754io: boolification.

- - - - -
7d3453e2 by Gary E. Miller at 2017-04-04T18:32:50-07:00
ieee754io: the right type removes the need dor many casts.

- - - - -
b885ccf5 by Gary E. Miller at 2017-04-04T18:53:04-07:00
ieee754io.  remove a bunch of pointless else statements.

And cuddle up some braces.

- - - - -
067a7f0a by Gary E. Miller at 2017-04-04T19:57:01-07:00
tests/ieee754io: add float and double tests for l_fp LSB.

- - - - -


2 changed files:

- libparse/ieee754io.c
- tests/libparse/ieee754io.c


Changes:

=====================================
libparse/ieee754io.c
=====================================
--- a/libparse/ieee754io.c
+++ b/libparse/ieee754io.c
@@ -13,14 +13,7 @@
 #include "ntp_fp.h"
 #include "ieee754io.h"
 
-static unsigned char get_byte (unsigned char *, offsets_t, int *);
-#if defined(DEBUG) && defined(DEBUG_PARSELIB)
-static int put_ieee754 (unsigned char **bufpp, int size, l_fp *lfpp,
-                        offsets_t offsets);
-#endif
-#ifdef __UNUSED__
-static void put_byte (unsigned char *, offsets_t, int *, unsigned char);
-#endif
+static unsigned long get_byte (unsigned char *, offsets_t, int *);
 
 #ifdef DEBUG_PARSELIB
 
@@ -57,7 +50,7 @@ fmt_blong(
 
 static char *
 fmt_flt(
-	unsigned int sign,
+	bool sign,
 	unsigned long mh,
 	unsigned long ml,
 	unsigned long ch
@@ -96,7 +89,7 @@ fmt_hex(
 
 #endif
 
-static unsigned char
+static unsigned long
 get_byte(
 	 unsigned char *bufp,
 	 offsets_t offset,
@@ -114,20 +107,6 @@ get_byte(
   return val;
 }
 
-#ifdef __UNUSED__
-static void
-put_byte(
-	 unsigned char *bufp,
-	 offsets_t offsets,
-	 int *fieldindex,
-	 unsigned char val
-	 )
-{
-  *(bufp + offsets[*fieldindex]) = val;
-  (*fieldindex)++;
-}
-#endif
-
 /*
  * make conversions to and from external IEEE754 formats and internal
  * NTP FP format.
@@ -141,24 +120,34 @@ fetch_ieee754(
 	      )
 {
   unsigned char *bufp = *buffpp;
-  unsigned int sign;
+  bool sign;
   unsigned int bias;
   unsigned int maxexp;
   int mbits;
   unsigned long mantissa_low;
   unsigned long mantissa_high;
-  unsigned long characteristic;
-  long exponent;
-  unsigned int maxexp_lfp;  /* maximum exponent that fits in an l_fp */
+  unsigned long characteristic;    /* biased exponent */
+  long exponent;                   /* unbiased exponent */
+  unsigned int maxexp_lfp;         /* maximum exponent that fits in an l_fp */
+  int frac_offset;	           /* where the fraction starts */
 #ifdef DEBUG_PARSELIB
   int length;
 #endif
   unsigned char val;
-  int fieldindex = 0;
+  int fieldindex = 0;              /* index into bufp */
   
 
   *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:
@@ -169,6 +158,18 @@ fetch_ieee754(
       mbits  = 52;
       bias   = 1023;
       maxexp = 2047;
+      characteristic <<= 4;
+      /* grab lower characteristic bits */
+      characteristic  |= (val & 0xF0U) >> 4;
+
+      mantissa_high  = (val & 0x0FU) << 16;
+      mantissa_high |= get_byte(bufp, offsets, &fieldindex) << 8;
+      mantissa_high |= get_byte(bufp, offsets, &fieldindex);
+
+      mantissa_low   = get_byte(bufp, offsets, &fieldindex) << 24;
+      mantissa_low  |= get_byte(bufp, offsets, &fieldindex) << 16;
+      mantissa_low  |= get_byte(bufp, offsets, &fieldindex) << 8;
+      mantissa_low  |= get_byte(bufp, offsets, &fieldindex);
       break;
 
     case IEEE_SINGLE:
@@ -179,52 +180,23 @@ fetch_ieee754(
       mbits  = 23;
       bias   = 127;
       maxexp = 255;
-      break;
-
-    default:
-      return IEEE_BADCALL;
-    }
-  
-  val = get_byte(bufp, offsets, &fieldindex); /* fetch sign byte & first part of characteristic */
-  
-  sign     = (val & 0x80) != 0;
-  characteristic = (val & 0x7F);
-
-  val = get_byte(bufp, offsets, &fieldindex); /* fetch rest of characteristic and start of mantissa */
-  
-  switch (size)
-    {
-    case IEEE_SINGLE:
       characteristic <<= 1;
-      characteristic  |= (val & 0x80) != 0; /* grab last characteristic bit */
+      /* grab last characteristic bit from 2nd byte */
+      characteristic |= (val & 0x80) ? 1U : 0 ;
 
       mantissa_high  = 0;
 
-      mantissa_low   = (val &0x7F) << 16;
-      mantissa_low  |= (unsigned long)get_byte(bufp, offsets, &fieldindex) << 8;
+      mantissa_low   = (val & 0x7FU) << 16;
+      mantissa_low  |= get_byte(bufp, offsets, &fieldindex) << 8;
       mantissa_low  |= get_byte(bufp, offsets, &fieldindex);
       break;
-      
-    case IEEE_DOUBLE:
-      characteristic <<= 4;
-      characteristic  |= (val & 0xF0) >> 4; /* grab lower characteristic bits */
-
-      mantissa_high  = (val & 0x0F) << 16;
-      mantissa_high |= (unsigned long)get_byte(bufp, offsets, &fieldindex) << 8;
-      mantissa_high |= get_byte(bufp, offsets, &fieldindex);
 
-      mantissa_low   = (unsigned long)get_byte(bufp, offsets, &fieldindex) << 24;
-      mantissa_low  |= (unsigned long)get_byte(bufp, offsets, &fieldindex) << 16;
-      mantissa_low  |= (unsigned long)get_byte(bufp, offsets, &fieldindex) << 8;
-      mantissa_low  |= get_byte(bufp, offsets, &fieldindex);
-      break;
-      
     default:
       return IEEE_BADCALL;
     }
+
 #ifdef DEBUG_PARSELIB
-  if (debug > 4)
-  {
+  if (debug > 4) {
     double d;
     float f;
 
@@ -237,16 +209,14 @@ fetch_ieee754(
 	    *((unsigned char *)(&f)+i) = *(*buffpp + offsets[i]);
 	  }
 	d = f;
-      }
-    else
-      {
+    } else {
 	int i;
 
 	for (i = 0; i < length; i++)
 	  {
 	    *((unsigned char *)(&d)+i) = *(*buffpp + offsets[i]);
 	  }
-      }
+    }
     
     printf("fetchieee754: FP: %s -> %s -> %e(=%s)\n", fmt_hex(*buffpp, length),
 	   fmt_flt(sign, mantissa_high, mantissa_low, characteristic),
@@ -259,340 +229,94 @@ fetch_ieee754(
   /*
    * detect funny numbers
    */
-  if (characteristic == maxexp)
-    {
+  if (characteristic == maxexp) {
       /*
        * NaN or Infinity
        */
-      if (mantissa_low || mantissa_high)
-	{
+      if (mantissa_low || mantissa_high) {
 	  /*
 	   * NaN
 	   */
 	  return IEEE_NAN;
-	}
-      else
-	{
-	  /*
-	   * +Inf or -Inf
-	   */
-	  return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY;
-	}
-    }
-  else
-    {
+      }
       /*
-       * collect real numbers
+       * +Inf or -Inf
        */
+      return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY;
+  }
+  /*
+   * collect real numbers
+   */
 
+  /*
+   * check for overflows
+   */
+  exponent = (long int)characteristic - bias;
+
+  if (exponent > maxexp_lfp) {
       /*
-       * check for overflows
+       * sorry an l_fp only so long
+       * overflow only in respect to NTP-FP representation
        */
-      exponent = characteristic - bias;
-
-      if (exponent > maxexp_lfp)	/* sorry an l_fp only so long */
-	{
-	  /*
-	   * overflow only in respect to NTP-FP representation
-	   */
-	  return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW;
-	}
-      else
-	{
-	  int frac_offset;	/* where the fraction starts */
-	  
-	  frac_offset = mbits - exponent;
-
-	  if (characteristic == 0) 
-	    {
-	      /*
-	       * de-normalized or tiny number - fits only as 0
-	       */
-	      return IEEE_OK;
-	    }
-	  else
-	    {
-	      /*
-	       * adjust for implied 1
-	       */
-	      if (mbits > 31)
-		mantissa_high |= 1 << (mbits - 32);
-	      else
-		mantissa_low  |= 1 << mbits;
-
-	      /*
-	       * take mantissa apart - if only all machine would support
-	       * 64 bit operations 8-(
-	       */
-	      if (frac_offset > mbits)
-		{
-		  setlfpuint(*lfpp, 0); /* only fractional number */
-		  frac_offset -= mbits + 1; /* will now contain right shift count - 1*/
-		  if (mbits > 31)
-		    {
-		      uint32_t frac;
-		      frac   = mantissa_high << (63 - mbits);
-		      frac  |= mantissa_low  >> (mbits - 33);
-		      frac >>= frac_offset;
-		      setlfpfrac(*lfpp, frac);
-		    }
-		  else
-		    {
-		      setlfpfrac(*lfpp, mantissa_low >> frac_offset);
-		    }
-		}
-	      else
-		{
-		  if (frac_offset > 32)
-		    {
-		      /*
-		       * must split in high word
-		       */
-		      setlfpuint(*lfpp, mantissa_high >> (frac_offset - 32));
-		      setlfpfrac(*lfpp, ((mantissa_high & ((1 << (frac_offset - 32)) - 1)) << (64 - frac_offset)) | (mantissa_low  >> (frac_offset - 32)));
-		    }
-		  else
-		    {
-		      /*
-		       * must split in low word
-		       */
-			setlfpuint(*lfpp, (mantissa_high << (32 - frac_offset)) | (((mantissa_low >> frac_offset) & ((1 << (32 - frac_offset)) - 1))));
-		      /* coverity[large_shift] */
-		      setlfpfrac(*lfpp, (mantissa_low & ((1 << frac_offset) - 1)) << (32 - frac_offset));
-		    }
-		}
-	      
-	      /*
-	       * adjust for sign
-	       */
-	      if (sign)
-		{
-		  L_NEG(*lfpp);
-		}
-	      
-	      return IEEE_OK;
-	    }
-	}
-    }
-}
-  
-#if defined(DEBUG) && defined(DEBUG_PARSELIB)
-#include <stdlib.h>
-
-static int
-put_ieee754(
-	    unsigned char **bufpp,
-	    int size,
-	    l_fp *lfpp,
-	    offsets_t offsets
-	    )
-{
-  l_fp outlfp;
-#ifdef DEBUG_PARSELIB
-  unsigned int sign;
-  unsigned int bias;
-#endif
-/*unsigned int maxexp;*/
-  int mbits;
-  int msb;
-  unsigned long mantissa_low = 0;
-  unsigned long mantissa_high = 0;
-#ifdef DEBUG_PARSELIB
-  unsigned long characteristic = 0;
-  long exponent;
-#endif
-/*int length;*/
-  unsigned long mask;
+      return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW;
+  }
 
-  UNUSED_ARG(bufpp);
-  UNUSED_ARG(offsets);
-  
-  outlfp = *lfpp;
+  frac_offset = mbits - exponent;
 
-  switch (size)
-    {
-    case IEEE_DOUBLE:
-    /*length = 8;*/
-      mbits  = 52;
-#ifdef DEBUG_PARSELIB
-      bias   = 1023;
-#endif
-    /*maxexp = 2047;*/
-      break;
-
-    case IEEE_SINGLE:
-    /*length = 4;*/
-      mbits  = 23;
-#ifdef DEBUG_PARSELIB
-      bias   = 127;
-#endif
-    /*maxexp = 255;*/
-      break;
+  if (characteristic == 0) {
+      /*
+       * de-normalized or tiny number - fits only as 0
+       */
+      return IEEE_OK;
+  }
 
-    default:
-      return IEEE_BADCALL;
-    }
-  
   /*
-   * find sign
+   * adjust for implied 1
    */
-  if (L_ISNEG(outlfp))
-    {
-      L_NEG(outlfp);
-#ifdef DEBUG_PARSELIB
-      sign = 1;
-#endif
-    }
+  if (mbits > 31)
+    mantissa_high |= 1U << (mbits - 32);
   else
-    {
-#ifdef DEBUG_PARSELIB
-      sign = 0;
-#endif
-    }
+    mantissa_low  |= 1U << mbits;
 
-  if (outlfp == 0)
-    {
-#ifdef DEBUG_PARSELIB
-      exponent = mantissa_high = mantissa_low = 0; /* true zero */
-#endif
-    }
-  else
-    {
-      /*
-       * find number of significant integer bits
-       */
-      mask = 0x80000000;
-      if (lfpuint(outlfp))
-	{
-	  msb = 63;
-	  while (mask && ((lfpuint(outlfp) & mask) == 0))
-	    {
-	      mask >>= 1;
-	      msb--;
-	    }
-	}
-      else
-	{
-	  msb = 31;
-	  while (mask && ((lfpfrac(outlfp) & mask) == 0))
-	    {
-	      mask >>= 1;
-	      msb--;
-	    }
-	}
-  
-      switch (size)
-	{
-	case IEEE_SINGLE:
-	  mantissa_high = 0;
-	  if (msb >= 32)
-	    {
-	      mantissa_low  = (lfpuint(outlfp) & ((1 << (msb - 32)) - 1)) << (mbits - (msb - 32));
-	      mantissa_low |=  lfpfrac(outlfp) >> (mbits - (msb - 32));
-	    }
-	  else
-	    {
-	      mantissa_low  = (lfpfrac(outlfp) << (mbits - msb)) & ((1 << mbits) - 1);
-	    }
-	  break;
-	  
-	case IEEE_DOUBLE:
-	  if (msb >= 32)
-	    {
-	      mantissa_high  = (lfpuint(outlfp) << (mbits - msb)) & ((1 << (mbits - 32)) - 1);
-	      mantissa_high |=  lfpfrac(outlfp) >> (32 - (mbits - msb));
-	      mantissa_low   = (lfpuint(outlfp) & ((1 << (msb - mbits)) - 1)) << (32 - (msb - mbits));
-	      /* coverity[negative_shift] */
-	      mantissa_low  |=  lfpfrac(outlfp) >> (msb - mbits);
-	    }
-	  else
-	    {
-	      mantissa_high  = lfpfrac(outlfp) << (mbits - 32 - msb);
-	      mantissa_low   = lfpfrac(outlfp) << (mbits - 32);
-	    }
+  /*
+   * take mantissa apart - if only all machine would support
+   * 64 bit operations 8-(
+   */
+  if (frac_offset > mbits) {
+      setlfpuint(*lfpp, 0); /* only fractional number */
+      frac_offset -= mbits + 1; /* will now contain right shift count - 1*/
+      if (mbits > 31) {
+	  uint32_t frac;
+	  frac   = mantissa_high << (63 - mbits);
+	  frac  |= mantissa_low  >> (mbits - 33);
+	  frac >>= frac_offset;
+	  setlfpfrac(*lfpp, frac);
+      } else {
+	  setlfpfrac(*lfpp, mantissa_low >> frac_offset);
+      }
+  } else {
+      if (frac_offset > 32) {
+	  /*
+	   * must split in high word
+	   */
+	  setlfpuint(*lfpp, mantissa_high >> (frac_offset - 32));
+	  setlfpfrac(*lfpp, ((mantissa_high & ((1 << (frac_offset - 32)) - 1)) << (64 - frac_offset)) | (mantissa_low  >> (frac_offset - 32)));
+      } else {
+	  /*
+	   * must split in low word
+	   */
+	    setlfpuint(*lfpp, (mantissa_high << (32 - frac_offset)) | (((mantissa_low >> frac_offset) & ((1 << (32 - frac_offset)) - 1))));
+	  /* coverity[large_shift] */
+	  setlfpfrac(*lfpp, (mantissa_low & ((1 << frac_offset) - 1)) << (32 - frac_offset));
 	}
-
-#ifdef DEBUG_PARSELIB
-      exponent = msb - 32;
-      characteristic = exponent + bias;
-
-      if (debug > 4)
-	printf("FP: %s\n", fmt_flt(sign, mantissa_high, mantissa_low, characteristic));
-#endif
     }
-  return IEEE_OK;
-}
 
-int main(
-	 int argc,
-	 char **argv
-	 )
-{
-  static offsets_t native_off = { 0, 1, 2, 3, 4, 5, 6, 7 };
-  double f = 1.0;
-  double *f_p = &f;
-  l_fp fp;
-  
-  if (argc == 2)
-    {
-      if (sscanf(argv[1], "%lf", &f) != 1)
-	{
-	  printf("cannot convert %s to a float\n", argv[1]);
-	  return EXIT_FAILURE;
-	}
+    /*
+    * adjust for sign
+    */
+    if (sign) {
+      L_NEG(*lfpp);
     }
-  
-  printf("double: %s %s\n", fmt_blong(*(unsigned long *)&f, 32), fmt_blong(*(unsigned long *)((char *)(&f)+4), 32));
-  printf("fetch from %f = %d\n", f, fetch_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off));
-  printf("fp [%s %s] = %s\n", fmt_blong(lfpuint(fp), 32), fmt_blong(lfpfrac(fp), 32), mfptoa(lfpuint(fp), lfpfrac(fp), 15));
-  f_p = &f;
-  put_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off);
-  
-  return EXIT_SUCCESS;
-}
 
-#endif
-/*
- * History:
- *
- * ieee754io.c,v
- * Revision 4.12  2005/04/16 17:32:10  kardel
- * update copyright
- *
- * Revision 4.11  2004/11/14 15:29:41  kardel
- * support PPSAPI, upgrade Copyright to Berkeley style
- *
- * Revision 4.8  1999/02/21 12:17:36  kardel
- * 4.91f reconciliation
- *
- * Revision 4.7  1999/02/21 11:26:03  kardel
- * renamed index to fieldindex to avoid index() name clash
- *
- * Revision 4.6  1998/11/15 20:27:52  kardel
- * Release 4.0.73e13 reconciliation
- *
- * Revision 4.5  1998/08/16 19:01:51  kardel
- * debug information only compile for LIBDEBUG case
- *
- * Revision 4.4  1998/08/09 09:39:28  kardel
- * Release 4.0.73e2 reconciliation
- *
- * Revision 4.3  1998/06/13 11:56:19  kardel
- * disabled putbute() for the time being
- *
- * Revision 4.2  1998/06/12 15:16:58  kardel
- * ansi2knr compatibility
- *
- * Revision 4.1  1998/05/24 07:59:56  kardel
- * conditional debug support
- *
- * Revision 4.0  1998/04/10 19:46:29  kardel
- * Start 4.0 release version numbering
- *
- * Revision 1.1  1998/04/10 19:27:46  kardel
- * initial NTP VERSION 4 integration of PARSE with GPS166 binary support
- *
- * Revision 1.1  1997/10/06 21:05:45  kardel
- * new parse structure
- *
- */
+    return IEEE_OK;
+}


=====================================
tests/libparse/ieee754io.c
=====================================
--- a/tests/libparse/ieee754io.c
+++ b/tests/libparse/ieee754io.c
@@ -147,6 +147,19 @@ TEST(ieee754io, test_order32) {
         TEST_ASSERT_EQUAL_UINT64( (unsigned long)0xFFFFFFFF00FEFE00UL, fp );
 }
 
+TEST(ieee754io, test_small32) {
+        int ret;
+        /* small number, one LSB of l_fp */
+        unsigned char buf[4] = { 0x33, 255, 255, 255};
+        unsigned char *bp = &buf[0];
+        l_fp fp;
+
+        ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+        TEST_ASSERT( IEEE_OK == ret);
+        TEST_ASSERT_EQUAL_INT64( 1, fp );
+}
+
 
 TEST(ieee754io, test_zero64) {
         int ret;
@@ -283,10 +296,24 @@ TEST(ieee754io, test_order64) {
         TEST_ASSERT_EQUAL_UINT64( (unsigned long)0xF808101820283000UL, fp );
 }
 
+TEST(ieee754io, test_small64) {
+        int ret;
+        /* small number, one LSB of l_fp  */
+        unsigned char buf[8] = { 0x33, 255, 255, 255, 255, 255, 255, 255};
+        unsigned char *bp = &buf[0];
+        l_fp fp;
+
+        ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+
+        TEST_ASSERT( IEEE_OK == ret);
+        TEST_ASSERT_EQUAL_INT64( 1, fp );
+}
+
 TEST_GROUP_RUNNER(ieee754io) {
         RUN_TEST_CASE(ieee754io, test_zero32);
         RUN_TEST_CASE(ieee754io, test_one32);
         RUN_TEST_CASE(ieee754io, test_negone32);
+        RUN_TEST_CASE(ieee754io, test_small32);
         RUN_TEST_CASE(ieee754io, test_nan32);
         RUN_TEST_CASE(ieee754io, test_max32);
         RUN_TEST_CASE(ieee754io, test_order32);
@@ -294,6 +321,7 @@ TEST_GROUP_RUNNER(ieee754io) {
         RUN_TEST_CASE(ieee754io, test_zero64);
         RUN_TEST_CASE(ieee754io, test_one64);
         RUN_TEST_CASE(ieee754io, test_negone64);
+        RUN_TEST_CASE(ieee754io, test_small64);
         RUN_TEST_CASE(ieee754io, test_nan64);
         RUN_TEST_CASE(ieee754io, test_max64);
         RUN_TEST_CASE(ieee754io, test_order64);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/521c727023c67a28d21e251971e7d7dfbe320556...067a7f0ad079181d5b8b201d818b60f256e23dec
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170405/158a09ab/attachment.html>


More information about the vc mailing list