-
Notifications
You must be signed in to change notification settings - Fork 7
/
sse_mathfun_test.c
executable file
·1361 lines (1227 loc) · 42.2 KB
/
sse_mathfun_test.c
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
/*
RJVB:
gccopt -DHAVE_VECLIB -DUSE_SSE_AUTO -o sse_mathfun_test sse_mathfun_test.c -framework Accelerate
results on a macbook with 1.83GHz Core 1 Duo (apple gcc 4.0.1)
command line: gcc -O2 -Wall -W -DHAVE_VECLIB -msse sse_mathfun_test.c -framework Accelerate
checking sines on [0*Pi, 1*Pi]
max deviation from sinf(x): 5.96046e-08 at 0.679296388013*Pi, max deviation from cephes_sin(x): 0
max deviation from cosf(x): 5.96046e-08 at 0.755605310477*Pi, max deviation from cephes_cos(x): 0
deviation of sin(x)^2+cos(x)^2-1: 1.78814e-07 (ref deviation is 1.19209e-07)
->> precision OK for the sin_ps / cos_ps / sincos_ps <<-
checking sines on [-1000*Pi, 1000*Pi]
max deviation from sinf(x): 5.96046e-08 at 1821.37678456*Pi, max deviation from cephes_sin(x): 0
max deviation from cosf(x): 5.96046e-08 at -999.820056289*Pi, max deviation from cephes_cos(x): 0
deviation of sin(x)^2+cos(x)^2-1: 1.78814e-07 (ref deviation is 1.19209e-07)
->> precision OK for the sin_ps / cos_ps / sincos_ps <<-
checking exp/log [-60, 60]
max (relative) deviation from expf(x): 1.18758e-07 at 45.0583610535, max deviation from cephes_expf(x): 0
max (absolute) deviation from logf(x): 1.19209e-07 at 1.66063511372, max deviation from cephes_logf(x): 0
deviation of x - log(exp(x)): 1.19209e-07 (ref deviation is 5.96046e-08)
->> precision OK for the exp_ps / log_ps <<-
exp([ -1000, -100, 100, 1000]) = [ 0, 0, 2.4061436e+38, 2.4061436e+38]
exp([ nan, inf, -inf, nan]) = [2.4061436e+38, 2.4061436e+38, 0, 2.4061436e+38]
log([ 0, -10, 1e+30, 1.0005271e-42]) = [ nan, nan, 69.077553, -87.336548]
log([ nan, inf, -inf, nan]) = [ -87.336548, 88.722839, nan, -87.336548]
sin([ nan, inf, -inf, nan]) = [ nan, nan, nan, nan]
cos([ nan, inf, -inf, nan]) = [ nan, nan, nan, nan]
sin([ -1e+30, -100000, 1e+30, 100000]) = [ inf, -0.035749275, -inf, 0.035749275]
cos([ -1e+30, -100000, 1e+30, 100000]) = [ nan, -0.9993608, nan, -0.9993608]
benching sinf .. -> 3.2 millions of vector evaluations/second -> 142 cycles/value on a 1830MHz computer
benching cosf .. -> 3.2 millions of vector evaluations/second -> 142 cycles/value on a 1830MHz computer
benching sincos (x87) .. -> 2.8 millions of vector evaluations/second -> 161 cycles/value on a 1830MHz computer
benching expf .. -> 3.0 millions of vector evaluations/second -> 148 cycles/value on a 1830MHz computer
benching logf .. -> 3.0 millions of vector evaluations/second -> 150 cycles/value on a 1830MHz computer
benching cephes_sinf .. -> 4.5 millions of vector evaluations/second -> 100 cycles/value on a 1830MHz computer
benching cephes_cosf .. -> 4.9 millions of vector evaluations/second -> 92 cycles/value on a 1830MHz computer
benching cephes_expf .. -> 3.0 millions of vector evaluations/second -> 151 cycles/value on a 1830MHz computer
benching cephes_logf .. -> 2.6 millions of vector evaluations/second -> 172 cycles/value on a 1830MHz computer
benching sin_ps .. -> 18.1 millions of vector evaluations/second -> 25 cycles/value on a 1830MHz computer
benching cos_ps .. -> 18.2 millions of vector evaluations/second -> 25 cycles/value on a 1830MHz computer
benching sincos_ps .. -> 15.0 millions of vector evaluations/second -> 30 cycles/value on a 1830MHz computer
benching exp_ps .. -> 17.6 millions of vector evaluations/second -> 26 cycles/value on a 1830MHz computer
benching log_ps .. -> 15.5 millions of vector evaluations/second -> 29 cycles/value on a 1830MHz computer
benching vsinf .. -> 14.3 millions of vector evaluations/second -> 32 cycles/value on a 1830MHz computer
benching vcosf .. -> 14.4 millions of vector evaluations/second -> 32 cycles/value on a 1830MHz computer
benching vexpf .. -> 12.0 millions of vector evaluations/second -> 38 cycles/value on a 1830MHz computer
benching vlogf .. -> 13.1 millions of vector evaluations/second -> 35 cycles/value on a 1830MHz computer
on a 2600MHz opteron running linux, with the 64 bits acml math vector lib (gcc 4.2):
command line: gcc-4.2 -msse -O3 -Wall -W sse_mathfun_test.c -lm -DHAVE_ACML -I /usr/local/acml3.6.0/pathscale64/include /usr/local/acml3.6.0/pathscale64/lib/libacml_mv.a
benching sinf .. -> 6.3 millions of vector evaluations/second -> 103 cycles/value on a 2600MHz computer
benching cosf .. -> 5.6 millions of vector evaluations/second -> 115 cycles/value on a 2600MHz computer
benching sincos (x87) .. -> 4.2 millions of vector evaluations/second -> 153 cycles/value on a 2600MHz computer
benching expf .. -> 1.1 millions of vector evaluations/second -> 546 cycles/value on a 2600MHz computer
benching logf .. -> 4.7 millions of vector evaluations/second -> 138 cycles/value on a 2600MHz computer
benching cephes_sinf .. -> 11.6 millions of vector evaluations/second -> 56 cycles/value on a 2600MHz computer
benching cephes_cosf .. -> 8.7 millions of vector evaluations/second -> 74 cycles/value on a 2600MHz computer
benching cephes_expf .. -> 3.7 millions of vector evaluations/second -> 172 cycles/value on a 2600MHz computer
benching cephes_logf .. -> 5.5 millions of vector evaluations/second -> 117 cycles/value on a 2600MHz computer
benching sin_ps .. -> 26.1 millions of vector evaluations/second -> 25 cycles/value on a 2600MHz computer
benching cos_ps .. -> 26.1 millions of vector evaluations/second -> 25 cycles/value on a 2600MHz computer
benching sincos_ps .. -> 23.7 millions of vector evaluations/second -> 27 cycles/value on a 2600MHz computer
benching exp_ps .. -> 22.9 millions of vector evaluations/second -> 28 cycles/value on a 2600MHz computer
benching log_ps .. -> 21.6 millions of vector evaluations/second -> 30 cycles/value on a 2600MHz computer
benching acml vrs4_sinf .. -> 17.9 millions of vector evaluations/second -> 36 cycles/value on a 2600MHz computer
benching acml vrs4_cosf .. -> 18.3 millions of vector evaluations/second -> 35 cycles/value on a 2600MHz computer
benching acml vrs4_expf .. -> 28.6 millions of vector evaluations/second -> 23 cycles/value on a 2600MHz computer
benching acml vrs4_logf .. -> 23.6 millions of vector evaluations/second -> 27 cycles/value on a 2600MHz computer
on a 2GHz athlon-xp 2400+ , using mingw (gcc 3.4.5)
command line: gcc -mfpmath=sse -msse -O2 -Wall -W sse_mathfun_test.c
benching sinf .. -> 3.4 millions of vector evaluations/second -> 144 cycles/value on a 2000MHz computer
benching cosf .. -> 5.1 millions of vector evaluations/second -> 97 cycles/value on a 2000MHz computer
benching sincos (x87) .. -> 2.3 millions of vector evaluations/second -> 214 cycles/value on a 2000MHz computer
benching expf .. -> 1.8 millions of vector evaluations/second -> 272 cycles/value on a 2000MHz computer
benching logf .. -> 2.5 millions of vector evaluations/second -> 200 cycles/value on a 2000MHz computer
benching cephes_sinf .. -> 3.7 millions of vector evaluations/second -> 132 cycles/value on a 2000MHz computer
benching cephes_cosf .. -> 3.2 millions of vector evaluations/second -> 153 cycles/value on a 2000MHz computer
benching cephes_expf .. -> 1.2 millions of vector evaluations/second -> 407 cycles/value on a 2000MHz computer
benching cephes_logf .. -> 1.4 millions of vector evaluations/second -> 355 cycles/value on a 2000MHz computer
benching sin_ps .. -> 17.2 millions of vector evaluations/second -> 29 cycles/value on a 2000MHz computer
benching cos_ps .. -> 17.0 millions of vector evaluations/second -> 29 cycles/value on a 2000MHz computer
benching sincos_ps .. -> 14.7 millions of vector evaluations/second -> 34 cycles/value on a 2000MHz computer
benching exp_ps .. -> 17.2 millions of vector evaluations/second -> 29 cycles/value on a 2000MHz computer
benching log_ps .. -> 14.7 millions of vector evaluations/second -> 34 cycles/value on a 2000MHz computer
on the same 2GHz athlon-xp 2400+ , using cl.exe (visual c++ express 2005)
command line: cl.exe //arch:SSE //O2 //TP //MD sse_mathfun_test.c
benching sinf .. -> 3.1 millions of vector evaluations/second -> 160 cycles/value on a 2000MHz computer
benching cosf .. -> 3.9 millions of vector evaluations/second -> 127 cycles/value on a 2000MHz computer
benching sincos (x87) .. -> 2.8 millions of vector evaluations/second -> 175 cycles/value on a 2000MHz computer
benching expf .. -> 2.0 millions of vector evaluations/second -> 239 cycles/value on a 2000MHz computer
benching logf .. -> 2.6 millions of vector evaluations/second -> 192 cycles/value on a 2000MHz computer
benching cephes_sinf .. -> 2.5 millions of vector evaluations/second -> 198 cycles/value on a 2000MHz computer
benching cephes_cosf .. -> 2.8 millions of vector evaluations/second -> 176 cycles/value on a 2000MHz computer
benching cephes_expf .. -> 0.9 millions of vector evaluations/second -> 546 cycles/value on a 2000MHz computer
benching cephes_logf .. -> 1.3 millions of vector evaluations/second -> 370 cycles/value on a 2000MHz computer
benching sin_ps .. -> 17.2 millions of vector evaluations/second -> 29 cycles/value on a 2000MHz computer
benching cos_ps .. -> 17.3 millions of vector evaluations/second -> 29 cycles/value on a 2000MHz computer
benching sincos_ps .. -> 15.5 millions of vector evaluations/second -> 32 cycles/value on a 2000MHz computer
benching exp_ps .. -> 17.8 millions of vector evaluations/second -> 28 cycles/value on a 2000MHz computer
benching log_ps .. -> 10.4 millions of vector evaluations/second -> 48 cycles/value on a 2000MHz computer
*/
#include <stdio.h>
#include "Macros.h"
IDENTIFY("sse_mathfun test suite");
#include <xmmintrin.h>
/* useful when debuggin.. */
void print4(__m128 v) {
float *p = (float*)&v;
#ifndef USE_SSE2
_mm_empty();
#endif
printf("[%13.8g, %13.8g, %13.8g, %13.8g]", p[0], p[1], p[2], p[3]);
}
void print2i(__m64 v) {
unsigned *p = (unsigned*)&v;
printf("[%08x %08x]", p[0], p[1]);
}
#ifdef USE_SSE2
#include <emmintrin.h>
void print4i(__m128i v) {
unsigned *p = (unsigned*)&v;
printf("[%08x %08x %08x %08x]", p[0], p[1], p[2], p[3]);
}
#endif
#define SSE_MATHFUN_WITH_CODE
#include "sse_mathfun.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#ifdef HAVE_SYS_TIMES
#include <sys/times.h>
#include <unistd.h>
#endif
#ifdef HAVE_VECLIB
# include <vecLib/vfp.h>
#endif
#ifdef HAVE_ACML
# include <acml_mv_m128.h>
#endif
typedef ALIGN16_BEG union {
float f[4];
int i[4];
v4sf v;
} ALIGN16_END V4SF;
#define MAX(a,b) (((a)>(b))?(a):(b))
double frand() {
return rand()/(double)RAND_MAX;
}
#if defined(HAVE_SYS_TIMES)
inline double uclock_sec(void) {
static double ttclk = 0.;
if (ttclk == 0.) ttclk = sysconf(_SC_CLK_TCK);
struct tms t; return ((double)times(&t)) / ttclk;
}
# else
inline double uclock_sec(void)
{ return (double)clock()/(double)CLOCKS_PER_SEC; }
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif
int bitdiff(float a, float b) {
if (a == b) return 24;
else if (a == 0) { int j = (int)(-log(fabs(b))/M_LN2); if (j > 24) j = 24; return j; }
else return (int)(log(fabs(a))/M_LN2 - log(fabs(b-a))/M_LN2);
}
/* they are defined at the bottom of the file */
float cephes_sinf(float);
float cephes_cosf(float);
double cephes_sin(double);
float cephes_logf(float);
float cephes_expf(float);
int check_sincos_precision(float xmin, float xmax) {
unsigned nb_trials = 100000;
printf("checking sines on [%g*Pi, %g*Pi]\n", xmin, xmax);
float max_err_sin_ref = 0, max_err_sin_cep = 0, max_err_sin_x = 0;
float max_err_sin_cepref = 0, max_err_sin_cep_x = 0;
double max_err_dsin_cepref = 0, max_err_dsin_cep_x = 0;
float max_err_cos_ref = 0, max_err_cos_cep = 0, max_err_cos_x = 0;
float max_err_sum_sqr_test = 0;
float max_err_sum_sqr_ref = 0;
xmin *= (float) M_PI; xmax *= (float) M_PI;
unsigned i, sindiff_warned = 0, cosdiff_warned = 0;
for (i=0; i < nb_trials; ++i) {
V4SF vx, sin4, cos4, sin4_2, cos4_2;
vx.f[0] = i*(xmax-xmin)/(nb_trials-1) + xmin;
vx.f[1] = (float) (i+.5)*(xmax-xmin)/(nb_trials-1) + xmin;
vx.f[2] = (float) frand()*(xmax-xmin);
vx.f[3] = (float) ((i / 32)*M_PI/((i%32)+1));
if (vx.f[3] < xmin || vx.f[3] > xmax) vx.f[3] = (float) frand()*(xmax-xmin);
/*
vx.f[0] = M_PI/2;
vx.f[1] = M_PI;
vx.f[2] = M_PI/3;
vx.f[3] = M_PI/4;
*/
sin4.v = sin_ps(vx.v);
cos4.v = cos_ps(vx.v);
sincos_ps(vx.v, &sin4_2.v, &cos4_2.v);
unsigned j;
for (j=0; j < 4; ++j) {
float x = vx.f[j];
float sin_test = sin4.f[j];
float cos_test = cos4.f[j];
if (sin_test != sin4_2.f[j]) {
if( !sindiff_warned ){
printf("sin / sincos mismatch at x=%g, diff=%g\n", x, sin_test - sin4_2.f[j] );
}
sindiff_warned += 1;
// exit(1); return 1;
}
if (cos_test != cos4_2.f[j]) {
if( !cosdiff_warned ){
printf("cos / sincos mismatch at x=%g, diff=%g\n", x, cos_test - cos4_2.f[j] );
}
cosdiff_warned += 1;
// return 1;
}
float sin_ref = sinf(x);
float sin_cep = cephes_sinf(x);
float err_sin_ref = fabs(sin_ref - sin_test);
float err_sin_cep = fabs(sin_cep - sin_test);
float err_sin_cepref = fabs(sin_cep - sin_ref);
if (err_sin_ref > max_err_sin_ref) {
max_err_sin_ref = err_sin_ref;
max_err_sin_x = x;
}
max_err_sin_cep = MAX(max_err_sin_cep, err_sin_cep);
if (err_sin_cepref > max_err_sin_cepref) {
max_err_sin_cepref = err_sin_cepref;
max_err_sin_cep_x = x;
}
double err_dsin_cepref = fabs(cephes_sin(x) - sin(x));
if (err_dsin_cepref > max_err_dsin_cepref) {
max_err_dsin_cepref = err_dsin_cepref;
max_err_dsin_cep_x = x;
}
float cos_ref = cosf(x);
float cos_cep = cephes_cosf(x);
float err_cos_ref = fabs(cos_ref - cos_test);
float err_cos_cep = fabs(cos_cep - cos_test);
if (err_cos_ref > max_err_cos_ref) {
max_err_cos_ref = err_cos_ref;
max_err_cos_x = x;
}
max_err_cos_cep = MAX(max_err_cos_cep, err_cos_cep);
float err_sum_sqr_test = fabs(1 - cos_test*cos_test - sin_test*sin_test);
float err_sum_sqr_ref = fabs(1 - cos_ref*cos_ref - sin_ref*sin_ref);
max_err_sum_sqr_ref = MAX(max_err_sum_sqr_ref, err_sum_sqr_ref);
max_err_sum_sqr_test = MAX(max_err_sum_sqr_test, err_sum_sqr_test);
//printf("sin(%g) = %g %g err=%g\n", x, sin_ref, sin_test, err_sin_ref);
}
}
printf("max deviation from sinf(x): %g at %14.12g*Pi, max deviation from cephes_sin(x): %g\n",
max_err_sin_ref, max_err_sin_x/M_PI, max_err_sin_cep);
printf("max deviation between cephes_sinf(x) and sinf(x): %g at %14.12g*Pi\n",
max_err_sin_cepref, max_err_sin_cep_x/M_PI );
printf("max deviation between cephes_sin(x) and sin(x): %g at %14.12g*Pi\n",
max_err_dsin_cepref, max_err_dsin_cep_x/M_PI );
printf("max deviation from cosf(x): %g at %14.12g*Pi, max deviation from cephes_cos(x): %g\n",
max_err_cos_ref, max_err_cos_x/M_PI, max_err_cos_cep);
printf("deviation of sin(x)^2+cos(x)^2-1: %g (ref deviation is %g)\n",
max_err_sum_sqr_test, max_err_sum_sqr_ref);
if (max_err_sum_sqr_ref < 2e-7 && max_err_sin_ref < 2e-7 && max_err_cos_ref < 2e-7) {
printf(" ->> precision OK for the sin_ps / cos_ps / sincos_ps <<-\n\n");
return 0;
} else {
printf("\n WRONG PRECISION !! there is a problem\n\n");
return 1;
}
}
union float_int_union {
int i;
float f;
} QNAN = { 0xFFC00000 }, QNAN2 = { 0x7FC00000 }, PINF = { 0x7F800000 }, MINF = { 0xFF800000 };
int check_explog_precision(float xmin, float xmax) {
unsigned nb_trials = 100000;
printf("checking exp/log [%g, %g]\n", xmin, xmax);
float max_err_exp_ref = 0, max_err_exp_cep = 0, max_err_exp_x = 0;
float max_err_log_ref = 0, max_err_log_cep = 0, max_err_log_x = 0;
float max_err_logexp_test = 0;
float max_err_logexp_ref = 0;
unsigned i;
for (i=0; i < nb_trials; ++i) {
V4SF vx, exp4, log4;
vx.f[0] = (float) frand()*(xmax-xmin)+xmin;
vx.f[1] = (float) frand()*(xmax-xmin)+xmin;
vx.f[2] = (float) frand()*(xmax-xmin)+xmin;
vx.f[3] = (float) frand()*(xmax-xmin)+xmin;
exp4.v = exp_ps(vx.v);
log4.v = log_ps(exp4.v);
unsigned j;
for (j=0; j < 4; ++j) {
float x = vx.f[j];
float exp_test = exp4.f[j];
float log_test = log4.f[j];
float exp_ref = expf(x);
float exp_cep = cephes_expf(x);
float err_exp_ref = fabs(exp_ref - exp_test)/exp_ref;
float err_exp_cep = fabs(exp_cep - exp_test)/exp_ref;
if (err_exp_ref > max_err_exp_ref) {
max_err_exp_ref = err_exp_ref;
max_err_exp_x = x;
}
max_err_exp_cep = MAX(max_err_exp_cep, err_exp_cep);
float log_ref = logf(exp_test);
float log_cep = cephes_logf(exp_test);
float err_log_ref = fabs(log_ref - log_test);
float err_log_cep = fabs(log_cep - log_test);
if (err_log_ref > max_err_log_ref) {
max_err_log_ref = err_log_ref;
max_err_log_x = x;
}
max_err_log_cep = MAX(max_err_log_cep, err_log_cep);
float err_logexp_test = fabs(x - log_test);
float err_logexp_ref = fabs(x - logf(expf(x)));
max_err_logexp_ref = MAX(max_err_logexp_ref, err_logexp_ref);
max_err_logexp_test = MAX(max_err_logexp_test, err_logexp_test);
}
}
printf("max (relative) deviation from expf(x): %g at %14.12g, max deviation from cephes_expf(x): %g\n",
max_err_exp_ref, max_err_exp_x, max_err_exp_cep);
printf("max (absolute) deviation from logf(x): %g at %14.12g, max deviation from cephes_logf(x): %g\n",
max_err_log_ref, max_err_log_x, max_err_log_cep);
printf("deviation of x - log(exp(x)): %g (ref deviation is %g)\n",
max_err_logexp_test, max_err_logexp_ref);
if (max_err_logexp_test < 2e-7 && max_err_exp_ref < 2e-7 && max_err_log_ref < 2e-7) {
printf(" ->> precision OK for the exp_ps / log_ps <<-\n\n");
return 0;
} else {
printf("\n WRONG PRECISION !! there is a problem\n\n");
return 1;
}
}
void dumb() {
V4SF x = {{ 0.0903333798051f, 0.0903333798051f, 0.0903333798051f, 0.0903333798051f }};
V4SF w; w.v = log_ps(x.v);
float z = cephes_logf(x.f[0]);
printf("log_ps returned "); print4(w.v);
printf("\ncephes returned: %14.12g and logf(%g)=%14.12g\n", z, x.f[0], logf(x.f[0]));
print4(_mm_cmpeq_ps(x.v, x.v)); printf("\n");
exit(1);
}
void check_special_values() {
V4SF vx;
vx.f[0] = -1000;
vx.f[1] = -100;
vx.f[2] = 100;
vx.f[3] = 1000;
printf("exp("); print4(vx.v); printf(") = "); print4(exp_ps(vx.v)); printf("\n");
vx.f[0] = QNAN.f;
vx.f[1] = PINF.f;
vx.f[2] = MINF.f;
vx.f[3] = QNAN2.f;
printf("exp("); print4(vx.v); printf(") = "); print4(exp_ps(vx.v)); printf("\n");
vx.f[0] = 0;
vx.f[1] = -10;
vx.f[2] = 1e30f;
vx.f[3] = 1e-42f;
printf("log("); print4(vx.v); printf(") = "); print4(log_ps(vx.v)); printf("\n");
vx.f[0] = QNAN.f;
vx.f[1] = PINF.f;
vx.f[2] = MINF.f;
vx.f[3] = QNAN2.f;
printf("log("); print4(vx.v); printf(") = "); print4(log_ps(vx.v)); printf("\n");
printf("sin("); print4(vx.v); printf(") = "); print4(sin_ps(vx.v)); printf("\n");
printf("cos("); print4(vx.v); printf(") = "); print4(cos_ps(vx.v)); printf("\n");
vx.f[0] = -1e30f;
vx.f[1] = -100000.0f;
vx.f[2] = 1e30f;
vx.f[3] = 100000.0f;
printf("sin("); print4(vx.v); printf(") = "); print4(sin_ps(vx.v)); printf("\n");
printf("cos("); print4(vx.v); printf(") = "); print4(cos_ps(vx.v)); printf("\n");
}
#define DECL_SCALAR_FN_BENCH(fn) \
int bench_##fn() { \
int niter = 100000,i,j; \
float x = 0.5f, y=0; \
for (i=0; i < niter; ++i) { \
for (j=0; j < 4; ++j) { \
x += 1e-6f; \
y += fn(x+5*(j&1)); \
} \
} \
if (y == 2.32132323232f) niter--; \
return niter; \
}
#define DECL_SCALAR_FN_BENCH64(fn) \
int bench_##fn() { \
int niter = 100000,i,j; \
double x = 0.5, y=0; \
for (i=0; i < niter; ++i) { \
for (j=0; j < 4; ++j) { \
x += 1e-6; \
y += fn(x+5*(j&1)); \
} \
} \
if (y == 2.32132323232) niter--; \
return niter; \
}
#define DECL_VECTOR_FN_BENCH(fn) \
int bench_##fn() { \
int niter = 100000,i; \
v4sf bmin = _mm_set_ps1(0.5), bmax = _mm_set_ps1(1.0); \
v4sf x = _mm_set_ps1(0.75); \
for (i=0; i < niter; ++i) { \
x = fn(x); x = _mm_min_ps(x, bmax); x = _mm_max_ps(x, bmin); \
} \
if (((float*)&x)[0] == 2.32132323232f) niter--; \
return niter; \
}
#define DECL_VECTOR_FN_BENCH64(fn) \
int bench_##fn() { \
int niter = 100000,i; \
v2df bmin = _mm_set1_pd(0.5), bmax = _mm_set1_pd(1.0); \
v2df x = _mm_set1_pd(0.75); \
for (i=0; i < niter; ++i) { \
x = fn(x); x = _mm_min_pd(x, bmax); x = _mm_max_pd(x, bmin); \
} \
if (((double*)&x)[0] == 2.32132323232) niter--; \
return niter; \
}
#ifdef __GNUC__
#define HAVE_SINCOS_X86_FPU
void sincos_x86_fpu(double t, double *st, double *ct) {
asm ("fsincos;" : "=t" (*ct), "=u" (*st) : "0" (t) : "st(7)");
//*st = sin(t); *ct = cos(t);
}
#elif defined(_MSC_VER) && !defined(_WIN64)
#define HAVE_SINCOS_X86_FPU
void sincos_x86_fpu(double t, double *st_, double *ct_) {
_asm {
fld QWORD PTR [t]
fsincos
mov ebx,[ct_]
fstp QWORD PTR [ebx]
mov ebx,[st_]
fstp QWORD PTR [ebx]
}
}
#endif
#ifdef HAVE_SINCOS_X86_FPU
float stupid_sincosf_x86_fpu(float x) {
double s, c;
sincos_x86_fpu(x, &s, &c);
return (float)(s+c);
}
double stupid_sincos_x86_fpu(double x) {
double s, c;
sincos_x86_fpu(x, &s, &c);
return (s+c);
}
#endif
float sinfPLUScosf(float x) {
float s = sinf(x), c = cosf(x);
return (s+c);
}
double sinPLUScos(double x) {
double s = sin(x), c = cos(x);
return (s+c);
}
v4sf stupid_sincos_ps(v4sf x) {
v4sf s, c;
sincos_ps(x, &s, &c);
return s;
}
#ifdef USE_SSE2
v2df stupid_sincos_pd(v2df x) {
v2df s, c;
sincos_pd(x, &s, &c);
return s;
}
#endif
#ifdef HAVE_VECLIB
vFloat stupid_vsincosf(vFloat arg)
{ vFloat res;
return vsincosf(arg, &res);
}
vDouble stupid_vsincos(vDouble arg)
{ vDouble sins, coss;
int n = 2;
vvsincos( sins, coss, arg, &n );
return coss;
}
#endif
DECL_SCALAR_FN_BENCH(sinf);
DECL_SCALAR_FN_BENCH(cosf);
#ifdef HAVE_SINCOS_X86_FPU
DECL_SCALAR_FN_BENCH(stupid_sincosf_x86_fpu);
#endif
DECL_SCALAR_FN_BENCH64(sin);
DECL_SCALAR_FN_BENCH64(cos);
#ifdef HAVE_SINCOS_X86_FPU
DECL_SCALAR_FN_BENCH64(stupid_sincos_x86_fpu);
#endif
DECL_SCALAR_FN_BENCH(sinfPLUScosf);
DECL_SCALAR_FN_BENCH64(sinPLUScos);
DECL_SCALAR_FN_BENCH(logf);
DECL_SCALAR_FN_BENCH(expf);
DECL_SCALAR_FN_BENCH(cephes_sinf);
DECL_SCALAR_FN_BENCH64(cephes_sin);
DECL_SCALAR_FN_BENCH(cephes_cosf);
DECL_SCALAR_FN_BENCH(cephes_expf);
DECL_SCALAR_FN_BENCH(cephes_logf);
DECL_VECTOR_FN_BENCH(sin_ps);
DECL_VECTOR_FN_BENCH(cos_ps);
DECL_VECTOR_FN_BENCH(stupid_sincos_ps);
#ifdef USE_SSE2
DECL_VECTOR_FN_BENCH64(stupid_sincos_pd);
#endif
DECL_VECTOR_FN_BENCH(exp_ps);
DECL_VECTOR_FN_BENCH(log_ps);
#ifdef HAVE_VECLIB
DECL_VECTOR_FN_BENCH(vsinf);
DECL_VECTOR_FN_BENCH(vcosf);
DECL_VECTOR_FN_BENCH(stupid_vsincosf);
DECL_VECTOR_FN_BENCH64(stupid_vsincos);
DECL_VECTOR_FN_BENCH(vlogf);
DECL_VECTOR_FN_BENCH(vexpf);
#endif
#ifdef HAVE_ACML
DECL_VECTOR_FN_BENCH(__vrs4_sinf);
DECL_VECTOR_FN_BENCH(__vrs4_cosf);
DECL_VECTOR_FN_BENCH(__vrs4_expf);
DECL_VECTOR_FN_BENCH(__vrs4_logf);
#endif
void run_bench(const char *s, int (*fn)()) {
printf("benching %20s ..", s); fflush(stdout);
double t0 = uclock_sec(), t1, tmax = 1.0;
double niter = 0;
do {
niter += fn();
t1 = uclock_sec();
} while (t1 - t0 < tmax);
#define REF_FREQ_MHZ 2000.0
printf(" -> %6.1f millions of vector evaluations/second over %g iters -> %3.0f cycles/value on a %gMHz computer\n",
floor(niter/(t1-t0)/1e5)/10, niter, (t1-t0)*REF_FREQ_MHZ*1e6/niter/4, REF_FREQ_MHZ);
}
void sanity_check() {
printf("doing some sanity checks...\n");
#ifndef USE_SSE2
V4SF v = {{1, 2, 3, 4}}, z = {{5, 6, 7, 8}};
v2si mm0, mm1;
COPY_XMM_TO_MM(v.v, mm0, mm1);
COPY_MM_TO_XMM(mm0, mm1, v.v);
printf("truncation to int: "); print2i(mm0); print2i(mm1); printf("\n");
_mm_empty();
printf("float vector: "); print4(v.v); printf("\n"); fflush(stdout);
assert(v.f[0] == 1);
assert(v.f[1] == 2);
assert(v.f[2] == 3);
assert(v.f[3] == 4);
V4SF w; w.v = v.v;
//printf("float vector: "); print4(w); printf("\n");
mm0 = _mm_cvttps_pi32(w.v);
w.v = _mm_movehl_ps(w.v,w.v);
mm1 = _mm_cvttps_pi32(w.v);
/*_mm_empty();
printf("truncation to int: "); print2i(mm0); print2i(mm1); printf("\n");
*/
w.v = _mm_cvtpi32x2_ps(mm0, mm1);
print2i(((v2si*)&w.v)[0]);
print2i(((v2si*)&w.v)[1]); _mm_empty();
v.v = w.v;
assert((((int)&v.v)&0xf) == 0);
printf("converted back to float: "); print4(v.v); printf("\n"); fflush(stdout);
assert(v.f[0] == 1);
assert(v.f[1] == 2);
assert(v.f[2] == 3);
assert(v.f[3] == 4);
w.v = _mm_movehl_ps(z.v, w.v);
printf("test for _mm_movehl_ps bug..\n"); print4(w.v); printf("\n"); fflush(stdout);
if (w.f[0] != 3 ||
w.f[1] != 4 ||
w.f[2] != 7 ||
w.f[3] != 8) {
printf("your compiler has the nasty bug on _mm_movehl_ps: IT IS BROKEN\n");
exit(1);
}
printf("test for _mm_cmpxx_ps bug..\n");
V4SF r; r.v = _mm_cmplt_ps(w.v, w.v);
if (r.i[0] != 0 || r.i[1] != 0 || r.i[2] != 0 || r.i[3] != 0) {
printf("your compiler has the nasty bug on all _mm_cmp*_ps functions: IT IS BROKEN\n");
exit(1);
}
r.v = _mm_cmpeq_ps(w.v, w.v);
if (r.i[0] != -1 || r.i[1] != -1 || r.i[2] != -1 || r.i[3] != -1) {
printf("your compiler has the nasty bug on all _mm_cmp*_ps functions: IT IS BROKEN\n");
exit(1);
}
#endif // USE_SSE2
}
int main() {
//dumb();
//sanity_check();
int err = 0;
err += check_sincos_precision(0., 1.0);
err += check_sincos_precision(-1000, 1000);
err += check_explog_precision(-60, 60);
if (err) {
printf("some precision tests have failed\n");
}
check_special_values();
run_bench("sinf", bench_sinf);
run_bench("cosf", bench_cosf);
#ifdef HAVE_SINCOS_X86_FPU
run_bench("sincosf (x87)", bench_stupid_sincosf_x86_fpu);
#endif
run_bench("sin", bench_sin);
run_bench("cos", bench_cos);
#ifdef HAVE_SINCOS_X86_FPU
run_bench("sincos (x87)", bench_stupid_sincos_x86_fpu);
#endif
run_bench("sinf+cosf", bench_sinfPLUScosf);
run_bench("sin+cos", bench_sinPLUScos);
run_bench("expf", bench_expf);
run_bench("logf", bench_logf);
run_bench("cephes_sinf", bench_cephes_sinf);
run_bench("cephes_cosf", bench_cephes_cosf);
run_bench("cephes_sin", bench_cephes_sin);
run_bench("cephes_expf", bench_cephes_expf);
run_bench("cephes_logf", bench_cephes_logf);
run_bench("sin_ps", bench_sin_ps);
run_bench("cos_ps", bench_cos_ps);
run_bench("sincos_ps", bench_stupid_sincos_ps);
run_bench("sincos_pd", bench_stupid_sincos_pd);
run_bench("exp_ps", bench_exp_ps);
run_bench("log_ps", bench_log_ps);
#ifdef HAVE_VECLIB
run_bench("vsinf", bench_vsinf);
run_bench("vcosf", bench_vcosf);
run_bench("vsincosf", bench_stupid_vsincosf);
run_bench("vsincos", bench_stupid_vsincos);
run_bench("vexpf", bench_vexpf);
run_bench("vlogf", bench_vlogf);
#endif
#ifdef HAVE_ACML
run_bench("acml vrs4_sinf", bench___vrs4_sinf);
run_bench("acml vrs4_cosf", bench___vrs4_cosf);
run_bench("acml vrs4_expf", bench___vrs4_expf);
run_bench("acml vrs4_logf", bench___vrs4_logf);
#endif
return err;
}
/* cephes functions, copied here to serve as a reference */
/* sinf.c
*
* Circular sine
*
*
*
* SYNOPSIS:
*
* float x, y, sinf();
*
* y = sinf( x );
*
*
*
* DESCRIPTION:
*
* Range reduction is into intervals of pi/4. The reduction
* error is nearly eliminated by contriving an extended precision
* modular arithmetic.
*
* Two polynomial approximating functions are employed.
* Between 0 and pi/4 the sine is approximated by
* x + x**3 P(x**2).
* Between pi/4 and pi/2 the cosine is represented as
* 1 - x**2 Q(x**2).
*
*
* ACCURACY:
*
* Relative error:
* arithmetic domain # trials peak rms
* IEEE -4096,+4096 100,000 1.2e-7 3.0e-8
* IEEE -8192,+8192 100,000 3.0e-7 3.0e-8
*
* ERROR MESSAGES:
*
* message condition value returned
* sin total loss x > 2^24 0.0
*
* Partial loss of accuracy begins to occur at x = 2^13
* = 8192. Results may be meaningless for x >= 2^24
* The routine as implemented flags a TLOSS error
* for x >= 2^24 and returns 0.0.
*/
/* cosf.c
*
* Circular cosine
*
*
*
* SYNOPSIS:
*
* float x, y, cosf();
*
* y = cosf( x );
*
*
*
* DESCRIPTION:
*
* Range reduction is into intervals of pi/4. The reduction
* error is nearly eliminated by contriving an extended precision
* modular arithmetic.
*
* Two polynomial approximating functions are employed.
* Between 0 and pi/4 the cosine is approximated by
* 1 - x**2 Q(x**2).
* Between pi/4 and pi/2 the sine is represented as
* x + x**3 P(x**2).
*
*
* ACCURACY:
*
* Relative error:
* arithmetic domain # trials peak rms
* IEEE -8192,+8192 100,000 3.0e-7 3.0e-8
*/
/*
Cephes Math Library Release 2.2: June, 1992
Copyright 1985, 1987, 1988, 1992 by Stephen L. Moshier
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
*/
/* Single precision circular sine
* test interval: [-pi/4, +pi/4]
* trials: 10000
* peak relative error: 6.8e-8
* rms relative error: 2.6e-8
*/
static float FOPI = 1.27323954473516f;
static float PIO4F = 0.7853981633974483096f;
/* Note, these constants are for a 32-bit significand: */
/*
static float DP1 = 0.7853851318359375;
static float DP2 = 1.30315311253070831298828125e-5;
static float DP3 = 3.03855025325309630e-11;
static float lossth = 65536.;
*/
/* These are for a 24-bit significand: */
static float DP1 = 0.78515625;
static float DP2 = 2.4187564849853515625e-4;
static float DP3 = 3.77489497744594108e-8f;
static float lossth = 8192.f;
static float T24M1 = 16777215.f;
static float sincof[] = {
-1.9515295891E-4f,
8.3321608736E-3f,
-1.6666654611E-1f
};
static float coscof[] = {
2.443315711809948E-005f,
-1.388731625493765E-003f,
4.166664568298827E-002f
};
float cephes_sinf( float xx )
{
float *p;
float x, y, z;
register unsigned long j;
register int sign;
sign = 1;
x = xx;
if( xx < 0 )
{
sign = -1;
x = -xx;
}
if( x > T24M1 )
{
//mtherr( "sinf", TLOSS );
return(0.0);
}
j = (unsigned long) (FOPI * x); /* integer part of x/(PI/4) */
y = (float) j;
/* map zeros to origin */
if( j & 1 )
{
j += 1;
y += 1.0;
}
j &= 7; /* octant modulo 360 degrees */
/* reflect in x axis */
if( j > 3)
{
sign = -sign;
j -= 4;
}
if( x > lossth )
{
//mtherr( "sinf", PLOSS );
x = x - y * PIO4F;
}
else
{
/* Extended precision modular arithmetic */
x = ((x - y * DP1) - y * DP2) - y * DP3;
}
/*einits();*/
z = x * x;
//printf("my_sinf: corrected oldx, x, y = %14.10g, %14.10g, %14.10g\n", oldx, x, y);
if( (j==1) || (j==2) )
{
/* measured relative error in +/- pi/4 is 7.8e-8 */
/*
y = (( 2.443315711809948E-005 * z
- 1.388731625493765E-003) * z
+ 4.166664568298827E-002) * z * z;
*/
p = coscof;
y = *p++;
y = y * z + *p++;
y = y * z + *p++;
y *= z; y *= z;
y -= 0.5f * z;
y += 1.0;
}
else
{
/* Theoretical relative error = 3.8e-9 in [-pi/4, +pi/4] */
/*
y = ((-1.9515295891E-4 * z
+ 8.3321608736E-3) * z
- 1.6666654611E-1) * z * x;
y += x;
*/
p = sincof;
y = *p++;
y = y * z + *p++;
y = y * z + *p++;
y *= z; y *= x;
y += x;
}
/*einitd();*/
//printf("my_sinf: j=%d result = %14.10g * %d\n", j, y, sign);
if(sign < 0)
y = -y;
return( y);
}
/* Single precision circular cosine
* test interval: [-pi/4, +pi/4]
* trials: 10000
* peak relative error: 8.3e-8
* rms relative error: 2.2e-8
*/
float cephes_cosf( float xx )
{
float x, y, z;
int j, sign;
/* make argument positive */
sign = 1;
x = xx;
if( x < 0 )
x = -x;
if( x > T24M1 )
{
//mtherr( "cosf", TLOSS );
return(0.0);
}
j = (int) (FOPI * x); /* integer part of x/PIO4 */
y = (float) j;
/* integer and fractional part modulo one octant */
if( j & 1 ) /* map zeros to origin */
{
j += 1;
y += 1.0;
}
j &= 7;
if( j > 3)
{
j -=4;
sign = -sign;
}
if( j > 1 )
sign = -sign;
if( x > lossth )
{
//mtherr( "cosf", PLOSS );
x = x - y * PIO4F;
}
else
/* Extended precision modular arithmetic */
x = ((x - y * DP1) - y * DP2) - y * DP3;
//printf("xx = %g -> x corrected = %g sign=%d j=%d y=%g\n", xx, x, sign, j, y);
z = x * x;
if( (j==1) || (j==2) )
{
y = (((-1.9515295891E-4f * z
+ 8.3321608736E-3f) * z
- 1.6666654611E-1f) * z * x)
+ x;
}
else
{
y = (( 2.443315711809948E-005f * z
- 1.388731625493765E-003f) * z
+ 4.166664568298827E-002f) * z * z;
y -= 0.5f * z;
y += 1.0;