[EMAIL PROTECTED] wrote:
Max,

Have you seen the white paper "The NTP Era and Era Numbering" on the
NTP project page at www.ntp.org
http://www.eecis.udel.edu/~mills/y2k.html? It includes a 128-bit date
format, which  has enough seconds bits to measure the age of the
Universe until the Sun grows dim and enough fraction bits to measure
the time a photon crosses an atomic nucleus. The mapping to and from
this format and NTP timestamps is transparent, even when the NTP era
rolls in 2036, 2172, ...

I just started reading that paper, and I've found a bug/typo:

The era number is the integer quotient 2^32 / s and the timstamp is
the integer s mod 2^32.

The first operations should of course be s / 2^32, not the reciprocal!

BTW, it is only when working with unsigned integers that the C standard guarantees that subtraction of two values will behave as required by NTP, in your paper you implicitly acknowledge this by stating the need for twos-complement arithmetic. :-)

It seems to me that for absolute maximum portability, the subtractions could be done on unsigned values, then the result inspected:

If the high (sign) bit is set, negate the 64-bit result manually (using twos-complement rules) and remember the sign bit. Next convert the (small) positive value to double, then re-apply the correct sign bit:

double ntp_diff(uint32_t ah, uint32_t al, uint32_t bh, uint32_t bl)
/* Calculate A-B and return the result as a double: */
{
  uint32_t dl, dh;
  double d, sign;

  dl = al - bl;
  dh = ah - bh;
  if (dl > al) { /* Wrap-around, so borrow: */
    dh--;
  }

  sign = 1.0;
  if (dh & 0x80000000) { /* Negative difference? */
    sign = -1.0;
    /* Negate by inverting all bits... */
    dl = ~dl;
    dh = ~dh;
    /* ... and increment the result, taking care of any carries: */
    if (++dl == 0) {
      dh++;
      if (dh == 0x80000000) { /* Maximal negative value: -2^31 */
        return -65536.0*32768.0;
      }
    }
  }
  d = (double) dl;
  d *= 1.0/(65536.0*65536.0); /* Shift the fractional bits down */
  d += (double) dh;

  d *= sign;

  return d;
}

(I believe that this is probably one of the smaller problems you'd have if/when porting NTP to something like a 60-bit sign-magnitude machine. :-)

Terje
--
- <[EMAIL PROTECTED]>
"almost all programming can be viewed as an exercise in caching"

_______________________________________________
questions mailing list
[email protected]
https://lists.ntp.isc.org/mailman/listinfo/questions

Reply via email to