-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
stress-cpu.c
3215 lines (2852 loc) · 77.9 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-2021 Canonical, Ltd.
* Copyright (C) 2022-2024 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#include "core-arch.h"
#include "core-attribute.h"
#include "core-bitops.h"
#include "core-builtin.h"
#include "core-cpu.h"
#include "core-pragma.h"
#include "core-put.h"
#include "core-target-clones.h"
#include <math.h>
#include <sched.h>
#include <time.h>
#if defined(HAVE_SYS_SELECT_H)
#include <sys/select.h>
#endif
#if defined(HAVE_COMPLEX_H)
#include <complex.h>
#endif
/*
* S390x on QEMU (and maybe H/W) trips SIGILL for decimal
* math with some compilers, so disable this for now
*/
#if defined(STRESS_ARCH_S390)
#undef HAVE_Decimal32
#undef HAVE_Decimal64
#undef HAVE_Decimal128
#endif
#define GAMMA (0.57721566490153286060651209008240243104215933593992L)
#define OMEGA (0.56714329040978387299996866221035554975381578718651L)
#define PSI (3.35988566624317755317201130291892717968890513373197L)
#define PI (3.14159265358979323846264338327950288419716939937511L)
#define STATS_MAX (250)
#define FFT_SIZE (4096)
#define STRESS_CPU_DITHER_X (1024)
#define STRESS_CPU_DITHER_Y (768)
#define MATRIX_PROD_SIZE (128)
#define CORRELATE_DATA_LEN (8192)
#define CORRELATE_LEN (CORRELATE_DATA_LEN / 16)
#define SIEVE_SIZE (104730)
/* do nothing */
#if defined(HAVE_ASM_NOP)
#define FORCE_DO_NOTHING() stress_asm_nop()
#elif defined(HAVE_ASM_NOTHING)
#define FORCE_DO_NOTHING() stress_asm_nothing()
#else
#define FORCE_DO_NOTHING() while (0)
#endif
#if defined(HAVE_INT128_T)
#define STRESS_UINT128(hi, lo) ((((__uint128_t)hi << 64) | (__uint128_t)lo))
#endif
/*
* the CPU stress test has different classes of cpu stressor
*/
typedef int (*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 */
const double bogo_op_rate; /* normalizing bogo-ops rate */
} stress_cpu_method_info_t;
static const stress_help_t help[] = {
{ "c N", "cpu N", "start N workers that perform CPU only loading" },
{ "l P", "cpu-load P", "load CPU by P %, 0=sleep, 100=full load (see -c)" },
{ NULL, "cpu-load-slice S", "specify time slice during busy load" },
{ NULL, "cpu-method M", "specify stress cpu method M, default is all" },
{ NULL, "cpu-old-metrics", "use old CPU metrics instead of normalized metrics" },
{ NULL, "cpu-ops N", "stop after N cpu bogo operations" },
{ NULL, NULL, NULL }
};
static const stress_cpu_method_info_t stress_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];
/*
* stress_cpu_sqrt()
* stress CPU on square roots
*/
static int TARGET_CLONES stress_cpu_sqrt(const char *name)
{
int i;
for (i = 0; i < 16384; i++) {
const uint64_t rnd = stress_mwc32();
double r_d = shim_sqrt((double)rnd) * shim_sqrt((double)rnd);
long double r_ld = shim_sqrtl((long double)rnd) * shim_sqrtl((long double)rnd);
register uint64_t tmp;
r_d = shim_rint(r_d);
tmp = (uint64_t)r_d;
if (UNLIKELY((g_opt_flags & OPT_FLAGS_VERIFY) && (tmp != rnd))) {
pr_fail("%s: sqrt error detected on "
"sqrt(%" PRIu64 ")\n", name, rnd);
return EXIT_FAILURE;
}
r_ld = shim_rintl(r_ld);
tmp = (uint64_t)r_ld;
if (UNLIKELY((g_opt_flags & OPT_FLAGS_VERIFY) && (tmp != rnd))) {
pr_fail("%s: sqrtf error detected on "
"sqrt(%" PRIu64 ")\n", name, rnd);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
static bool stress_is_affinity_set(void)
{
#if defined(HAVE_SCHED_GETAFFINITY)
cpu_set_t mask;
int i;
const int cpus_online = (int)stress_get_processors_online();
CPU_ZERO(&mask);
if (sched_getaffinity(0, sizeof(mask), &mask) < 0)
return false; /* Can't tell, so assume not */
/*
* If any of the CPU affinities across all the CPUs
* are zero then we know the stressor as been pinned
* to some CPUs and not to others, so affinity has been
* set which can lead to load balancing difficulties
*/
for (i = 0; i < cpus_online; i++) {
if (!CPU_ISSET(i, &mask))
return true;
}
return false;
#else
return false; /* Don't know, so assume not */
#endif
}
/*
* stress_cpu_loop()
* simple CPU busy loop
*/
static int OPTIMIZE0 stress_cpu_loop(const char *name)
{
uint32_t i, i_sum = 0;
const uint32_t sum = 134209536UL;
for (i = 0; LIKELY(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);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_gcd()
* compute Greatest Common Divisor
*/
static int OPTIMIZE3 TARGET_CLONES stress_cpu_gcd(const char *name)
{
uint32_t i, gcd_sum = 0;
const uint32_t gcd_checksum = 63000868UL;
uint64_t lcm_sum = 0;
const uint64_t lcm_checksum = 41637399273ULL;
for (i = 0; i < 16384; i++) {
register uint32_t a = i, b = i % (3 + (1997 ^ i));
register uint64_t lcm = ((uint64_t)a * b);
while (b != 0) {
register const uint32_t r = b;
b = a % b;
a = r;
}
if (a)
lcm_sum += (lcm / a);
gcd_sum += a;
FORCE_DO_NOTHING();
}
if ((g_opt_flags & OPT_FLAGS_VERIFY) &&
(gcd_sum != gcd_checksum) &&
(lcm_sum != lcm_checksum)) {
pr_fail("%s: gcd error detected, failed modulo "
"or assignment operations\n", name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_bitops()
* various bit manipulation hacks from bithacks
* https://graphics.stanford.edu/~seander/bithacks.html
*/
static int OPTIMIZE3 TARGET_CLONES stress_cpu_bitops(const char *name)
{
uint32_t i, i_sum = 0;
const uint32_t sum = 0x8aac4aab;
for (i = 0; i < 16384; i++) {
i_sum += stress_bitreverse32(i);
i_sum += stress_parity32(i);
i_sum += stress_popcount32(i);
i_sum += stress_nextpwr2(i);
}
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i_sum != sum)) {
pr_fail("%s: bitops error detected, failed "
"bitops operations\n", name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_trig()
* simple sin, cos trig functions
*/
static int OPTIMIZE_FAST_MATH stress_cpu_trig(const char *name)
{
int i;
long double d_sum = 0.0L;
(void)name;
for (i = 0; i < 1500; i++) {
const long double theta = (2.0L * PI * (long double)i) / 1500.0L;
{
const double thetad = (double)theta;
const float thetaf = (float)theta;
d_sum += (shim_cosl(theta) * shim_sinl(theta));
d_sum += ((long double)shim_cos(thetad) * (long double)shim_sin(thetad));
d_sum += ((long double)shim_cosf(thetaf) * (long double)shim_sinf(thetaf));
}
{
const long double thetal = theta * 2.0L;
const double thetad = (double)thetal;
const float thetaf = (float)thetal;
d_sum += shim_cosl(thetal);
d_sum += (long double)shim_cos(thetad);
d_sum += (long double)shim_cosf(thetaf);
}
{
const long double thetal = theta * 3.0L;
const double thetad = (double)thetal;
const float thetaf = (float)thetal;
d_sum += shim_sinl(thetal);
d_sum += (long double)shim_sin(thetad);
d_sum += (long double)shim_sinf(thetaf);
}
}
stress_long_double_put(d_sum);
return EXIT_SUCCESS;
}
/*
* stress_cpu_hyperbolic()
* simple hyperbolic sinh, cosh functions
*/
static int OPTIMIZE_FAST_MATH stress_cpu_hyperbolic(const char *name)
{
int i;
long double d_sum = 0.0L;
(void)name;
for (i = 0; i < 1500; i++) {
const long double theta = (2.0L * PI * (long double)i) / 1500.0L;
{
const double thetad = (double)theta;
const float thetaf = (float)theta;
d_sum += (shim_coshl(theta) * shim_sinhl(theta));
d_sum += ((long double)cosh(thetad) * (long double)shim_sinh(thetad));
d_sum += ((long double)coshf(thetaf) * (long double)shim_sinhf(thetaf));
}
{
const long double thetal = theta * 2.0L;
const double thetad = (double)theta;
const float thetaf = (float)theta;
d_sum += shim_coshl(thetal);
d_sum += (long double)shim_cosh(thetad);
d_sum += (long double)shim_coshf(thetaf);
}
{
const long double thetal = theta * 3.0L;
const double thetad = (double)theta;
const float thetaf = (float)theta;
d_sum += shim_sinhl(thetal);
d_sum += (long double)shim_sinh(thetad);
d_sum += (long double)shim_sinhf(thetaf);
}
}
stress_long_double_put(d_sum);
return EXIT_SUCCESS;
}
/*
* stress_cpu_rand()
* generate lots of pseudo-random integers
*/
static int OPTIMIZE3 stress_cpu_rand(const char *name)
{
int i;
uint32_t i_sum = 0;
const uint32_t sum = 0xc253698c;
stress_mwc_seed();
PRAGMA_UNROLL_N(8)
for (i = 0; LIKELY(i < 16384); i++)
i_sum += stress_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);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_logmap()
* Compute logistic map of x = r * x * (x - 1.0) where r is the
* accumulation point based on A098587. Data is scaled in the
* range 0..255
*/
static int OPTIMIZE3 stress_cpu_logmap(const char *name)
{
static double x = 0.4;
/*
* Use an accumulation point that is slightly larger
* than the point where chaotic behaviour starts
*/
const double r = 3.569945671870944901842L * 1.0999999L;
register int i;
(void)name;
PRAGMA_UNROLL_N(8)
for (i = 0; i < 16384; i++) {
/*
* Scale up a fractional part of x
* by an arbitrary value and take
* the bottom 8 bits of the result
* to make a quasi-chaotic random-ish
* value
*/
x = x * r * (1.0 - x);
}
stress_double_put(x);
return EXIT_SUCCESS;
}
#if defined(HAVE_SRAND48) && \
defined(HAVE_LRAND48) && \
defined(HAVE_DRAND48)
/*
* Some libc's such as OpenBSD don't implement rand48 the same
* as linux's glibc, so beware of different implementations with
* the verify mode checking.
*/
#if defined(__linux__) && \
defined(__GLIBC__)
#define STRESS_CPU_RAND48_VERIFY
#endif
/*
* stress_cpu_rand48()
* generate random values using rand48 family of functions
*/
static int OPTIMIZE3 stress_cpu_rand48(const char *name)
{
int i;
double d = 0;
long long int l = 0;
#if defined(STRESS_CPU_RAND48_VERIFY)
const double d_expected_sum = 8184.618041L;
const long long int l_expected_sum = 17522760427916;
#endif
(void)name;
srand48(0x0defaced);
for (i = 0; LIKELY(i < 16384); i++) {
d += drand48();
l += lrand48();
}
#if defined(STRESS_CPU_RAND48_VERIFY)
if (g_opt_flags & OPT_FLAGS_VERIFY) {
double d_error = d - d_expected_sum;
if (shim_fabs(d_error) > 0.0001) {
pr_fail("%s: drand48 error detected, failed sum\n", name);
return EXIT_FAILURE;
}
if (l != l_expected_sum) {
pr_fail("%s: lrand48 error detected, failed sum\n", name);
return EXIT_FAILURE;
}
}
#endif
stress_double_put(d);
stress_uint64_put((uint64_t)l);
return EXIT_SUCCESS;
}
#endif
/*
* stress_cpu_lfsr32()
* generate 16384 values from the Galois polynomial
* x^32 + x^31 + x^29 + x + 1
*/
static int OPTIMIZE3 stress_cpu_lfsr32(const char *name)
{
static uint32_t lfsr = 0xf63acb01;
register int i;
(void)name;
PRAGMA_UNROLL_N(8)
for (i = 0; LIKELY(i < 16384); i++) {
lfsr = (lfsr >> 1) ^ (unsigned int)(-(lfsr & 1u) & 0xd0000001U);
}
stress_uint32_put(lfsr);
return EXIT_SUCCESS;
}
/*
* stress_cpu_nsqrt()
* iterative Newton-Raphson square root
*/
static int 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 = 16300; i < 16384; i++) {
const long double n = (long 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)) {
const 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) {
const long double r2 = shim_rintl(rt * rt);
if (j >= max_iter) {
pr_fail("%s: Newton-Raphson sqrt "
"computation took more iterations "
"than expected\n", name);
return EXIT_FAILURE;
}
if ((int)r2 != i) {
pr_fail("%s: Newton-Raphson sqrt not "
"accurate enough\n", name);
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_phi()
* compute the Golden Ratio
*/
static int 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 + shim_sqrtl(5.0L)) / 2.0L;
register uint64_t a, b;
const uint64_t mask = 1ULL << 63;
int i;
/* Pick any two starting points */
a = stress_mwc64modn(99);
b = stress_mwc64modn(99);
/* Iterate until we approach overflow */
for (i = 0; (i < 64) && !((a | b) & mask); i++) {
/* Find nth term */
register const 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) &&
(shim_fabsl(phi - phi_) > precision)) {
pr_fail("%s: Golden Ratio phi not accurate enough\n",
name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_apery()
* compute Apéry's constant
*/
static int OPTIMIZE3 stress_cpu_apery(const char *name)
{
uint32_t n;
long double a = 0.0L, a_ = a;
const long double precision = 1.0e-14L;
(void)name;
for (n = 1; LIKELY(n < 100000); n++) {
register long double n3 = (long double)n;
a_ = a;
n3 = n3 * n3 * n3;
a += (1.0L / n3);
if (shim_fabsl(a - a_) < precision)
break;
}
if (shim_fabsl(a - a_) > precision) {
pr_fail("%s: Apéry's constant not accurate enough\n", name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#if defined(HAVE_COMPLEX_H) && \
defined(HAVE_COMPLEX) && \
defined(__STDC_IEC_559_COMPLEX__) &&\
!defined(__UCLIBC__)
/*
* fft_partial()
* partial Fast Fourier Transform
*/
static void OPTIMIZE3 fft_partial(
double complex * RESTRICT data,
double complex * RESTRICT 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 = -(double complex)I;
register double complex v = tmp[i];
register double complex t =
shim_cexp((negI * (double)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 int TARGET_CLONES stress_cpu_fft(const char *name)
{
static double complex buf[FFT_SIZE] ALIGN64, tmp[FFT_SIZE] ALIGN64;
int i;
(void)name;
for (i = 0; i < FFT_SIZE; i++)
buf[i] = (double complex)(i % 63);
(void)shim_memcpy(tmp, buf, sizeof(*tmp) * FFT_SIZE);
fft_partial(buf, tmp, FFT_SIZE, 1);
return EXIT_SUCCESS;
}
#else
UNEXPECTED
#endif
/*
* stress_cpu_euler()
* compute e using series
*/
static int OPTIMIZE3 TARGET_CLONES stress_cpu_euler(const char *name)
{
long double e = 1.0L, last_e;
long double fact = 1.0L;
const long double precision = 1.0e-20L;
int n = 1;
do {
last_e = e;
fact *= n;
n++;
e += (1.0L / fact);
} while ((n < 25) && (shim_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);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* 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++) {
register uint32_t v = stress_mwc32();
*data++ = (uint8_t)v;
v >>= 8;
*data++ = (uint8_t)v;
v >>= 8;
*data++ = (uint8_t)v;
v >>= 8;
*data++ = (uint8_t)v;
}
}
/*
* stress_cpu_collatz()
* stress test integer collatz conjecture
*/
static int OPTIMIZE3 TARGET_CLONES stress_cpu_collatz(const char *name)
{
register uint64_t n = 989345275647ULL; /* Has 1348 steps in cycle */
register uint64_t s = stress_mwc8();
register int i;
/*
* We need to put in the accumulation of s to force the compiler
* to generate code that does the following computation at run time.
*/
for (i = 0; n != 1; i++) {
n = (n & 1) ? (3 * n) + 1 : n / 2;
s += n; /* Force compiler to do iterative computations */
}
stress_uint64_put(s);
if ((g_opt_flags & OPT_FLAGS_VERIFY) && (i != 1348)) {
pr_fail("%s: error detected, failed collatz progression\n",
name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* stress_cpu_idct()
* compute 8x8 Inverse Discrete Cosine Transform
*/
static int OPTIMIZE3 TARGET_CLONES stress_cpu_idct(const char *name)
{
#define IDCT_SIZE (8)
const double invsqrt2 = 1.0 / shim_sqrt(2.0);
const double pi_over_16 = (double)PI / 16.0;
int i, j, u, v;
float data[IDCT_SIZE][IDCT_SIZE];
float idct[IDCT_SIZE][IDCT_SIZE];
/*
* Set up DCT
*/
for (i = 0; i < IDCT_SIZE; i++) {
PRAGMA_UNROLL_N(8)
for (j = 0; j < IDCT_SIZE; j++) {
data[i][j] = (i + j == 0) ? 2040: 0;
}
}
for (i = 0; i < IDCT_SIZE; i++) {
const double pi_i = (i + i + 1) * pi_over_16;
for (j = 0; j < IDCT_SIZE; j++) {
const double pi_j = (j + j + 1) * pi_over_16;
double sum = 0.0;
for (u = 0; u < IDCT_SIZE; u++) {
const double cos_pi_i_u = shim_cos(pi_i * u);
const double tmp = cos_pi_i_u * (u ? 1.0 : invsqrt2);
for (v = 0; v < IDCT_SIZE; v++) {
const double cos_pi_j_v = shim_cos(pi_j * v);
sum += (double)data[u][v] *
(v ? 1.0 : invsqrt2) * cos_pi_j_v * tmp;
}
}
idct[i][j] = (float)(0.25 * sum);
}
}
/* Final output should be a 8x8 matrix of values 255 */
if (g_opt_flags & OPT_FLAGS_VERIFY) {
for (i = 0; i < IDCT_SIZE; i++) {
for (j = 0; j < IDCT_SIZE; 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]);
return EXIT_FAILURE;
}
}
if (!stress_continue_flag())
return EXIT_SUCCESS;
}
}
#undef IDCT_SIZE
return EXIT_SUCCESS;
}
#define int_ops(_type, a, b, c1, c2, c3)\
do { \
a += b; \
b ^= a; \
a >>= 1; \
b <<= 2; \
b -= a; \
a ^= (_type)~0; \
b ^= ~(c1); \
a *= 3; \
b *= 7; \
a += 2; \
b -= 3; \
a /= 77; \
b /= 3; \
a <<= 1; \
b <<= 2; \
a |= 1; \
b |= 3; \
a *= stress_mwc32(); \
b ^= stress_mwc32(); \
a += stress_mwc32(); \
b -= stress_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 int OPTIMIZE3 TARGET_CLONES stress_cpu_int ## _sz(const char *name)\
{ \
const _type mask = (_type)~(_type)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; \
\
stress_mwc_seed(); \
a = (_type)stress_mwc32(); \
b = (_type)stress_mwc32(); \
\
for (i = 0; i < 1000; i++) { \
int_ops(_type, 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); \
return EXIT_FAILURE; \
} \
return EXIT_SUCCESS; \
} \
/* For compilers that support int128 .. */
#if defined(HAVE_INT128_T)
stress_cpu_int(__uint128_t, 128,
STRESS_UINT128(0x132af604d8b9183a,0x5e3af8fa7a663d74),
STRESS_UINT128(0x62f086e6160e4e,0xd84c9f800365858),
STRESS_UINT128(C1, C1), STRESS_UINT128(C2, C2), STRESS_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_thresh(x, _type) x = (_type) \
((shim_fabs((double)x) > 1.0) ? \
((_type)(0.1 + (double)x - (double)(long)x)) : \
((_type)(x)))
#define float_ops(_type, a, b, c, d, _sin, _cos) \
do { \
a = a + b; \
b = a * c; \
c = a - b; \
d = a / (_type)8.1; \
float_thresh(d, _type); \
a = c / (_type)5.1923; \
float_thresh(a, _type); \
float_thresh(c, _type); \
b = c + a; \
c = b * (_type)_sin(b); \
d = d + b + (_type)_sin(a); \
a = (_type)_cos(b + c); \
b = b * c; \
c = c + (_type)1.5; \
d = d - (_type)_sin(c); \
a = a * (_type)_cos(b); \
b = b + (_type)_cos(c); \
c = (_type)_sin(a + b) / (_type)2.344; \
b = d - (_type)0.5; \
} while (0)
/*
* Generic floating point stressor macro
*/
#define stress_cpu_fp(_type, _name, _sin, _cos) \
static int OPTIMIZE3 TARGET_CLONES stress_cpu_ ## _name(const char *name)\
{ \
int i; \
const uint32_t r1 = stress_mwc32(), \
r2 = stress_mwc32(); \
_type a = (_type)0.18728L, \
b = (_type)((double)r1 / 65536.0), \
c = (_type)((double)r2 / 65536.0), \
d = (_type)0.0, \
r; \
\
(void)name; \
\
for (i = 0; i < 1000; i++) { \
float_ops(_type, a, b, c, d, \
_sin, _cos); \
} \
r = a + b + c + d; \
stress_double_put((double)r); \
return EXIT_SUCCESS; \
}
stress_cpu_fp(float, float, shim_sinf, shim_cosf)
stress_cpu_fp(double, double, shim_sin, shim_cos)
stress_cpu_fp(long double, longdouble, shim_sinl, shim_cosl)
#if defined(HAVE_Decimal32) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(_Decimal32, decimal32, shim_sinf, shim_cosf)
#endif
#if defined(HAVE_Decimal64) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(_Decimal64, decimal64, shim_sin, shim_cos)
#endif
#if defined(HAVE_Decimal128) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(_Decimal128, decimal128, shim_sinl, shim_cosl)
#endif
#if defined(HAVE_fp16) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(__fp16, float16, shim_sin, shim_cos)
#endif
#if defined(HAVE_Float32) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(_Float32, float32, shim_sin, shim_cos)
#endif
#if defined(HAVE_Float64) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(_Float64, float64, shim_sin, shim_cos)
#endif
#if defined(HAVE__float80) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(__float80, float80, shim_sinl, shim_cosl)
#endif
#if defined(HAVE__float128) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(__float128, float128, shim_sinl, shim_cosl)
#elif defined(HAVE_Float128) && \
!defined(HAVE_COMPILER_CLANG)
stress_cpu_fp(_Float128, float128, shim_sinl, shim_cosl)
#endif
/* Append floating point literal specifier to literal value */
#define FP(val, ltype) val ## ltype
#if defined(HAVE_COMPLEX_H) && \
defined(HAVE_COMPLEX) && \
defined(__STDC_IEC_559_COMPLEX__) &&\
!defined(__UCLIBC__)
static inline void stress_cpu_complex_float_put(complex float v)
{
stress_float_put((float)(v * v));
}
static inline void stress_cpu_complex_double_put(complex double v)
{
stress_double_put((double)(v * v));
}
static inline void stress_cpu_complex_long_double_put(complex long double v)
{
stress_long_double_put((long double)(v * v));
}
/*
* Generic complex stressor macro
*/
#define stress_cpu_complex(_type, _ltype, _name, _csin, _ccos, _put) \
static int OPTIMIZE3 TARGET_CLONES stress_cpu_ ## _name(const char *name) \
{ \
int i; \
const uint32_t r1 = stress_mwc32(), \
r2 = stress_mwc32(); \
_type cI = (_type)I; \
_type a = FP(0.18728, _ltype) + \
cI * FP(0.2762, _ltype), \
b = (_type)((double)r1/(double)(1UL<<31)) - cI * FP(0.11121, _ltype), \
c = (_type)((double)r2/(double)(1UL<<31)) + cI * stress_mwc32(), \
d = (_type)0.5, \
r; \
\
(void)name; \
\
for (i = 0; i < 1000; i++) { \
float_ops(_type, a, b, c, d, _csin, _ccos); \
} \
r = a + b + c + d; \
_put(r); \