This repository has been archived by the owner on Mar 21, 2020. It is now read-only.
forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-cpu.c
2515 lines (2227 loc) · 56.4 KB
/
stress-cpu.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
/*
* Copyright (C) 2013-2018 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of 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.
*
* This program 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 a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#include <math.h>
#include <complex.h>
#define GAMMA (0.57721566490153286060651209008240243104215933593992L)
#define OMEGA (0.56714329040978387299996866221035554975381578718651L)
#define PSI (3.359885666243177553172011302918927179688905133732L)
#define STATS_MAX (250)
/*
* Some awful math lib workarounds for functions that some
* math libraries don't have implemented (yet)
*/
#if !defined(HAVE_CABSL)
#define cabsl cabs
#endif
#if !defined(HAVE_LGAMMAL)
#define lgammal lgamma
#endif
#if !defined(HAVE_CCOSL)
#define ccosl ccos
#endif
#if !defined(HAVE_CSINL)
#define csinl csin
#endif
#if !defined(HAVE_CPOW)
#define cpow pow
#endif
#if !defined(HAVE_POWL)
#define powl pow
#endif
#if !defined(HAVE_RINTL)
#define rintl rint
#endif
#if !defined(HAVE_LOGL)
#define logl log
#endif
#if !defined(HAVE_EXPL)
#define expl exp
#endif
#if !defined(HAVE_COSL)
#define cosl cos
#endif
#if !defined(HAVE_SINL)
#define sinl sin
#endif
#if !defined(HAVE_COSHL)
#define coshl cosh
#endif
#if !defined(HAVE_SINHL)
#define sinhl sinh
#endif
#if !defined(HAVE_SQRTL)
#define sqrtl sqrt
#endif
/*
* the CPU stress test has different classes of cpu stressor
*/
typedef void (*stress_cpu_func)(const char *name);
typedef struct {
const char *name; /* human readable form of stressor */
const stress_cpu_func func; /* the cpu method function */
} stress_cpu_method_info_t;
static const stress_cpu_method_info_t cpu_methods[];
/* Don't make this static to ensure dithering does not get optimised out */
uint8_t pixels[STRESS_CPU_DITHER_X][STRESS_CPU_DITHER_Y];
int stress_set_cpu_load(const char *opt) {
int32_t cpu_load;
cpu_load = get_int32(opt);
check_range("cpu-load", cpu_load, 0, 100);
return set_setting("cpu-load", TYPE_ID_INT32, &cpu_load);
}
/*
* stress_set_cpu_load_slice()
* < 0 - number of iterations per busy slice
* = 0 - random duration between 0..0.5 seconds
* > 0 - milliseconds per busy slice
*/
int stress_set_cpu_load_slice(const char *opt)
{
int32_t cpu_load_slice;
cpu_load_slice = get_int32(opt);
if ((cpu_load_slice < -5000) || (cpu_load_slice > 5000)) {
(void)fprintf(stderr, "cpu-load-slice must in the range -5000 to 5000.\n");
_exit(EXIT_FAILURE);
}
return set_setting("cpu-load-slice", TYPE_ID_INT32, &cpu_load_slice);
}
/*
* stress_cpu_sqrt()
* stress CPU on square roots
*/
static void HOT TARGET_CLONES stress_cpu_sqrt(const char *name)
{
int i;
for (i = 0; i < 16384; i++) {
uint64_t rnd = mwc32();
double r = sqrt((double)rnd) * sqrt((double)rnd);
if (UNLIKELY((g_opt_flags & OPT_FLAGS_VERIFY) &&
(uint64_t)rint(r) != rnd)) {
pr_fail("%s: sqrt error detected on "
"sqrt(%" PRIu64 ")\n", name, rnd);
if (!g_keep_stressing_flag)
break;
}
}
}
/*
* stress_cpu_loop()
* simple CPU busy loop
*/
static void OPTIMIZE0 stress_cpu_loop(const char *name)
{
uint32_t i, i_sum = 0;
const uint32_t sum = 134209536UL;
for (i = 0; i < 16384; i++) {
i_sum += i;
FORCE_DO_NOTHING();
}
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i_sum != sum))
pr_fail("%s: cpu loop 0..16383 sum was %" PRIu32 " and "
"did not match the expected value of %" PRIu32 "\n",
name, i_sum, sum);
}
/*
* stress_cpu_gcd()
* compute Greatest Common Divisor
*/
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_gcd(const char *name)
{
uint32_t i, i_sum = 0;
const uint32_t sum = 63000868UL;
for (i = 0; i < 16384; i++) {
register uint32_t a = i, b = i % (3 + (1997 ^ i));
while (b != 0) {
register uint32_t r = b;
b = a % b;
a = r;
}
i_sum += a;
FORCE_DO_NOTHING();
}
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i_sum != sum))
pr_fail("%s: gcd error detected, failed modulo "
"or assignment operations\n", name);
}
/*
* stress_cpu_bitops()
* various bit manipulation hacks from bithacks
* https://graphics.stanford.edu/~seander/bithacks.html
*/
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_bitops(const char *name)
{
uint32_t i, i_sum = 0;
const uint32_t sum = 0x8aac0aab;
for (i = 0; i < 16384; i++) {
{
register uint32_t r, v, s = (sizeof(v) * 8) - 1;
/* Reverse bits */
r = v = i;
for (v >>= 1; v; v >>= 1, s--) {
r <<= 1;
r |= v & 1;
}
r <<= s;
i_sum += r;
}
{
/* parity check */
register uint32_t v = i;
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
i_sum += (0x6996 >> v) & 1;
}
{
/* Brian Kernighan count bits */
register uint32_t j, v = i;
for (j = 0; v; j++)
v &= v - 1;
i_sum += j;
}
{
/* round up to nearest highest power of 2 */
register uint32_t v = i - 1;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
i_sum += v;
}
}
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i_sum != sum))
pr_fail("%s: bitops error detected, failed "
"bitops operations\n", name);
}
/*
* stress_cpu_trig()
* simple sin, cos trig functions
*/
static void HOT stress_cpu_trig(const char *name)
{
int i;
long double d_sum = 0.0L;
(void)name;
for (i = 0; i < 1500; i++) {
long double theta = (2.0L * M_PI * (double)i)/1500.0L;
{
d_sum += (cosl(theta) * sinl(theta));
d_sum += (cos(theta) * sin(theta));
d_sum += (cosf(theta) * sinf(theta));
}
{
long double theta2 = theta * 2.0L;
d_sum += cosl(theta2);
d_sum += cos(theta2);
d_sum += cosf(theta2);
}
{
long double theta3 = theta * 3.0L;
d_sum += sinl(theta3);
d_sum += sin(theta3);
d_sum += sinf(theta3);
}
}
double_put(d_sum);
}
/*
* stress_cpu_hyperbolic()
* simple hyperbolic sinh, cosh functions
*/
static void HOT stress_cpu_hyperbolic(const char *name)
{
int i;
double d_sum = 0.0;
(void)name;
for (i = 0; i < 1500; i++) {
long double theta = (2.0L * M_PI * (double)i)/1500.0L;
{
d_sum += (coshl(theta) * sinhl(theta));
d_sum += (cosh(theta) * sinh(theta));
d_sum += (double)(coshf(theta) * sinhf(theta));
}
{
long double theta2 = theta * 2.0L;
d_sum += coshl(theta2);
d_sum += cosh(theta2);
d_sum += (double)coshf(theta2);
}
{
long double theta3 = theta * 3.0L;
d_sum += sinhl(theta3);
d_sum += sinh(theta3);
d_sum += (double)sinhf(theta3);
}
}
double_put(d_sum);
}
/*
* stress_cpu_rand()
* generate lots of pseudo-random integers
*/
static void HOT OPTIMIZE3 stress_cpu_rand(const char *name)
{
int i;
uint32_t i_sum = 0;
const uint32_t sum = 0xc253698c;
MWC_SEED();
for (i = 0; i < 16384; i++)
i_sum += mwc32();
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i_sum != sum))
pr_fail("%s: rand error detected, failed sum of "
"pseudo-random values\n", name);
}
/*
* stress_cpu_rand48()
* generate random values using rand48 family of functions
*/
static void HOT OPTIMIZE3 stress_cpu_rand48(const char *name)
{
int i;
double d = 0;
long int l = 0;
(void)name;
srand48(0x0defaced);
for (i = 0; i < 16384; i++) {
d += drand48();
l += lrand48();
}
double_put(d);
uint64_put(l);
}
/*
* stress_cpu_nsqrt()
* iterative Newton–Raphson square root
*/
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_nsqrt(const char *name)
{
int i;
const long double precision = 1.0e-12L;
const int max_iter = 56;
for (i = 0; i < 16384; i++) {
long double n = (double)i;
long double lo = (n < 1.0L) ? n : 1.0L;
long double hi = (n < 1.0L) ? 1.0L : n;
long double rt;
int j = 0;
while ((j++ < max_iter) && ((hi - lo) > precision)) {
long double g = (lo + hi) / 2.0L;
if ((g * g) > n)
hi = g;
else
lo = g;
}
rt = (lo + hi) / 2.0L;
if (g_opt_flags & OPT_FLAGS_VERIFY) {
if (j >= max_iter)
pr_fail("%s: Newton-Raphson sqrt "
"computation took more iterations "
"than expected\n", name);
if ((int)rintl(rt * rt) != i)
pr_fail("%s: Newton-Rapshon sqrt not "
"accurate enough\n", name);
}
}
}
/*
* stress_cpu_phi()
* compute the Golden Ratio
*/
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_phi(const char *name)
{
long double phi; /* Golden ratio */
const long double precision = 1.0e-15L;
const long double phi_ = (1.0L + sqrtl(5.0L)) / 2.0L;
register uint64_t a, b;
const uint64_t mask = 1ULL << 63;
int i;
/* Pick any two starting points */
a = mwc64() % 99;
b = mwc64() % 99;
/* Iterate until we approach overflow */
for (i = 0; (i < 64) && !((a | b) & mask); i++) {
/* Find nth term */
register uint64_t c = a + b;
a = b;
b = c;
}
/* And we have the golden ratio */
phi = (long double)b / (long double)a;
if ((g_opt_flags & OPT_FLAGS_VERIFY) &&
(fabsl(phi - phi_) > precision))
pr_fail("%s: Golden Ratio phi not accurate enough\n",
name);
}
/*
* fft_partial()
* partial Fast Fourier Transform
*/
static void HOT OPTIMIZE3 fft_partial(
double complex *data,
double complex *tmp,
const int n,
const int m)
{
if (m < n) {
const int m2 = m * 2;
int i;
fft_partial(tmp, data, n, m2);
fft_partial(tmp + m, data + m, n, m2);
for (i = 0; i < n; i += m2) {
const double complex negI = -I;
double complex v = tmp[i];
double complex t =
cexp((negI * M_PI * (double)i) /
(double)n) * tmp[i + m];
data[i / 2] = v + t;
data[(i + n) / 2] = v - t;
}
}
}
/*
* stress_cpu_fft()
* Fast Fourier Transform
*/
static void HOT TARGET_CLONES stress_cpu_fft(const char *name)
{
double complex buf[FFT_SIZE], tmp[FFT_SIZE];
int i;
(void)name;
for (i = 0; i < FFT_SIZE; i++)
buf[i] = (double complex)(i % 63);
(void)memcpy(tmp, buf, sizeof(*tmp) * FFT_SIZE);
fft_partial(buf, tmp, FFT_SIZE, 1);
}
/*
* stress_cpu_euler()
* compute e using series
*/
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_euler(const char *name)
{
long double e = 1.0L, last_e;
long double fact = 1.0L;
long double precision = 1.0e-20L;
int n = 1;
do {
last_e = e;
fact *= n;
n++;
e += (1.0L / fact);
} while ((n < 25) && (fabsl(e - last_e) > precision));
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (n >= 25))
pr_fail("%s: Euler computation took more iterations "
"than expected\n", name);
}
/*
* random_buffer()
* fill a uint8_t buffer full of random data
* buffer *must* be multiple of 4 bytes in size
*/
static void random_buffer(uint8_t *data, const size_t len)
{
size_t i;
for (i = 0; i < len / 4; i++) {
uint32_t v = mwc32();
*data++ = v;
v >>= 8;
*data++ = v;
v >>= 8;
*data++ = v;
v >>= 8;
*data++ = v;
}
}
/*
* stress_cpu_hash_generic()
* stress test generic string hash function
*/
static void stress_cpu_hash_generic(
const char *name,
const char *hash_name,
uint32_t (*hash_func)(const char *str),
const uint32_t result)
{
char buffer[128];
size_t i;
uint32_t i_sum = 0;
MWC_SEED();
random_buffer((uint8_t *)buffer, sizeof(buffer));
/* Make it ASCII range ' '..'_' */
for (i = 0; i < sizeof(buffer); i++)
buffer[i] = (buffer[i] & 0x3f) + ' ';
for (i = sizeof(buffer) - 1; i; i--) {
buffer[i] = '\0';
i_sum += hash_func(buffer);
}
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i_sum != result))
pr_fail("%s: %s error detected, failed hash %s sum\n",
name, hash_name, hash_name);
}
/*
* jenkin()
* Jenkin's hash on random data
* http://www.burtleburtle.net/bob/hash/doobs.html
*/
static uint32_t HOT OPTIMIZE3 jenkin(const uint8_t *data, const size_t len)
{
register size_t i;
register uint32_t h = 0;
for (i = 0; i < len; i++) {
h += *data++;
h += h << 10;
h ^= h >> 6;
}
h += h << 3;
h ^= h >> 11;
h += h << 15;
return h;
}
/*
* stress_cpu_jenkin()
* multiple iterations on jenkin hash
*/
static void stress_cpu_jenkin(const char *name)
{
uint8_t buffer[128];
size_t i;
uint32_t i_sum = 0;
const uint32_t sum = 0x96673680;
MWC_SEED();
random_buffer(buffer, sizeof(buffer));
for (i = 0; i < sizeof(buffer); i++)
i_sum += jenkin(buffer, sizeof(buffer));
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i_sum != sum))
pr_fail("%s: jenkin error detected, failed hash "
"jenkin sum\n", name);
}
/*
* pjw()
* Hash a string, from Aho, Sethi, Ullman, Compiling Techniques.
*/
static uint32_t HOT OPTIMIZE3 pjw(const char *str)
{
register uint32_t h = 0;
while (*str) {
register uint32_t g;
h = (h << 4) + (*str);
if (0 != (g = h & 0xf0000000)) {
h = h ^ (g >> 24);
h = h ^ g;
}
str++;
}
return h;
}
/*
* stress_cpu_pjw()
* stress test hash pjw
*/
static void stress_cpu_pjw(const char *name)
{
stress_cpu_hash_generic(name, "pjw", pjw, 0xa89a91c0);
}
/*
* djb2a()
* Hash a string, from Dan Bernstein comp.lang.c (xor version)
*/
static uint32_t HOT OPTIMIZE3 djb2a(const char *str)
{
register uint32_t hash = 5381;
register int c;
while ((c = *str++)) {
/* (hash * 33) ^ c */
hash = ((hash << 5) + hash) ^ c;
}
return hash;
}
/*
* stress_cpu_djb2a()
* stress test hash djb2a
*/
static void stress_cpu_djb2a(const char *name)
{
stress_cpu_hash_generic(name, "djb2a", djb2a, 0x6a60cb5a);
}
/*
* fnv1a()
* Hash a string, using the improved 32 bit FNV-1a hash
*/
static uint32_t HOT OPTIMIZE3 fnv1a(const char *str)
{
register uint32_t hash = 5381;
const uint32_t fnv_prime = 16777619; /* 2^24 + 2^9 + 0x93 */
register int c;
while ((c = *str++)) {
hash ^= c;
hash *= fnv_prime;
}
return hash;
}
/*
* stress_cpu_fnv1a()
* stress test hash fnv1a
*/
static void HOT stress_cpu_fnv1a(const char *name)
{
stress_cpu_hash_generic(name, "fnv1a", fnv1a, 0x8ef17e80);
}
/*
* sdbm()
* Hash a string, using the sdbm data base hash and also
* apparently used in GNU awk.
*/
static uint32_t OPTIMIZE3 sdbm(const char *str)
{
register uint32_t hash = 0;
register int c;
while ((c = *str++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
/*
* stress_cpu_sdbm()
* stress test hash sdbm
*/
static void stress_cpu_sdbm(const char *name)
{
stress_cpu_hash_generic(name, "sdbm", sdbm, 0x46357819);
}
/*
* stress_cpu_idct()
* compute 8x8 Inverse Discrete Cosine Transform
*/
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_idct(const char *name)
{
const double invsqrt2 = 1.0 / sqrt(2.0);
const double pi_over_16 = M_PI / 16.0;
const int sz = 8;
int i, j, u, v;
float data[sz][sz], idct[sz][sz];
/*
* Set up DCT
*/
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
data[i][j] = (i + j == 0) ? 2040: 0;
}
}
for (i = 0; i < sz; i++) {
const double pi_i = (i + i + 1) * pi_over_16;
for (j = 0; j < sz; j++) {
const double pi_j = (j + j + 1) * pi_over_16;
double sum = 0.0;
for (u = 0; u < sz; u++) {
const double cos_pi_i_u = cos(pi_i * u);
for (v = 0; v < sz; v++) {
const double cos_pi_j_v =
cos(pi_j * v);
sum += (data[u][v] *
(u ? 1.0 : invsqrt2) *
(v ? 1.0 : invsqrt2) *
cos_pi_i_u * cos_pi_j_v);
}
}
idct[i][j] = 0.25 * sum;
}
}
/* Final output should be a 8x8 matrix of values 255 */
if (g_opt_flags & OPT_FLAGS_VERIFY) {
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
if ((int)idct[i][j] != 255) {
pr_fail("%s: IDCT error detected, "
"IDCT[%d][%d] was %d, "
"expecting 255\n",
name, i, j, (int)idct[i][j]);
}
}
if (!g_keep_stressing_flag)
return;
}
}
}
#define int_ops(a, b, c1, c2, c3) \
do { \
a += b; \
b ^= a; \
a >>= 1; \
b <<= 2; \
b -= a; \
a ^= ~0; \
b ^= ~(c1); \
a *= 3; \
b *= 7; \
a += 2; \
b -= 3; \
a /= 77; \
b /= 3; \
a <<= 1; \
b <<= 2; \
a |= 1; \
b |= 3; \
a *= mwc32(); \
b ^= mwc32(); \
a += mwc32(); \
b -= mwc32(); \
a /= 7; \
b /= 9; \
a |= (c2); \
b &= (c3); \
} while (0);
#define C1 (0xf0f0f0f0f0f0f0f0ULL)
#define C2 (0x1000100010001000ULL)
#define C3 (0xffeffffefebefffeULL)
/*
* Generic int stressor macro
*/
#define stress_cpu_int(_type, _sz, _a, _b, _c1, _c2, _c3) \
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_int ## _sz(const char *name)\
{ \
const _type mask = ~0; \
const _type a_final = _a; \
const _type b_final = _b; \
const _type c1 = _c1 & mask; \
const _type c2 = _c2 & mask; \
const _type c3 = _c3 & mask; \
register _type a, b; \
int i; \
\
MWC_SEED(); \
a = mwc32(); \
b = mwc32(); \
\
for (i = 0; i < 1000; i++) { \
int_ops(a, b, c1, c2, c3) \
} \
\
if ((g_opt_flags & OPT_FLAGS_VERIFY) && \
((a != a_final) || (b != b_final))) \
pr_fail("%s: int" # _sz " error detected, " \
"failed int" # _sz \
" math operations\n", name); \
} \
/* For compilers that support int128 .. */
#if defined(STRESS_INT128)
#define _UINT128(hi, lo) ((((__uint128_t)hi << 64) | (__uint128_t)lo))
stress_cpu_int(__uint128_t, 128,
_UINT128(0x132af604d8b9183a,0x5e3af8fa7a663d74),
_UINT128(0x62f086e6160e4e,0xd84c9f800365858),
_UINT128(C1, C1), _UINT128(C2, C2), _UINT128(C3, C3))
#endif
stress_cpu_int(uint64_t, 64, \
0x013f7f6dc1d79197cULL, 0x01863d2c6969a51ceULL,
C1, C2, C3)
stress_cpu_int(uint32_t, 32, \
0x1ce9b547UL, 0xa24b33aUL,
C1, C2, C3)
stress_cpu_int(uint16_t, 16, \
0x1871, 0x07f0,
C1, C2, C3)
stress_cpu_int(uint8_t, 8, \
0x12, 0x1a,
C1, C2, C3)
#define float_ops(_type, a, b, c, d, _sin, _cos) \
do { \
a = a + b; \
b = a * c; \
c = a - b; \
d = a / b; \
a = c / (_type)0.1923L; \
b = c + a; \
c = b * (_type)3.12L; \
d = d + b + (_type)_sin(a); \
a = (b + c) / c; \
b = b * c; \
c = c + (_type)1.0L; \
d = d - (_type)_sin(c); \
a = a * (_type)_cos(b); \
b = b + (_type)_cos(c); \
c = (_type)_sin(a + b) / (_type)2.344L; \
b = d - (_type)1.0L; \
} while (0)
/*
* Generic floating point stressor macro
*/
#define stress_cpu_fp(_type, _name, _sin, _cos) \
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_ ## _name(const char *name)\
{ \
int i; \
_type a = 0.18728L, b = mwc32(), c = mwc32(), d;\
\
(void)name; \
\
for (i = 0; i < 1000; i++) { \
float_ops(_type, a, b, c, d, \
_sin, _cos); \
} \
double_put(a + b + c + d); \
}
stress_cpu_fp(float, float, sinf, cosf)
stress_cpu_fp(double, double, sin, cos)
stress_cpu_fp(long double, longdouble, sinl, cosl)
#if defined(HAVE_FLOAT_DECIMAL32) && !defined(__clang__)
stress_cpu_fp(_Decimal32, decimal32, sinf, cosf)
#endif
#if defined(HAVE_FLOAT_DECIMAL64) && !defined(__clang__)
stress_cpu_fp(_Decimal64, decimal64, sin, cos)
#endif
#if defined(HAVE_FLOAT_DECIMAL128) && !defined(__clang__)
stress_cpu_fp(_Decimal128, decimal128, sinl, cosl)
#endif
#if defined(HAVE_FLOAT16) && !defined(__clang__)
stress_cpu_fp(__fp16, float16, sin, cos)
#endif
#if defined(HAVE_FLOAT32) && !defined(__clang__)
stress_cpu_fp(_Float32, float32, sin, cos)
#endif
#if defined(HAVE_FLOAT80) && !defined(__clang__)
stress_cpu_fp(__float80, float80, sinl, cosl)
#endif
#if defined(HAVE_FLOAT128) && !defined(__clang__)
stress_cpu_fp(__float128, float128, sinl, cosl)
#endif
/* Append floating point literal specifier to literal value */
#define FP(val, ltype) val ## ltype
#if defined(__STDC_IEC_559_COMPLEX__)
/*
* Generic complex stressor macro
*/
#define stress_cpu_complex(_type, _ltype, _name, _csin, _ccos) \
static void HOT OPTIMIZE3 TARGET_CLONES stress_cpu_ ## _name(const char *name)\
{ \
int i; \
_type cI = I; \
_type a = FP(0.18728, _ltype) + \
cI * FP(0.2762, _ltype), \
b = mwc32() - cI * FP(0.11121, _ltype),\
c = mwc32() + cI * mwc32(), d; \
\
(void)name; \
\
for (i = 0; i < 1000; i++) { \
float_ops(_type, a, b, c, d, \
_csin, _ccos); \
} \
double_put(a + b + c + d); \
}
stress_cpu_complex(complex float, f, complex_float, csinf, ccosf)
stress_cpu_complex(complex double, , complex_double, csin, ccos)
stress_cpu_complex(complex long double, l, complex_long_double, csinl, ccosl)
#endif /* __STDC_IEC_559_COMPLEX__ */
#define int_float_ops(_ftype, flt_a, flt_b, flt_c, flt_d, \
_sin, _cos, int_a, int_b, _c1, _c2, _c3) \
do { \
int_a += int_b; \
int_b ^= int_a; \
flt_a = flt_a + flt_b; \
int_a >>= 1; \
int_b <<= 2; \
flt_b = flt_a * flt_c; \
int_b -= int_a; \
int_a ^= ~0; \
flt_c = flt_a - flt_b; \
int_b ^= ~(_c1); \
int_a *= 3; \
flt_d = flt_a / flt_b; \
int_b *= 7; \
int_a += 2; \
flt_a = flt_c / (_ftype)0.1923L; \
int_b -= 3; \
int_a /= 77; \
flt_b = flt_c + flt_a; \
int_b /= 3; \
int_a <<= 1; \
flt_c = flt_b * (_ftype)3.12L; \
int_b <<= 2; \
int_a |= 1; \
flt_d = flt_d + flt_b + (_ftype)_sin(flt_a); \
int_b |= 3; \
int_a *= mwc32(); \
flt_a = (flt_b + flt_c) / flt_c; \
int_b ^= mwc32(); \
int_a += mwc32(); \
flt_b = flt_b * flt_c; \
int_b -= mwc32(); \
int_a /= 7; \
flt_c = flt_c + (_ftype)1.0L; \
int_b /= 9; \
flt_d = flt_d - (_ftype)_sin(flt_c); \
int_a |= (_c2); \
flt_a = flt_a * (_ftype)_cos(flt_b); \
flt_b = flt_b + (_ftype)_cos(flt_c); \
int_b &= (_c3); \
flt_c = (_ftype)_sin(flt_a + flt_b) / (_ftype)2.344L; \
flt_b = flt_d - (_ftype)1.0L; \
} while (0)