Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1 | #include <sys/time.h> |
| 2 | #include <stdio.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 3 | #include "debug.h" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 4 | |
| 5 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 6 | unsigned long micro = 1; |
| 7 | |
| 8 | void myusec_delay(int time) |
| 9 | { |
| 10 | volatile unsigned long i; |
Uwe Hermann | 0b7afe6 | 2007-04-01 19:44:21 +0000 | [diff] [blame] | 11 | for (i = 0; i < time * micro; i++) ; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | void myusec_calibrate_delay() |
| 15 | { |
| 16 | int count = 1000; |
| 17 | unsigned long timeusec; |
| 18 | struct timeval start, end; |
| 19 | int ok = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 20 | |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 21 | printf("Calibrating delay loop... "); |
| 22 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 23 | while (!ok) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 24 | gettimeofday(&start, 0); |
| 25 | myusec_delay(count); |
| 26 | gettimeofday(&end, 0); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 27 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 28 | (end.tv_usec - start.tv_usec); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 29 | count *= 2; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 30 | if (timeusec < 1000000 / 4) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 31 | continue; |
| 32 | ok = 1; |
| 33 | } |
| 34 | |
| 35 | // compute one microsecond. That will be count / time |
| 36 | micro = count / timeusec; |
| 37 | |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 38 | printf_debug("%ldM loops per second. ", (unsigned long)micro); |
| 39 | printf("ok\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 40 | } |