关于ntp协议中两个宏NTPFRAC(x)和USEC(x)的疑问
/* How to multiply by 4294.967296 quickly (and not quite exactly)
* without using floating point or greater than 32-bit integers.
* If you want to fix the last 12 microseconds of error, add in
* (2911*(x))>>28)
*/
#define NTPFRAC(x) ( 4294*(x) + ( (1981*(x))>>11 ) )
/* The reverse of the above, needed if we want to set our microsecond
* clock (via settimeofday) based on the incoming time in NTP format.
* Basically exact.
*/
#define USEC(x) ( ( (x) >> 12 ) - 759 * ( ( ( (x) >> 10 ) + 32768 ) >> 16 ) )
想写关于ntp的东西,在网上找了个程序,其中有两个宏定义NTPFRAC(x)和USEC(x)。有注释胆识还是不太明白,有没有高人指点。
[解决办法]
英文注释里不是写了吗?
#define NTPFRAC(x) ( 4294*(x) + ( (1981*(x))>>11 ) )
这个宏是用于计算x*4294.967296的非精确写法,这样就避免了浮点计算
#define USEC(x) ( ( (x) >> 12 ) - 759 * ( ( ( (x) >> 10 ) + 32768 ) >> 16 ) )
这个是上面过程的反过程,表示x/4294.967296