summaryrefslogtreecommitdiff
path: root/tune/time.c
blob: 5ba482ba12b0827468a29aa0928af4b87ee0f8c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
/* Time routines for speed measurements.

Copyright 1999-2004, 2010-2012 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */


/* Usage:

   The code in this file implements the lowest level of time measuring,
   simple one-time measuring of time between two points.

   void speed_starttime (void)
   double speed_endtime (void)
       Call speed_starttime to start measuring, and then call speed_endtime
       when done.

       speed_endtime returns the time taken, in seconds.  Or if the timebase
       is in CPU cycles and the CPU frequency is unknown then speed_endtime
       returns cycles.  Applications can identify the cycles return by
       checking for speed_cycletime (described below) equal to 1.0.

       If some sort of temporary glitch occurs then speed_endtime returns
       0.0.  Currently this is for various cases where a negative time has
       occurred.  This unfortunately occurs with getrusage on some systems,
       and with the hppa cycle counter on hpux.

   double speed_cycletime
       The time in seconds for each CPU cycle.  For example on a 100 MHz CPU
       this would be 1.0e-8.

       If the CPU frequency is unknown, then speed_cycletime is either 0.0
       or 1.0.  It's 0.0 when speed_endtime is returning seconds, or it's
       1.0 when speed_endtime is returning cycles.

       It may be noted that "speed_endtime() / speed_cycletime" gives a
       measured time in cycles, irrespective of whether speed_endtime is
       returning cycles or seconds.  (Assuming cycles can be had, ie. it's
       either cycles already or the cpu frequency is known.  See also
       speed_cycletime_need_cycles below.)

   double speed_unittime
       The unit of time measurement accuracy for the timing method in use.
       This is in seconds or cycles, as per speed_endtime.

   char speed_time_string[]
       A null-terminated string describing the time method in use.

   void speed_time_init (void)
       Initialize time measuring.  speed_starttime() does this
       automatically, so it's only needed if an application wants to inspect
       the above global variables before making a measurement.

   int speed_precision
       The intended accuracy of time measurements.  speed_measure() in
       common.c for instance runs target routines with enough repetitions so
       it takes at least "speed_unittime * speed_precision" (this expression
       works for both cycles or seconds from speed_endtime).

       A program can provide an option so the user to set speed_precision.
       If speed_precision is zero when speed_time_init or speed_starttime
       first run then it gets a default based on the measuring method
       chosen.  (More precision for higher accuracy methods.)

   void speed_cycletime_need_seconds (void)
       Call this to demand that speed_endtime will return seconds, and not
       cycles.  If only cycles are available then an error is printed and
       the program exits.

   void speed_cycletime_need_cycles (void)
       Call this to demand that speed_cycletime is non-zero, so that
       "speed_endtime() / speed_cycletime" will give times in cycles.



   Notes:

   Various combinations of cycle counter, read_real_time(), getrusage(),
   gettimeofday() and times() can arise, according to which are available
   and their precision.


   Allowing speed_endtime() to return either seconds or cycles is only a
   slight complication and makes it possible for the speed program to do
   some sensible things without demanding the CPU frequency.  If seconds are
   being measured then it can always print seconds, and if cycles are being
   measured then it can always print them without needing to know how long
   they are.  Also the tune program doesn't care at all what the units are.

   GMP_CPU_FREQUENCY can always be set when the automated methods in freq.c
   fail.  This will be needed if times in seconds are wanted but a cycle
   counter is being used, or if times in cycles are wanted but getrusage or
   another seconds based timer is in use.

   If the measuring method uses a cycle counter but supplements it with
   getrusage or the like, then knowing the CPU frequency is mandatory since
   the code compares values from the two.


   Not done:

   Solaris gethrtime() seems no more than a slow way to access the Sparc V9
   cycle counter.  gethrvtime() seems to be relevant only to light weight
   processes, it doesn't for instance give nanosecond virtual time.  So
   neither of these are used.


   Bugs:

   getrusage_microseconds_p is fundamentally flawed, getrusage and
   gettimeofday can have resolutions other than clock ticks or microseconds,
   for instance IRIX 5 has a tick of 10 ms but a getrusage of 1 ms.


   Enhancements:

   The SGI hardware counter has 64 bits on some machines, which could be
   used when available.  But perhaps 32 bits is enough range, and then rely
   on the getrusage supplement.

   Maybe getrusage (or times) should be used as a supplement for any
   wall-clock measuring method.  Currently a wall clock with a good range
   (eg. a 64-bit cycle counter) is used without a supplement.

   On PowerPC the timebase registers could be used, but would have to do
   something to find out the speed.  On 6xx chips it's normally 1/4 bus
   speed, on 4xx chips it's either that or an external clock.  Measuring
   against gettimeofday might be ok.  */


#include "config.h"

#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for getenv() */

#if HAVE_FCNTL_H
#include <fcntl.h>  /* for open() */
#endif

#if HAVE_STDINT_H
#include <stdint.h> /* for uint64_t */
#endif

#if HAVE_UNISTD_H
#include <unistd.h> /* for sysconf() */
#endif

#include <sys/types.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>  /* for struct timeval */
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#if HAVE_SYS_MMAN_H
#include <sys/mman.h>      /* for mmap() */
#endif

#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>  /* for struct rusage */
#endif

#if HAVE_SYS_SYSSGI_H
#include <sys/syssgi.h>    /* for syssgi() */
#endif

#if HAVE_SYS_SYSTEMCFG_H
#include <sys/systemcfg.h> /* for RTC_POWER on AIX */
#endif

#if HAVE_SYS_TIMES_H
#include <sys/times.h>  /* for times() and struct tms */
#endif

#include "gmp-impl.h"

#include "speed.h"


/* strerror is only used for some stuff on newish systems, no need to have a
   proper replacement */
#if ! HAVE_STRERROR
#define strerror(n)  "<strerror not available>"
#endif


char    speed_time_string[256];
int     speed_precision = 0;
double  speed_unittime;
double  speed_cycletime = 0.0;


/* don't rely on "unsigned" to "double" conversion, it's broken in SunOS 4
   native cc */
#define M_2POWU   (((double) INT_MAX + 1.0) * 2.0)

#define M_2POW32  4294967296.0
#define M_2POW64  (M_2POW32 * M_2POW32)


/* Conditionals for the time functions available are done with normal C
   code, which is a lot easier than wildly nested preprocessor directives.

   The choice of what to use is partly made at run-time, according to
   whether the cycle counter works and the measured accuracy of getrusage
   and gettimeofday.

   A routine that's not available won't be getting called, but is an abort()
   to be sure it isn't called mistakenly.

   It can be assumed that if a function exists then its data type will, but
   if the function doesn't then the data type might or might not exist, so
   the type can't be used unconditionally.  The "struct_rusage" etc macros
   provide dummies when the respective function doesn't exist. */


#if HAVE_SPEED_CYCLECOUNTER
static const int have_cycles = HAVE_SPEED_CYCLECOUNTER;
#else
static const int have_cycles = 0;
#define speed_cyclecounter(p)  ASSERT_FAIL (speed_cyclecounter not available)
#endif

/* "stck" returns ticks since 1 Jan 1900 00:00 GMT, where each tick is 2^-12
   microseconds.  Same #ifdefs here as in longlong.h.  */
#if defined (__GNUC__) && ! defined (NO_ASM)                            \
  && (defined (__i370__) || defined (__s390__) || defined (__mvs__))
static const int  have_stck = 1;
static const int  use_stck = 1;  /* always use when available */
typedef uint64_t  stck_t; /* gcc for s390 is quite new, always has uint64_t */
#define STCK(timestamp)                 \
  do {                                  \
    asm ("stck %0" : "=Q" (timestamp)); \
  } while (0)
#else
static const int  have_stck = 0;
static const int  use_stck = 0;
typedef unsigned long  stck_t;   /* dummy */
#define STCK(timestamp)  ASSERT_FAIL (stck instruction not available)
#endif
#define STCK_PERIOD      (1.0 / 4096e6)   /* 2^-12 microseconds */

/* mftb
   Enhancement: On 64-bit chips mftb gives a 64-bit value, no need for mftbu
   and a loop (see powerpc64.asm).  */
#if HAVE_HOST_CPU_FAMILY_powerpc
static const int  have_mftb = 1;
#if defined (__GNUC__) && ! defined (NO_ASM)
#define MFTB(a)                         \
  do {                                  \
    unsigned  __h1, __l, __h2;          \
    do {                                \
      asm volatile ("mftbu %0\n"        \
		    "mftb  %1\n"        \
		    "mftbu %2"          \
		    : "=r" (__h1),      \
		      "=r" (__l),       \
		      "=r" (__h2));     \
    } while (__h1 != __h2);             \
    a[0] = __l;                         \
    a[1] = __h1;                        \
  } while (0)
#else
#define MFTB(a)   mftb_function (a)
#endif
#else /* ! powerpc */
static const int  have_mftb = 0;
#define MFTB(a)                         \
  do {                                  \
    a[0] = 0;                           \
    a[1] = 0;                           \
    ASSERT_FAIL (mftb not available);   \
  } while (0)
#endif

/* Unicos 10.X has syssgi(), but not mmap(). */
#if HAVE_SYSSGI && HAVE_MMAP
static const int  have_sgi = 1;
#else
static const int  have_sgi = 0;
#endif

#if HAVE_READ_REAL_TIME
static const int have_rrt = 1;
#else
static const int have_rrt = 0;
#define read_real_time(t,s)     ASSERT_FAIL (read_real_time not available)
#define time_base_to_time(t,s)  ASSERT_FAIL (time_base_to_time not available)
#define RTC_POWER     1
#define RTC_POWER_PC  2
#define timebasestruct_t   struct timebasestruct_dummy
struct timebasestruct_dummy {
  int             flag;
  unsigned int    tb_high;
  unsigned int    tb_low;
};
#endif

#if HAVE_CLOCK_GETTIME
static const int have_cgt = 1;
#define struct_timespec  struct timespec
#else
static const int have_cgt = 0;
#define struct_timespec       struct timespec_dummy
#define clock_gettime(id,ts)  (ASSERT_FAIL (clock_gettime not available), -1)
#define clock_getres(id,ts)   (ASSERT_FAIL (clock_getres not available), -1)
#endif

#if HAVE_GETRUSAGE
static const int have_grus = 1;
#define struct_rusage   struct rusage
#else
static const int have_grus = 0;
#define getrusage(n,ru)  ASSERT_FAIL (getrusage not available)
#define struct_rusage    struct rusage_dummy
#endif

#if HAVE_GETTIMEOFDAY
static const int have_gtod = 1;
#define struct_timeval   struct timeval
#else
static const int have_gtod = 0;
#define gettimeofday(tv,tz)  ASSERT_FAIL (gettimeofday not available)
#define struct_timeval   struct timeval_dummy
#endif

#if HAVE_TIMES
static const int have_times = 1;
#define struct_tms   struct tms
#else
static const int have_times = 0;
#define times(tms)   ASSERT_FAIL (times not available)
#define struct_tms   struct tms_dummy
#endif

struct tms_dummy {
  long  tms_utime;
};
struct timeval_dummy {
  long  tv_sec;
  long  tv_usec;
};
struct rusage_dummy {
  struct_timeval ru_utime;
};
struct timespec_dummy {
  long  tv_sec;
  long  tv_nsec;
};

static int  use_cycles;
static int  use_mftb;
static int  use_sgi;
static int  use_rrt;
static int  use_cgt;
static int  use_gtod;
static int  use_grus;
static int  use_times;
static int  use_tick_boundary;

static unsigned         start_cycles[2];
static stck_t           start_stck;
static unsigned         start_mftb[2];
static unsigned         start_sgi;
static timebasestruct_t start_rrt;
static struct_timespec  start_cgt;
static struct_rusage    start_grus;
static struct_timeval   start_gtod;
static struct_tms       start_times;

static double  cycles_limit = 1e100;
static double  mftb_unittime;
static double  sgi_unittime;
static double  cgt_unittime;
static double  grus_unittime;
static double  gtod_unittime;
static double  times_unittime;

/* for RTC_POWER format, ie. seconds and nanoseconds */
#define TIMEBASESTRUCT_SECS(t)  ((t)->tb_high + (t)->tb_low * 1e-9)


/* Return a string representing a time in seconds, nicely formatted.
   Eg. "10.25ms".  */
char *
unittime_string (double t)
{
  static char  buf[128];

  const char  *unit;
  int         prec;

  /* choose units and scale */
  if (t < 1e-6)
    t *= 1e9, unit = "ns";
  else if (t < 1e-3)
    t *= 1e6, unit = "us";
  else if (t < 1.0)
    t *= 1e3, unit = "ms";
  else
    unit = "s";

  /* want 4 significant figures */
  if (t < 1.0)
    prec = 4;
  else if (t < 10.0)
    prec = 3;
  else if (t < 100.0)
    prec = 2;
  else
    prec = 1;

  sprintf (buf, "%.*f%s", prec, t, unit);
  return buf;
}


static jmp_buf  cycles_works_buf;

static RETSIGTYPE
cycles_works_handler (int sig)
{
  longjmp (cycles_works_buf, 1);
}

int
cycles_works_p (void)
{
  static int  result = -1;

  if (result != -1)
    goto done;

  /* FIXME: On linux, the cycle counter is not saved and restored over
   * context switches, making it almost useless for precise cputime
   * measurements. When available, it's better to use clock_gettime,
   * which seems to have reasonable accuracy (tested on x86_32,
   * linux-2.6.26, glibc-2.7). However, there are also some linux
   * systems where clock_gettime is broken in one way or the other,
   * like CLOCK_PROCESS_CPUTIME_ID not implemented (easy case) or
   * kind-of implemented but broken (needs code to detect that), and
   * on those systems a wall-clock cycle counter is the least bad
   * fallback.
   *
   * So we need some code to disable the cycle counter on some but not
   * all linux systems. */
#ifdef SIGILL
  {
    RETSIGTYPE (*old_handler) (int);
    unsigned  cycles[2];

    old_handler = signal (SIGILL, cycles_works_handler);
    if (old_handler == SIG_ERR)
      {
	if (speed_option_verbose)
	  printf ("cycles_works_p(): SIGILL not supported, assuming speed_cyclecounter() works\n");
	goto yes;
      }
    if (setjmp (cycles_works_buf))
      {
	if (speed_option_verbose)
	  printf ("cycles_works_p(): SIGILL during speed_cyclecounter(), so doesn't work\n");
	result = 0;
	goto done;
      }
    speed_cyclecounter (cycles);
    signal (SIGILL, old_handler);
    if (speed_option_verbose)
      printf ("cycles_works_p(): speed_cyclecounter() works\n");
  }
#else

  if (speed_option_verbose)
    printf ("cycles_works_p(): SIGILL not defined, assuming speed_cyclecounter() works\n");
  goto yes;
#endif

 yes:
  result = 1;

 done:
  return result;
}


/* The number of clock ticks per second, but looking at sysconf rather than
   just CLK_TCK, where possible.  */
long
clk_tck (void)
{
  static long  result = -1L;
  if (result != -1L)
    return result;

#if HAVE_SYSCONF
  result = sysconf (_SC_CLK_TCK);
  if (result != -1L)
    {
      if (speed_option_verbose)
	printf ("sysconf(_SC_CLK_TCK) is %ld per second\n", result);
      return result;
    }

  fprintf (stderr,
	   "sysconf(_SC_CLK_TCK) not working, using CLK_TCK instead\n");
#endif

#ifdef CLK_TCK
  result = CLK_TCK;
  if (speed_option_verbose)
    printf ("CLK_TCK is %ld per second\n", result);
  return result;
#else
  fprintf (stderr, "CLK_TCK not defined, cannot continue\n");
  abort ();
#endif
}


/* If two times can be observed less than half a clock tick apart, then
   assume "get" is microsecond accurate.

   Two times only 1 microsecond apart are not believed, since some kernels
   take it upon themselves to ensure gettimeofday doesn't return the same
   value twice, for the benefit of applications using it for a timestamp.
   This is obviously very stupid given the speed of CPUs these days.

   Making "reps" many calls to noop_1() is designed to waste some CPU, with
   a view to getting measurements 2 microseconds (or more) apart.  "reps" is
   increased progressively until such a period is seen.

   The outer loop "attempts" are just to allow for any random nonsense or
   system load upsetting the measurements (ie. making two successive calls
   to "get" come out as a longer interval than normal).

   Bugs:

   The assumption that any interval less than a half tick implies
   microsecond resolution is obviously fairly rash, the true resolution
   could be anything between a microsecond and that half tick.  Perhaps
   something special would have to be done on a system where this is the
   case, since there's no obvious reliable way to detect it
   automatically.  */

#define MICROSECONDS_P(name, type, get, sec, usec)                      \
  {                                                                     \
    static int  result = -1;                                            \
    type      st, et;                                                   \
    long      dt, half_tick;                                            \
    unsigned  attempt, reps, i, j;                                      \
									\
    if (result != -1)                                                   \
      return result;                                                    \
									\
    result = 0;                                                         \
    half_tick = (1000000L / clk_tck ()) / 2;                            \
									\
    for (attempt = 0; attempt < 5; attempt++)                           \
      {                                                                 \
	reps = 0;                                                       \
	for (;;)                                                        \
	  {                                                             \
	    get (st);                                                   \
	    for (i = 0; i < reps; i++)                                  \
	      for (j = 0; j < 100; j++)                                 \
		noop_1 (CNST_LIMB(0));                                  \
	    get (et);                                                   \
									\
	    dt = (sec(et)-sec(st))*1000000L + usec(et)-usec(st);        \
									\
	    if (speed_option_verbose >= 2)                              \
	      printf ("%s attempt=%u, reps=%u, dt=%ld\n",               \
		      name, attempt, reps, dt);                         \
									\
	    if (dt >= 2)                                                \
	      break;                                                    \
									\
	    reps = (reps == 0 ? 1 : 2*reps);                            \
	    if (reps == 0)                                              \
	      break;  /* uint overflow, not normal */                   \
	  }                                                             \
									\
	if (dt < half_tick)                                             \
	  {                                                             \
	    result = 1;                                                 \
	    break;                                                      \
	  }                                                             \
      }                                                                 \
									\
    if (speed_option_verbose)                                           \
      {                                                                 \
	if (result)                                                     \
	  printf ("%s is microsecond accurate\n", name);                \
	else                                                            \
	  printf ("%s is only %s clock tick accurate\n",                \
		  name, unittime_string (1.0/clk_tck()));               \
      }                                                                 \
    return result;                                                      \
  }


int
gettimeofday_microseconds_p (void)
{
#define call_gettimeofday(t)   gettimeofday (&(t), NULL)
#define timeval_tv_sec(t)      ((t).tv_sec)
#define timeval_tv_usec(t)     ((t).tv_usec)
  MICROSECONDS_P ("gettimeofday", struct_timeval,
		  call_gettimeofday, timeval_tv_sec, timeval_tv_usec);
}

int
getrusage_microseconds_p (void)
{
#define call_getrusage(t)   getrusage (0, &(t))
#define rusage_tv_sec(t)    ((t).ru_utime.tv_sec)
#define rusage_tv_usec(t)   ((t).ru_utime.tv_usec)
  MICROSECONDS_P ("getrusage", struct_rusage,
		  call_getrusage, rusage_tv_sec, rusage_tv_usec);
}

/* Test whether getrusage goes backwards, return non-zero if it does
   (suggesting it's flawed).

   On a macintosh m68040-unknown-netbsd1.4.1 getrusage looks like it's
   microsecond accurate, but has been seen remaining unchanged after many
   microseconds have elapsed.  It also regularly goes backwards by 1000 to
   5000 usecs, this has been seen after between 500 and 4000 attempts taking
   perhaps 0.03 seconds.  We consider this too broken for good measuring.
   We used to have configure pretend getrusage didn't exist on this system,
   but a runtime test should be more reliable, since we imagine the problem
   is not confined to just this exact system tuple.  */

int
getrusage_backwards_p (void)
{
  static int result = -1;
  struct rusage  start, prev, next;
  long  d;
  int   i;

  if (result != -1)
    return result;

  getrusage (0, &start);
  memcpy (&next, &start, sizeof (next));

  result = 0;
  i = 0;
  for (;;)
    {
      memcpy (&prev, &next, sizeof (prev));
      getrusage (0, &next);

      if (next.ru_utime.tv_sec < prev.ru_utime.tv_sec
	  || (next.ru_utime.tv_sec == prev.ru_utime.tv_sec
	      && next.ru_utime.tv_usec < prev.ru_utime.tv_usec))
	{
	  if (speed_option_verbose)
	    printf ("getrusage went backwards (attempt %d: %ld.%06ld -> %ld.%06ld)\n",
		    i,
		    (long) prev.ru_utime.tv_sec, (long) prev.ru_utime.tv_usec,
		    (long) next.ru_utime.tv_sec, (long) next.ru_utime.tv_usec);
	  result = 1;
	  break;
	}

      /* minimum 1000 attempts, then stop after either 0.1 seconds or 50000
	 attempts, whichever comes first */
      d = 1000000 * (next.ru_utime.tv_sec - start.ru_utime.tv_sec)
	+ (next.ru_utime.tv_usec - start.ru_utime.tv_usec);
      i++;
      if (i > 50000 || (i > 1000 && d > 100000))
	break;
    }

  return result;
}

/* CLOCK_PROCESS_CPUTIME_ID looks like it's going to be in a future version
   of glibc (some time post 2.2).

   CLOCK_VIRTUAL is process time, available in BSD systems (though sometimes
   defined, but returning -1 for an error).  */

#ifdef CLOCK_PROCESS_CPUTIME_ID
# define CGT_ID        CLOCK_PROCESS_CPUTIME_ID
#else
# ifdef CLOCK_VIRTUAL
#  define CGT_ID       CLOCK_VIRTUAL
# endif
#endif
#ifdef CGT_ID
const int  have_cgt_id = 1;
#else
const int  have_cgt_id = 0;
# define CGT_ID       (ASSERT_FAIL (CGT_ID not determined), -1)
#endif

#define CGT_DELAY_COUNT 1000

int
cgt_works_p (void)
{
  static int  result = -1;
  struct_timespec  unit;

  if (! have_cgt)
    return 0;

  if (! have_cgt_id)
    {
      if (speed_option_verbose)
	printf ("clock_gettime don't know what ID to use\n");
      result = 0;
      return result;
    }

  if (result != -1)
    return result;

  /* trial run to see if it works */
  if (clock_gettime (CGT_ID, &unit) != 0)
    {
      if (speed_option_verbose)
	printf ("clock_gettime id=%d error: %s\n", CGT_ID, strerror (errno));
      result = 0;
      return result;
    }

  /* get the resolution */
  if (clock_getres (CGT_ID, &unit) != 0)
    {
      if (speed_option_verbose)
	printf ("clock_getres id=%d error: %s\n", CGT_ID, strerror (errno));
      result = 0;
      return result;
    }

  cgt_unittime = unit.tv_sec + unit.tv_nsec * 1e-9;
  if (speed_option_verbose)
    printf ("clock_gettime is %s accurate\n", unittime_string (cgt_unittime));

  if (cgt_unittime < 10e-9)
    {
      /* Do we believe this? */
      struct timespec start, end;
      static volatile int counter;
      double duration;
      if (clock_gettime (CGT_ID, &start))
	{
	  if (speed_option_verbose)
	    printf ("clock_gettime id=%d error: %s\n", CGT_ID, strerror (errno));
	  result = 0;
	  return result;
	}
      /* Loop of at least 1000 memory accesses, ought to take at
	 least 100 ns*/
      for (counter = 0; counter < CGT_DELAY_COUNT; counter++)
	;
      if (clock_gettime (CGT_ID, &end))
	{
	  if (speed_option_verbose)
	    printf ("clock_gettime id=%d error: %s\n", CGT_ID, strerror (errno));
	  result = 0;
	  return result;
	}
      duration = (end.tv_sec + end.tv_nsec * 1e-9
		  - start.tv_sec - start.tv_nsec * 1e-9);
      if (speed_option_verbose)
	printf ("delay loop of %d rounds took %s (according to clock_gettime)\n",
		CGT_DELAY_COUNT, unittime_string (duration));
      if (duration < 100e-9)
	{
	  if (speed_option_verbose)
	    printf ("clock_gettime id=%d not believable\n", CGT_ID);
	  result = 0;
	  return result;
	}
    }
  result = 1;
  return result;
}


static double
freq_measure_mftb_one (void)
{
#define call_gettimeofday(t)   gettimeofday (&(t), NULL)
#define timeval_tv_sec(t)      ((t).tv_sec)
#define timeval_tv_usec(t)     ((t).tv_usec)
  FREQ_MEASURE_ONE ("mftb", struct_timeval,
		    call_gettimeofday, MFTB,
		    timeval_tv_sec, timeval_tv_usec);
}


static jmp_buf  mftb_works_buf;

static RETSIGTYPE
mftb_works_handler (int sig)
{
  longjmp (mftb_works_buf, 1);
}

int
mftb_works_p (void)
{
  unsigned   a[2];
  RETSIGTYPE (*old_handler) (int);
  double     cycletime;

  /* suppress a warning about a[] unused */
  a[0] = 0;

  if (! have_mftb)
    return 0;

#ifdef SIGILL
  old_handler = signal (SIGILL, mftb_works_handler);
  if (old_handler == SIG_ERR)
    {
      if (speed_option_verbose)
	printf ("mftb_works_p(): SIGILL not supported, assuming mftb works\n");
      return 1;
    }
  if (setjmp (mftb_works_buf))
    {
      if (speed_option_verbose)
	printf ("mftb_works_p(): SIGILL during mftb, so doesn't work\n");
      return 0;
    }
  MFTB (a);
  signal (SIGILL, old_handler);
  if (speed_option_verbose)
    printf ("mftb_works_p(): mftb works\n");
#else

  if (speed_option_verbose)
    printf ("mftb_works_p(): SIGILL not defined, assuming mftb works\n");
#endif

#if ! HAVE_GETTIMEOFDAY
  if (speed_option_verbose)
    printf ("mftb_works_p(): no gettimeofday available to measure mftb\n");
  return 0;
#endif

  /* The time base is normally 1/4 of the bus speed on 6xx and 7xx chips, on
     other chips it can be driven from an external clock. */
  cycletime = freq_measure ("mftb", freq_measure_mftb_one);
  if (cycletime == -1.0)
    {
      if (speed_option_verbose)
	printf ("mftb_works_p(): cannot measure mftb period\n");
      return 0;
    }

  mftb_unittime = cycletime;
  return 1;
}


volatile unsigned  *sgi_addr;

int
sgi_works_p (void)
{
#if HAVE_SYSSGI && HAVE_MMAP
  static int  result = -1;

  size_t          pagesize, offset;
  __psunsigned_t  phys, physpage;
  void            *virtpage;
  unsigned        period_picoseconds;
  int             size, fd;

  if (result != -1)
    return result;

  phys = syssgi (SGI_QUERY_CYCLECNTR, &period_picoseconds);
  if (phys == (__psunsigned_t) -1)
    {
      /* ENODEV is the error when a counter is not available */
      if (speed_option_verbose)
	printf ("syssgi SGI_QUERY_CYCLECNTR error: %s\n", strerror (errno));
      result = 0;
      return result;
    }
  sgi_unittime = period_picoseconds * 1e-12;

  /* IRIX 5 doesn't have SGI_CYCLECNTR_SIZE, assume 32 bits in that case.
     Challenge/ONYX hardware has a 64 bit byte counter, but there seems no
     obvious way to identify that without SGI_CYCLECNTR_SIZE.  */
#ifdef SGI_CYCLECNTR_SIZE
  size = syssgi (SGI_CYCLECNTR_SIZE);
  if (size == -1)
    {
      if (speed_option_verbose)
	{
	  printf ("syssgi SGI_CYCLECNTR_SIZE error: %s\n", strerror (errno));
	  printf ("    will assume size==4\n");
	}
      size = 32;
    }
#else
  size = 32;
#endif

  if (size < 32)
    {
      printf ("syssgi SGI_CYCLECNTR_SIZE gives %d, expected 32 or 64\n", size);
      result = 0;
      return result;
    }

  pagesize = getpagesize();
  offset = (size_t) phys & (pagesize-1);
  physpage = phys - offset;

  /* shouldn't cross over a page boundary */
  ASSERT_ALWAYS (offset + size/8 <= pagesize);

  fd = open("/dev/mmem", O_RDONLY);
  if (fd == -1)
    {
      if (speed_option_verbose)
	printf ("open /dev/mmem: %s\n", strerror (errno));
      result = 0;
      return result;
    }

  virtpage = mmap (0, pagesize, PROT_READ, MAP_PRIVATE, fd, (off_t) physpage);
  if (virtpage == (void *) -1)
    {
      if (speed_option_verbose)
	printf ("mmap /dev/mmem: %s\n", strerror (errno));
      result = 0;
      return result;
    }

  /* address of least significant 4 bytes, knowing mips is big endian */
  sgi_addr = (unsigned *) ((char *) virtpage + offset
			   + size/8 - sizeof(unsigned));
  result = 1;
  return result;

#else /* ! (HAVE_SYSSGI && HAVE_MMAP) */
  return 0;
#endif
}


#define DEFAULT(var,n)  \
  do {                  \
    if (! (var))        \
      (var) = (n);      \
  } while (0)

void
speed_time_init (void)
{
  double supplement_unittime = 0.0;

  static int  speed_time_initialized = 0;
  if (speed_time_initialized)
    return;
  speed_time_initialized = 1;

  speed_cycletime_init ();

  if (!speed_option_cycles_broken && have_cycles && cycles_works_p ())
    {
      use_cycles = 1;
      DEFAULT (speed_cycletime, 1.0);
      speed_unittime = speed_cycletime;
      DEFAULT (speed_precision, 10000);
      strcpy (speed_time_string, "CPU cycle counter");

      /* only used if a supplementary method is chosen below */
      cycles_limit = (have_cycles == 1 ? M_2POW32 : M_2POW64) / 2.0
	* speed_cycletime;

      if (have_grus && getrusage_microseconds_p() && ! getrusage_backwards_p())
	{
	  /* this is a good combination */
	  use_grus = 1;
	  supplement_unittime = grus_unittime = 1.0e-6;
	  strcpy (speed_time_string, "CPU cycle counter, supplemented by microsecond getrusage()");
	}
      else if (have_cycles == 1)
	{
	  /* When speed_cyclecounter has a limited range, look for something
	     to supplement it. */
	  if (have_gtod && gettimeofday_microseconds_p())
	    {
	      use_gtod = 1;
	      supplement_unittime = gtod_unittime = 1.0e-6;
	      strcpy (speed_time_string, "CPU cycle counter, supplemented by microsecond gettimeofday()");
	    }
	  else if (have_grus)
	    {
	      use_grus = 1;
	      supplement_unittime = grus_unittime = 1.0 / (double) clk_tck ();
	      sprintf (speed_time_string, "CPU cycle counter, supplemented by %s clock tick getrusage()", unittime_string (supplement_unittime));
	    }
	  else if (have_times)
	    {
	      use_times = 1;
	      supplement_unittime = times_unittime = 1.0 / (double) clk_tck ();
	      sprintf (speed_time_string, "CPU cycle counter, supplemented by %s clock tick times()", unittime_string (supplement_unittime));
	    }
	  else if (have_gtod)
	    {
	      use_gtod = 1;
	      supplement_unittime = gtod_unittime = 1.0 / (double) clk_tck ();
	      sprintf (speed_time_string, "CPU cycle counter, supplemented by %s clock tick gettimeofday()", unittime_string (supplement_unittime));
	    }
	  else
	    {
	      fprintf (stderr, "WARNING: cycle counter is 32 bits and there's no other functions.\n");
	      fprintf (stderr, "    Wraparounds may produce bad results on long measurements.\n");
	    }
	}

      if (use_grus || use_times || use_gtod)
	{
	  /* must know cycle period to compare cycles to other measuring
	     (via cycles_limit) */
	  speed_cycletime_need_seconds ();

	  if (speed_precision * supplement_unittime > cycles_limit)
	    {
	      fprintf (stderr, "WARNING: requested precision can't always be achieved due to limited range\n");
	      fprintf (stderr, "    cycle counter and limited precision supplemental method\n");
	      fprintf (stderr, "    (%s)\n", speed_time_string);
	    }
	}
    }
  else if (have_stck)
    {
      strcpy (speed_time_string, "STCK timestamp");
      /* stck is in units of 2^-12 microseconds, which is very likely higher
	 resolution than a cpu cycle */
      if (speed_cycletime == 0.0)
	speed_cycletime_fail
	  ("Need to know CPU frequency for effective stck unit");
      speed_unittime = MAX (speed_cycletime, STCK_PERIOD);
      DEFAULT (speed_precision, 10000);
    }
  else if (have_mftb && mftb_works_p ())
    {
      use_mftb = 1;
      DEFAULT (speed_precision, 10000);
      speed_unittime = mftb_unittime;
      sprintf (speed_time_string, "mftb counter (%s)",
	       unittime_string (speed_unittime));
    }
  else if (have_sgi && sgi_works_p ())
    {
      use_sgi = 1;
      DEFAULT (speed_precision, 10000);
      speed_unittime = sgi_unittime;
      sprintf (speed_time_string, "syssgi() mmap counter (%s), supplemented by millisecond getrusage()",
	       unittime_string (speed_unittime));
      /* supplemented with getrusage, which we assume to have 1ms resolution */
      use_grus = 1;
      supplement_unittime = 1e-3;
    }
  else if (have_rrt)
    {
      timebasestruct_t  t;
      use_rrt = 1;
      DEFAULT (speed_precision, 10000);
      read_real_time (&t, sizeof(t));
      switch (t.flag) {
      case RTC_POWER:
	/* FIXME: What's the actual RTC resolution? */
	speed_unittime = 1e-7;
	strcpy (speed_time_string, "read_real_time() power nanoseconds");
	break;
      case RTC_POWER_PC:
	t.tb_high = 1;
	t.tb_low = 0;
	time_base_to_time (&t, sizeof(t));
	speed_unittime = TIMEBASESTRUCT_SECS(&t) / M_2POW32;
	sprintf (speed_time_string, "%s read_real_time() powerpc ticks",
		 unittime_string (speed_unittime));
	break;
      default:
	fprintf (stderr, "ERROR: Unrecognised timebasestruct_t flag=%d\n",
		 t.flag);
	abort ();
      }
    }
  else if (have_cgt && cgt_works_p() && cgt_unittime < 1.5e-6)
    {
      /* use clock_gettime if microsecond or better resolution */
    choose_cgt:
      use_cgt = 1;
      speed_unittime = cgt_unittime;
      DEFAULT (speed_precision, (cgt_unittime <= 0.1e-6 ? 10000 : 1000));
      strcpy (speed_time_string, "microsecond accurate clock_gettime()");
    }
  else if (have_times && clk_tck() > 1000000)
    {
      /* Cray vector systems have times() which is clock cycle resolution
	 (eg. 450 MHz).  */
      DEFAULT (speed_precision, 10000);
      goto choose_times;
    }
  else if (have_grus && getrusage_microseconds_p() && ! getrusage_backwards_p())
    {
      use_grus = 1;
      speed_unittime = grus_unittime = 1.0e-6;
      DEFAULT (speed_precision, 1000);
      strcpy (speed_time_string, "microsecond accurate getrusage()");
    }
  else if (have_gtod && gettimeofday_microseconds_p())
    {
      use_gtod = 1;
      speed_unittime = gtod_unittime = 1.0e-6;
      DEFAULT (speed_precision, 1000);
      strcpy (speed_time_string, "microsecond accurate gettimeofday()");
    }
  else if (have_cgt && cgt_works_p() && cgt_unittime < 1.5/clk_tck())
    {
      /* use clock_gettime if 1 tick or better resolution */
      goto choose_cgt;
    }
  else if (have_times)
    {
      use_tick_boundary = 1;
      DEFAULT (speed_precision, 200);
    choose_times:
      use_times = 1;
      speed_unittime = times_unittime = 1.0 / (double) clk_tck ();
      sprintf (speed_time_string, "%s clock tick times()",
	       unittime_string (speed_unittime));
    }
  else if (have_grus)
    {
      use_grus = 1;
      use_tick_boundary = 1;
      speed_unittime = grus_unittime = 1.0 / (double) clk_tck ();
      DEFAULT (speed_precision, 200);
      sprintf (speed_time_string, "%s clock tick getrusage()\n",
	       unittime_string (speed_unittime));
    }
  else if (have_gtod)
    {
      use_gtod = 1;
      use_tick_boundary = 1;
      speed_unittime = gtod_unittime = 1.0 / (double) clk_tck ();
      DEFAULT (speed_precision, 200);
      sprintf (speed_time_string, "%s clock tick gettimeofday()",
	       unittime_string (speed_unittime));
    }
  else
    {
      fprintf (stderr, "No time measuring method available\n");
      fprintf (stderr, "None of: speed_cyclecounter(), STCK(), getrusage(), gettimeofday(), times()\n");
      abort ();
    }

  if (speed_option_verbose)
    {
      printf ("speed_time_init: %s\n", speed_time_string);
      printf ("    speed_precision     %d\n", speed_precision);
      printf ("    speed_unittime      %.2g\n", speed_unittime);
      if (supplement_unittime)
	printf ("    supplement_unittime %.2g\n", supplement_unittime);
      printf ("    use_tick_boundary   %d\n", use_tick_boundary);
      if (have_cycles)
	printf ("    cycles_limit        %.2g seconds\n", cycles_limit);
    }
}



/* Burn up CPU until a clock tick boundary, for greater accuracy.  Set the
   corresponding "start_foo" appropriately too. */

void
grus_tick_boundary (void)
{
  struct_rusage  prev;
  getrusage (0, &prev);
  do {
    getrusage (0, &start_grus);
  } while (start_grus.ru_utime.tv_usec == prev.ru_utime.tv_usec);
}

void
gtod_tick_boundary (void)
{
  struct_timeval  prev;
  gettimeofday (&prev, NULL);
  do {
    gettimeofday (&start_gtod, NULL);
  } while (start_gtod.tv_usec == prev.tv_usec);
}

void
times_tick_boundary (void)
{
  struct_tms  prev;
  times (&prev);
  do
    times (&start_times);
  while (start_times.tms_utime == prev.tms_utime);
}


/* "have_" values are tested to let unused code go dead.  */

void
speed_starttime (void)
{
  speed_time_init ();

  if (have_grus && use_grus)
    {
      if (use_tick_boundary)
	grus_tick_boundary ();
      else
	getrusage (0, &start_grus);
    }

  if (have_gtod && use_gtod)
    {
      if (use_tick_boundary)
	gtod_tick_boundary ();
      else
	gettimeofday (&start_gtod, NULL);
    }

  if (have_times && use_times)
    {
      if (use_tick_boundary)
	times_tick_boundary ();
      else
	times (&start_times);
    }

  if (have_cgt && use_cgt)
    clock_gettime (CGT_ID, &start_cgt);

  if (have_rrt && use_rrt)
    read_real_time (&start_rrt, sizeof(start_rrt));

  if (have_sgi && use_sgi)
    start_sgi = *sgi_addr;

  if (have_mftb && use_mftb)
    MFTB (start_mftb);

  if (have_stck && use_stck)
    STCK (start_stck);

  /* Cycles sampled last for maximum accuracy. */
  if (have_cycles && use_cycles)
    speed_cyclecounter (start_cycles);
}


/* Calculate the difference between two cycle counter samples, as a "double"
   counter of cycles.

   The start and end values are allowed to cancel in integers in case the
   counter values are bigger than the 53 bits that normally fit in a double.

   This works even if speed_cyclecounter() puts a value bigger than 32-bits
   in the low word (the high word always gets a 2**32 multiplier though). */

double
speed_cyclecounter_diff (const unsigned end[2], const unsigned start[2])
{
  unsigned  d;
  double    t;

  if (have_cycles == 1)
    {
      t = (end[0] - start[0]);
    }
  else
    {
      d = end[0] - start[0];
      t = d - (d > end[0] ? M_2POWU : 0.0);
      t += (end[1] - start[1]) * M_2POW32;
    }
  return t;
}


double
speed_mftb_diff (const unsigned end[2], const unsigned start[2])
{
  unsigned  d;
  double    t;

  d = end[0] - start[0];
  t = (double) d - (d > end[0] ? M_2POW32 : 0.0);
  t += (end[1] - start[1]) * M_2POW32;
  return t;
}


/* Calculate the difference between "start" and "end" using fields "sec" and
   "psec", where each "psec" is a "punit" of a second.

   The seconds parts are allowed to cancel before being combined with the
   psec parts, in case a simple "sec+psec*punit" exceeds the precision of a
   double.

   Total time is only calculated in a "double" since an integer count of
   psecs might overflow.  2^32 microseconds is only a bit over an hour, or
   2^32 nanoseconds only about 4 seconds.

   The casts to "long" are for the benefit of timebasestruct_t, where the
   fields are only "unsigned int", but we want a signed difference.  */

#define DIFF_SECS_ROUTINE(sec, psec, punit)                     \
  {                                                             \
    long  sec_diff, psec_diff;                                  \
    sec_diff = (long) end->sec - (long) start->sec;             \
    psec_diff = (long) end->psec - (long) start->psec;          \
    return (double) sec_diff + punit * (double) psec_diff;      \
  }

double
timeval_diff_secs (const struct_timeval *end, const struct_timeval *start)
{
  DIFF_SECS_ROUTINE (tv_sec, tv_usec, 1e-6);
}

double
rusage_diff_secs (const struct_rusage *end, const struct_rusage *start)
{
  DIFF_SECS_ROUTINE (ru_utime.tv_sec, ru_utime.tv_usec, 1e-6);
}

double
timespec_diff_secs (const struct_timespec *end, const struct_timespec *start)
{
  DIFF_SECS_ROUTINE (tv_sec, tv_nsec, 1e-9);
}

/* This is for use after time_base_to_time, ie. for seconds and nanoseconds. */
double
timebasestruct_diff_secs (const timebasestruct_t *end,
			  const timebasestruct_t *start)
{
  DIFF_SECS_ROUTINE (tb_high, tb_low, 1e-9);
}


double
speed_endtime (void)
{
#define END_USE(name,value)                             \
  do {                                                  \
    if (speed_option_verbose >= 3)                      \
      printf ("speed_endtime(): used %s\n", name);      \
    result = value;                                     \
    goto done;                                          \
  } while (0)

#define END_ENOUGH(name,value)                                          \
  do {                                                                  \
    if (speed_option_verbose >= 3)                                      \
      printf ("speed_endtime(): %s gives enough precision\n", name);    \
    result = value;                                                     \
    goto done;                                                          \
  } while (0)

#define END_EXCEED(name,value)                                            \
  do {                                                                    \
    if (speed_option_verbose >= 3)                                        \
      printf ("speed_endtime(): cycle counter limit exceeded, used %s\n", \
	      name);                                                      \
    result = value;                                                       \
    goto done;                                                            \
  } while (0)

  unsigned          end_cycles[2];
  stck_t            end_stck;
  unsigned          end_mftb[2];
  unsigned          end_sgi;
  timebasestruct_t  end_rrt;
  struct_timespec   end_cgt;
  struct_timeval    end_gtod;
  struct_rusage     end_grus;
  struct_tms        end_times;
  double            t_gtod, t_grus, t_times, t_cgt;
  double            t_rrt, t_sgi, t_mftb, t_stck, t_cycles;
  double            result;

  /* Cycles sampled first for maximum accuracy.
     "have_" values tested to let unused code go dead.  */

  if (have_cycles && use_cycles)  speed_cyclecounter (end_cycles);
  if (have_stck   && use_stck)    STCK (end_stck);
  if (have_mftb   && use_mftb)    MFTB (end_mftb);
  if (have_sgi    && use_sgi)     end_sgi = *sgi_addr;
  if (have_rrt    && use_rrt)     read_real_time (&end_rrt, sizeof(end_rrt));
  if (have_cgt    && use_cgt)     clock_gettime (CGT_ID, &end_cgt);
  if (have_gtod   && use_gtod)    gettimeofday (&end_gtod, NULL);
  if (have_grus   && use_grus)    getrusage (0, &end_grus);
  if (have_times  && use_times)   times (&end_times);

  result = -1.0;

  if (speed_option_verbose >= 4)
    {
      printf ("speed_endtime():\n");
      if (use_cycles)
	printf ("   cycles  0x%X,0x%X -> 0x%X,0x%X\n",
		start_cycles[1], start_cycles[0],
		end_cycles[1], end_cycles[0]);

      if (use_stck)
	printf ("   stck  0x%lX -> 0x%lX\n", start_stck, end_stck);

      if (use_mftb)
	printf ("   mftb  0x%X,%08X -> 0x%X,%08X\n",
		start_mftb[1], start_mftb[0],
		end_mftb[1], end_mftb[0]);

      if (use_sgi)
	printf ("   sgi  0x%X -> 0x%X\n", start_sgi, end_sgi);

      if (use_rrt)
	printf ("   read_real_time  (%d)%u,%u -> (%d)%u,%u\n",
		start_rrt.flag, start_rrt.tb_high, start_rrt.tb_low,
		end_rrt.flag, end_rrt.tb_high, end_rrt.tb_low);

      if (use_cgt)
	printf ("   clock_gettime  %ld.%09ld -> %ld.%09ld\n",
		(long) start_cgt.tv_sec, (long) start_cgt.tv_nsec,
		(long) end_cgt.tv_sec, (long) end_cgt.tv_nsec);

      if (use_gtod)
	printf ("   gettimeofday  %ld.%06ld -> %ld.%06ld\n",
		(long) start_gtod.tv_sec, (long) start_gtod.tv_usec,
		(long) end_gtod.tv_sec, (long) end_gtod.tv_usec);

      if (use_grus)
	printf ("   getrusage  %ld.%06ld -> %ld.%06ld\n",
		(long) start_grus.ru_utime.tv_sec,
		(long) start_grus.ru_utime.tv_usec,
		(long) end_grus.ru_utime.tv_sec,
		(long) end_grus.ru_utime.tv_usec);

      if (use_times)
	printf ("   times  %ld -> %ld\n",
		start_times.tms_utime, end_times.tms_utime);
    }

  if (use_rrt)
    {
      time_base_to_time (&start_rrt, sizeof(start_rrt));
      time_base_to_time (&end_rrt, sizeof(end_rrt));
      t_rrt = timebasestruct_diff_secs (&end_rrt, &start_rrt);
      END_USE ("read_real_time()", t_rrt);
    }

  if (use_cgt)
    {
      t_cgt = timespec_diff_secs (&end_cgt, &start_cgt);
      END_USE ("clock_gettime()", t_cgt);
    }

  if (use_grus)
    {
      t_grus = rusage_diff_secs (&end_grus, &start_grus);

      /* Use getrusage() if the cycle counter limit would be exceeded, or if
	 it provides enough accuracy already. */
      if (use_cycles)
	{
	  if (t_grus >= speed_precision*grus_unittime)
	    END_ENOUGH ("getrusage()", t_grus);
	  if (t_grus >= cycles_limit)
	    END_EXCEED ("getrusage()", t_grus);
	}
    }

  if (use_times)
    {
      t_times = (end_times.tms_utime - start_times.tms_utime) * times_unittime;

      /* Use times() if the cycle counter limit would be exceeded, or if
	 it provides enough accuracy already. */
      if (use_cycles)
	{
	  if (t_times >= speed_precision*times_unittime)
	    END_ENOUGH ("times()", t_times);
	  if (t_times >= cycles_limit)
	    END_EXCEED ("times()", t_times);
	}
    }

  if (use_gtod)
    {
      t_gtod = timeval_diff_secs (&end_gtod, &start_gtod);

      /* Use gettimeofday() if it measured a value bigger than the cycle
	 counter can handle.  */
      if (use_cycles)
	{
	  if (t_gtod >= cycles_limit)
	    END_EXCEED ("gettimeofday()", t_gtod);
	}
    }

  if (use_mftb)
    {
      t_mftb = speed_mftb_diff (end_mftb, start_mftb) * mftb_unittime;
      END_USE ("mftb", t_mftb);
    }

  if (use_stck)
    {
      t_stck = (end_stck - start_stck) * STCK_PERIOD;
      END_USE ("stck", t_stck);
    }

  if (use_sgi)
    {
      t_sgi = (end_sgi - start_sgi) * sgi_unittime;
      END_USE ("SGI hardware counter", t_sgi);
    }

  if (use_cycles)
    {
      t_cycles = speed_cyclecounter_diff (end_cycles, start_cycles)
	* speed_cycletime;
      END_USE ("cycle counter", t_cycles);
    }

  if (use_grus && getrusage_microseconds_p())
    END_USE ("getrusage()", t_grus);

  if (use_gtod && gettimeofday_microseconds_p())
    END_USE ("gettimeofday()", t_gtod);

  if (use_times)  END_USE ("times()",        t_times);
  if (use_grus)   END_USE ("getrusage()",    t_grus);
  if (use_gtod)   END_USE ("gettimeofday()", t_gtod);

  fprintf (stderr, "speed_endtime(): oops, no time method available\n");
  abort ();

 done:
  if (result < 0.0)
    {
      if (speed_option_verbose >= 2)
	fprintf (stderr, "speed_endtime(): warning, treating negative time as zero: %.9f\n", result);
      result = 0.0;
    }
  return result;
}