plan 9 kernel history: overview | file list | diff list

1999/0421/port/tod.c (diff list | history)

1999/0227/sys/src/9/port/tod.c:31,411999/0421/sys/src/9/port/tod.c:31,41 (short | long | prev | next)
1999/0225    
//  constraints: 
// 
//	1) log2(1000000000<<s1) <= 63 
//	   or s1 <= 33 
//	2) accomodate 15 minutes of ticks without overflow 
//	   or log2(((1000000000<<s1)/f)*((15*60*f)>>s2)) <= 63 
//	   or log2(mult) + 12 + log2(f) - s2 <= 63 
//	   or log2(mult) + log2(f) - 51 <= s2 
1999/0421    
//	   or s1 <= 32 
//	2) accomodate 1 minute of ticks without overflow 
//	   or log2(((1000000000<<s1)/f)*((60*f)>>s2)) <= 63 
//	   or log2(mult) + 6 + log2(f) - s2 <= 63 
//	   or log2(mult) + log2(f) - 57 <= s2 
1999/0225    
// 
//  by definition 
// 
1999/0227/sys/src/9/port/tod.c:50,581999/0421/sys/src/9/port/tod.c:50,58
1999/0225    
// 
//  Combining 2) and 4) we get 
// 
//	5) log2(f) - s2 + log2(f) - 51 <= s2 
//	   or 2*log2(f) - 51 <= 2*s2 
//	   or log2(f) - 25 <= s2 
1999/0421    
//	5) log2(f) - s2 + log2(f) - 57 <= s2 
//	   or 2*log2(f) - 57 <= 2*s2 
//	   or log2(f) - 28 <= s2 
1999/0225    
// 
//  Combining 3) and 4) 
// 
1999/0227/sys/src/9/port/tod.c:74,911999/0421/sys/src/9/port/tod.c:74,91
1999/0225    
// 
//	for f = 267000000, log2(f) = 28 
// 
//		s2 = 3 
//		s1 = 23 
1999/0421    
//		s2 = 0 
//		s1 = 26 
1999/0225    
// 
//	for f = 2000000000, log2(f) = 31 
// 
//		s2 = 6 
//		s1 = 26 
1999/0421    
//		s2 = 3 
//		s1 = 29 
1999/0225    
// 
//	for f = 8000000000, log2(f) = 33 
// 
//		s2 = 8 
//		s1 = 28 
1999/0421    
//		s2 = 5 
//		s1 = 31 
1999/0219    
 
// frequency of the tod clock 
#define TODFREQ	1000000000LL 
1999/0227/sys/src/9/port/tod.c:97,1031999/0421/sys/src/9/port/tod.c:97,102
1999/0225    
	int	s1;		// time = ((ticks>>s2)*multiplier)>>(s1-s2) 
1999/0219    
	vlong	multiplier;	// ... 
1999/0225    
	int	s2;		// ... 
	vlong	maxdiff;	// max diff between ticks and last to avoid overflow 
1999/0219    
	vlong	hz;		// frequency of fast clock 
	vlong	last;		// last reading of fast clock 
	vlong	off;		// offset from epoch to last 
1999/0227/sys/src/9/port/tod.c:148,1631999/0421/sys/src/9/port/tod.c:147,161
1999/0219    
	ilock(&tod); 
	tod.hz = f; 
1999/0225    
	lf = log2(f); 
	tod.s2 = lf - 25; 
1999/0421    
	tod.s2 = lf - 28; 
1999/0225    
	if(tod.s2 < 0) 
		tod.s2 = 0; 
	tod.s1 = 2*lf - tod.s2 - 30; 
	if(tod.s1 < 0) 
		tod.s1 = 0; 
	if(tod.s1 > 33) 
		tod.s1 = 33; 
1999/0421    
	if(tod.s1 > 32) 
		tod.s1 = 32; 
1999/0225    
	tod.multiplier = (TODFREQ<<tod.s1)/f; 
	tod.maxdiff = 1LL<<(10 + lf); 
1999/0219    
	iunlock(&tod); 
} 
 
1999/0227/sys/src/9/port/tod.c:219,2291999/0421/sys/src/9/port/tod.c:217,225
1999/0225    
	x = ((diff>>tod.s2)*tod.multiplier)>>(tod.s1-tod.s2); 
1999/0219    
	x += tod.off; 
 
	// protect against overflows 
1999/0225    
	if(diff > tod.maxdiff){ 
1999/0219    
		tod.last = ticks; 
		tod.off = x; 
	} 
1999/0421    
	// protect against overflows (gettod is called at least once a second) 
	tod.last = ticks; 
	tod.off = x; 
1999/0219    
 
	/* time can't go backwards */ 
	if(x < tod.lasttime) 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)