-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbftest.c
2074 lines (1916 loc) · 56.1 KB
/
bftest.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
/*
* Tiny arbitrary precision floating point library tests
*
* Copyright (c) 2017 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <getopt.h>
#include <sys/time.h>
#include <gmp.h>
#include <mpfr.h>
#include "libbf.h"
#include "cutils.h"
#include "softfp.h"
#include "mpdecimal.h"
typedef enum {
/* low level operations */
BF_OP_MP_SQRTREM,
BF_OP_MP_RECIP,
/* binary floating point */
BF_OP_MUL,
BF_OP_ADD,
BF_OP_SUB,
BF_OP_RINT,
BF_OP_ROUND,
BF_OP_CMP_EQ,
BF_OP_CMP_LT,
BF_OP_CMP_LE,
BF_OP_DIV,
BF_OP_FMOD,
BF_OP_REM,
BF_OP_SQRT,
BF_OP_OR,
BF_OP_XOR,
BF_OP_AND,
BF_OP_CAN_ROUND,
BF_OP_MUL_L2RADIX,
BF_OP_DIV_L2RADIX,
BF_OP_ATOF,
BF_OP_FTOA,
BF_OP_EXP,
BF_OP_LOG,
BF_OP_COS,
BF_OP_SIN,
BF_OP_TAN,
BF_OP_ATAN,
BF_OP_ATAN2,
BF_OP_ASIN,
BF_OP_ACOS,
BF_OP_POW,
/* decimal floating point */
BF_OP_ADD_DEC,
BF_OP_MUL_DEC,
BF_OP_DIV_DEC,
BF_OP_SQRT_DEC,
BF_OP_FMOD_DEC,
BF_OP_DIVREM_DEC,
BF_OP_RINT_DEC,
BF_OP_COUNT,
} MPFTestOPEnum;
const char *op_str[BF_OP_COUNT] = {
"mp_sqrtrem",
"mp_recip",
"mul",
"add",
"sub",
"rint",
"round",
"cmp_eq",
"cmp_lt",
"cmp_le",
"div",
"fmod",
"rem",
"sqrt",
"or",
"xor",
"and",
"can_round",
"mul_l2radix",
"div_l2radix",
"atof",
"ftoa",
"exp",
"log",
"cos",
"sin",
"tan",
"atan",
"atan2",
"asin",
"acos",
"pow",
"add_dec",
"mul_dec",
"div_dec",
"sqrt_dec",
"fmod_dec",
"divrem_dec",
"rint_dec",
};
const char *rnd_str[7] = {
"N",
"Z",
"D",
"U",
"NA",
"A",
"F",
};
#define SPECIAL_COUNT 7
static bf_context_t bf_ctx;
static void *my_bf_realloc(void *opaque, void *ptr, size_t size)
{
return realloc(ptr, size);
}
int mp_cmp(const limb_t *taba, size_t na, const limb_t *tabb, size_t nb)
{
slimb_t n, i;
limb_t a, b;
n = na;
if (nb > n)
n = nb;
for(i = n - 1; i >= 0; i--) {
if (i < na)
a = taba[i];
else
a = 0;
if (i < nb)
b = tabb[i];
else
b = 0;
if (a != b) {
if (a < b)
return -1;
else
return 1;
}
}
return 0;
}
static void set_special(bf_t *a, int idx)
{
switch(idx) {
case 0:
bf_set_zero(a, 0);
break;
case 1:
bf_set_zero(a, 1); /* -0 */
break;
case 2:
bf_set_inf(a, 0);
break;
case 3:
bf_set_inf(a, 1);
break;
case 4:
bf_set_si(a, 1);
break;
case 5:
bf_set_si(a, -1);
break;
case 6:
bf_set_nan(a);
break;
default:
abort();
}
}
static void set_special_dec(bfdec_t *a, int idx)
{
switch(idx) {
case 0:
bfdec_set_zero(a, 0);
break;
case 1:
bfdec_set_zero(a, 1); /* -0 */
break;
case 2:
bfdec_set_inf(a, 0);
break;
case 3:
bfdec_set_inf(a, 1);
break;
case 4:
bfdec_set_si(a, 1);
break;
case 5:
bfdec_set_si(a, -1);
break;
case 6:
bfdec_set_nan(a);
break;
default:
abort();
}
}
typedef struct mp_randstate_t {
uint64_t val;
} mp_randstate_t;
void mp_randinit(mp_randstate_t *state, uint64_t seed)
{
state->val = seed;
}
static inline uint64_t mp_random64(mp_randstate_t *s)
{
s->val = s->val * 6364136223846793005 + 1;
/* avoid bad modulo properties
XXX: use mersenne twistter generator */
return (s->val << 32) | (s->val >> 32);
}
/* random number between 0 and 1 with large sequences of identical bits */
static void mp_rrandom(limb_t *tab, limb_t prec, mp_randstate_t *state)
{
slimb_t n, max_run_len, cur_len, j, len, bit_index, nb_bits;
int cur_state, m;
n = (prec + LIMB_BITS - 1) / LIMB_BITS;
/* same idea as GMP. It would be probably better to use a non
uniform law */
m = mp_random64(state) % 4 + 1;
max_run_len = bf_max(prec / m, 1);
cur_state = mp_random64(state) & 1;
cur_len = mp_random64(state) % max_run_len + 1;
nb_bits = n * LIMB_BITS;
memset(tab, 0, sizeof(limb_t) * n);
bit_index = nb_bits - prec;
while (bit_index < nb_bits) {
len = bf_min(cur_len, nb_bits - bit_index);
if (cur_state) {
/* XXX: inefficient */
for(j = 0; j < len; j++) {
tab[bit_index >> LIMB_LOG2_BITS] |= (limb_t)1 << (bit_index & (LIMB_BITS - 1));
bit_index++;
}
}
bit_index += len;
cur_len -= len;
if (cur_len == 0) {
cur_len = mp_random64(state) % max_run_len + 1;
cur_state ^= 1;
}
}
}
static void bf_rrandom(bf_t *a, limb_t prec, mp_randstate_t *state)
{
slimb_t n;
n = (prec + LIMB_BITS - 1) / LIMB_BITS;
bf_resize(a, n);
mp_rrandom(a->tab, prec, state);
a->sign = 0;
a->expn = 0;
bf_normalize_and_round(a, prec, BF_RNDZ);
}
static void bf_rrandom_large(bf_t *a, limb_t prec, mp_randstate_t *s)
{
limb_t prec1;
prec1 = mp_random64(s) % (2 * prec) + 1;
bf_rrandom(a, prec1, s);
a->sign = mp_random64(s) & 1;
}
/* random number between 0 and 1 with large sequences zeros, nines or
random digits */
static void bfdec_rrandom(bfdec_t *a, limb_t prec, mp_randstate_t *state)
{
slimb_t n, max_run_len, cur_len, j, len, digit_index, nb_digits;
int cur_state, m;
n = (prec + LIMB_DIGITS - 1) / LIMB_DIGITS;
bfdec_resize(a, n);
/* same idea as GMP. It would be probably better to use a non
uniform law */
m = mp_random64(state) % 4 + 1;
max_run_len = bf_max(prec / m, 1);
cur_state = mp_random64(state) % 3;
cur_len = mp_random64(state) % max_run_len + 1;
nb_digits = n * LIMB_DIGITS;
memset(a->tab, 0, sizeof(limb_t) * n);
digit_index = nb_digits - prec;
while (digit_index < nb_digits) {
len = bf_min(cur_len, nb_digits - digit_index);
switch(cur_state) {
case 0:
/* zeros */
break;
case 1:
/* nines */
for(j = 0; j < len; j++) {
a->tab[digit_index / LIMB_DIGITS] +=
9 * mp_pow_dec[digit_index % LIMB_DIGITS];
digit_index++;
}
break;
case 2:
/* random */
for(j = 0; j < len; j++) {
a->tab[digit_index / LIMB_DIGITS] +=
(mp_random64(state) % 10) *
mp_pow_dec[digit_index % LIMB_DIGITS];
digit_index++;
}
break;
}
digit_index += len;
cur_len -= len;
if (cur_len == 0) {
cur_len = mp_random64(state) % max_run_len + 1;
cur_state ^= 1;
}
}
a->sign = 0;
a->expn = 0;
bfdec_normalize_and_round(a, prec, BF_RNDZ);
}
static void bfdec_rrandom_large(bfdec_t *a, limb_t prec, mp_randstate_t *s)
{
limb_t prec1;
prec1 = mp_random64(s) % (2 * prec) + 1;
bfdec_rrandom(a, prec1, s);
a->sign = mp_random64(s) & 1;
}
/* random integer with 0 to prec bits */
static void bf_rrandom_int(bf_t *a, limb_t prec, mp_randstate_t *rnd_state)
{
limb_t prec1;
prec1 = mp_random64(rnd_state) % prec + 1;
bf_rrandom(a, prec1, rnd_state);
if (a->expn != BF_EXP_ZERO)
a->expn += prec1;
a->sign = mp_random64(rnd_state) & 1;
}
/* random integer with long sequences of '0' and '1' */
uint64_t rrandom_u(int len, mp_randstate_t *s)
{
int bit, pos, n, end;
uint64_t a;
bit = mp_random64(s) & 1;
pos = 0;
a = 0;
for(;;) {
n = (mp_random64(s) % len) + 1;
end = pos + n;
if (end > len)
end = len;
if (bit) {
n = end - pos;
a |= ((uint64_t)(1 << n) - 1) << pos;
}
if (end >= len)
break;
pos = end;
bit ^= 1;
}
return a;
}
#define F64_MANT_SIZE 52
#define F64_EXP_MASK ((1 << 11) - 1)
uint64_t rrandom_sf64(mp_randstate_t *s)
{
uint32_t a_exp, a_sign;
uint64_t a_mant;
a_sign = mp_random64(s) & 1;
/* generate exponent close to the min/max more often than random */
switch(mp_random64(s) & 15) {
case 0:
a_exp = (mp_random64(s) % (2 * F64_MANT_SIZE)) & F64_EXP_MASK;
break;
case 1:
a_exp = (F64_EXP_MASK - (mp_random64(s) % (2 * F64_MANT_SIZE))) & F64_EXP_MASK;
break;
default:
a_exp = mp_random64(s) & F64_EXP_MASK;
break;
}
a_mant = rrandom_u(F64_MANT_SIZE, s);
return ((uint64_t)a_sign << 63) | ((uint64_t)a_exp << F64_MANT_SIZE) | a_mant;
}
static int64_t get_clock_msec(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000LL + (tv.tv_usec / 1000);
}
static inline uint64_t get_cycles(void)
{
uint32_t low,high;
uint64_t val;
asm volatile("rdtsc" : "=a" (low), "=d" (high));
val = high;
val <<= 32;
val |= low;
return val;
}
static mpfr_rnd_t mpfr_get_rnd_mode(bf_rnd_t rnd_mode)
{
const mpfr_rnd_t rnd_mode_tab[] = {
MPFR_RNDN,
MPFR_RNDZ,
MPFR_RNDD,
MPFR_RNDU,
MPFR_RNDNA,
MPFR_RNDA,
};
return rnd_mode_tab[rnd_mode];
}
static void mpfr_to_bf(bf_t *r1, mpfr_t r)
{
char *str;
mpfr_asprintf(&str, "%Ra", r);
// printf("mpfr r=%s\n", str);
assert(bf_atof(r1, str, NULL, 16, BF_PREC_INF, BF_RNDZ) == 0);
mpfr_free_str(str);
}
static void bf_to_mpfr(mpfr_t a, const bf_t *a1)
{
char *str;
// bf_print_str("a", a1);
str = bf_ftoa(NULL, a1, 16, BF_PREC_INF, BF_RNDZ | BF_FTOA_FORMAT_FREE |
BF_FTOA_ADD_PREFIX);
// printf("mpfr a=%s\n", str);
mpfr_set_str(a, str, 0, MPFR_RNDZ);
free(str);
}
void mpfr_exec_init(void)
{
slimb_t e_max, e_min;
e_max = (limb_t)1 << (BF_EXP_BITS_MAX - 1);
e_min = -e_max + 3;
mpfr_set_emin(e_min);
mpfr_set_emax(e_max);
}
int mpfr_exec_op(MPFTestOPEnum op, bf_t *r1, bf_t *a1, bf_t *b1,
int64_t prec, int rnd_mode1, int64_t *pcycles)
{
mpfr_t a, b, r;
mpfr_rnd_t rnd_mode;
int ret, mpfr_ret;
mpfr_init2(a, bf_max(a1->len, 1) * LIMB_BITS);
mpfr_init2(b, bf_max(b1->len, 1) * LIMB_BITS);
if (op == BF_OP_RINT) {
/* infinite precision for rint */
mpfr_init2(r, bf_max(a1->len, 1) * LIMB_BITS);
} else {
mpfr_init2(r, prec);
}
bf_to_mpfr(a, a1);
bf_to_mpfr(b, b1);
rnd_mode = mpfr_get_rnd_mode(rnd_mode1);
ret = 0;
mpfr_ret = 0;
*pcycles -= get_cycles();
switch(op) {
case BF_OP_MUL:
mpfr_ret = mpfr_mul(r, a, b, rnd_mode);
break;
case BF_OP_ADD:
mpfr_ret = mpfr_add(r, a, b, rnd_mode);
break;
case BF_OP_SUB:
mpfr_ret = mpfr_sub(r, a, b, rnd_mode);
break;
case BF_OP_RINT:
mpfr_ret = mpfr_rint(r, a, rnd_mode);
break;
case BF_OP_ROUND:
mpfr_ret = mpfr_set(r, a, rnd_mode);
break;
case BF_OP_CMP_EQ:
ret = mpfr_equal_p(a, b);
break;
case BF_OP_CMP_LT:
ret = mpfr_less_p(a, b);
break;
case BF_OP_CMP_LE:
ret = mpfr_lessequal_p(a, b);
break;
case BF_OP_DIV:
mpfr_ret = mpfr_div(r, a, b, rnd_mode);
break;
case BF_OP_FMOD:
mpfr_ret = mpfr_fmod(r, a, b, rnd_mode);
break;
case BF_OP_REM:
mpfr_ret = mpfr_remainder(r, a, b, rnd_mode);
break;
case BF_OP_SQRT:
mpfr_ret = mpfr_sqrt(r, a, rnd_mode);
break;
case BF_OP_OR:
case BF_OP_XOR:
case BF_OP_AND:
{
mpz_t ai, bi;
mpz_init(ai);
mpz_init(bi);
mpfr_get_z(ai, a, MPFR_RNDZ);
mpfr_get_z(bi, b, MPFR_RNDZ);
switch(op) {
case BF_OP_OR:
mpz_ior(ai, ai, bi);
break;
case BF_OP_XOR:
mpz_xor(ai, ai, bi);
break;
case BF_OP_AND:
mpz_and(ai, ai, bi);
break;
default:
break;
}
mpfr_set_z(r, ai, MPFR_RNDZ);
mpz_clear(ai);
mpz_clear(bi);
}
break;
case BF_OP_EXP:
mpfr_ret = mpfr_exp(r, a, rnd_mode);
break;
case BF_OP_LOG:
mpfr_ret = mpfr_log(r, a, rnd_mode);
break;
case BF_OP_COS:
mpfr_ret = mpfr_cos(r, a, rnd_mode);
break;
case BF_OP_SIN:
mpfr_ret = mpfr_sin(r, a, rnd_mode);
break;
case BF_OP_TAN:
mpfr_ret = mpfr_tan(r, a, rnd_mode);
break;
case BF_OP_ATAN:
mpfr_ret = mpfr_atan(r, a, rnd_mode);
break;
case BF_OP_ATAN2:
mpfr_ret = mpfr_atan2(r, a, b, rnd_mode);
break;
case BF_OP_ASIN:
mpfr_ret = mpfr_asin(r, a, rnd_mode);
break;
case BF_OP_ACOS:
mpfr_ret = mpfr_acos(r, a, rnd_mode);
break;
case BF_OP_POW:
mpfr_ret = mpfr_pow(r, a, b, rnd_mode);
break;
default:
abort();
}
*pcycles += get_cycles();
if (mpfr_ret != 0)
ret |= BF_ST_INEXACT;
mpfr_to_bf(r1, r);
mpfr_clear(a);
mpfr_clear(b);
mpfr_clear(r);
return ret;
}
int mpfr_exec_setstr(bf_t *r, const char *str, int radix,
int64_t prec, int rnd_mode)
{
mpfr_t r1;
int mpfr_ret, ret;
mpfr_init2(r1, prec);
mpfr_ret = mpfr_strtofr(r1, str, NULL, radix, mpfr_get_rnd_mode(rnd_mode));
ret = 0;
if (mpfr_ret != 0)
ret |= BF_ST_INEXACT;
mpfr_to_bf(r, r1);
mpfr_clear(r1);
return ret;
}
static int softfp_get_rnd_mode(bf_rnd_t rnd_mode)
{
switch(rnd_mode) {
case BF_RNDN:
return RM_RNE;
case BF_RNDZ:
return RM_RTZ;
case BF_RNDU:
return RM_RUP;
case BF_RNDD:
return RM_RDN;
case BF_RNDNA:
return RM_RMM;
default:
abort();
}
}
static int softfp_set_status(uint32_t fflags)
{
int ret = 0;
if (fflags & FFLAG_INVALID_OP)
ret |= BF_ST_INVALID_OP;
if (fflags & FFLAG_DIVIDE_ZERO)
ret |= BF_ST_DIVIDE_ZERO;
if (fflags & FFLAG_OVERFLOW)
ret |= BF_ST_OVERFLOW;
if (fflags & FFLAG_UNDERFLOW)
ret |= BF_ST_UNDERFLOW;
if (fflags & FFLAG_INEXACT)
ret |= BF_ST_INEXACT;
return ret;
}
typedef union {
double d;
sfloat64 u;
} Float64Union;
int softfp_exec_op(MPFTestOPEnum op, bf_t *r1, bf_t *a1, bf_t *b1,
limb_t prec, bf_rnd_t rnd_mode, int64_t *pcycles)
{
sfloat64 r, a, b;
int ret = 0;
uint32_t fflags, rm;
Float64Union u;
*pcycles -= get_cycles();
/* Note: the inputs must already be float64 */
bf_get_float64(a1, &u.d, BF_RNDZ);
// printf("ad=%a\n", u.d);
a = u.u;
/* Note: the inputs must already be float64 */
bf_get_float64(b1, &u.d, BF_RNDZ);
// printf("bd=%a\n", u.d);
b = u.u;
rm = softfp_get_rnd_mode(rnd_mode);
fflags = 0;
switch(op) {
case BF_OP_MUL:
r = mul_sf64(a, b, rm, &fflags);
ret = softfp_set_status(fflags);
break;
case BF_OP_ADD:
r = add_sf64(a, b, rm, &fflags);
ret = softfp_set_status(fflags);
break;
case BF_OP_SUB:
r = sub_sf64(a, b, rm, &fflags);
ret = softfp_set_status(fflags);
break;
case BF_OP_CMP_EQ:
r = 0;
ret = eq_quiet_sf64(a, b, &fflags);
break;
case BF_OP_CMP_LT:
r = 0;
ret = lt_sf64(a, b, &fflags);
break;
case BF_OP_CMP_LE:
r = 0;
ret = le_sf64(a, b, &fflags);
break;
case BF_OP_DIV:
r = div_sf64(a, b, rm, &fflags);
ret = softfp_set_status(fflags);
break;
case BF_OP_SQRT:
r = sqrt_sf64(a, rm, &fflags);
ret = softfp_set_status(fflags);
break;
// case BF_OP_RINT:
// case BF_OP_OR:
// case BF_OP_XOR:
// case BF_OP_AND:
default:
abort();
}
/* Note: the inputs must already be float64 */
u.u = r;
// printf("rd=%a\n", u.d);
bf_set_float64(r1, u.d);
*pcycles += get_cycles();
return ret;
}
mpd_context_t mpd_ctx;
static void bfdec_to_mpd(mpd_t *a1, const bfdec_t *a)
{
char *a_str;
a_str = bfdec_ftoa(NULL, a, BF_PREC_INF, BF_RNDZ | BF_FTOA_FORMAT_FREE);
// printf("a_str=%s\n", a_str);
mpd_qsetprec(&mpd_ctx, a->len * LIMB_DIGITS);
mpd_set_string(a1, a_str, &mpd_ctx);
free(a_str);
}
static void mpd_to_bfdec(bfdec_t *r, const mpd_t *r1)
{
char *r1_str;
r1_str = mpd_to_sci(r1, 0);
// printf("r1_str=%s\n", r1_str);
bfdec_atof(r, r1_str, NULL, BF_PREC_INF, BF_RNDZ);
// bfdec_print_str("ref", r);
free(r1_str);
}
int mpdecimal_exec_op(MPFTestOPEnum op, bfdec_t *r, bfdec_t *a, bfdec_t *b,
limb_t prec, bf_rnd_t rnd_mode, int64_t *pcycles)
{
mpd_t *a1, *b1, *r1;
uint32_t status;
int ret;
a1 = mpd_new(&mpd_ctx);
b1 = mpd_new(&mpd_ctx);
r1 = mpd_new(&mpd_ctx);
bfdec_to_mpd(a1, a);
bfdec_to_mpd(b1, b);
mpd_qsetprec(&mpd_ctx, prec);
// printf("rnd_mode1=%d\n", rnd_mode);
switch(rnd_mode) {
case BF_RNDN:
mpd_qsetround(&mpd_ctx, MPD_ROUND_HALF_EVEN);
break;
case BF_RNDZ:
mpd_qsetround(&mpd_ctx, MPD_ROUND_DOWN);
break;
case BF_RNDU:
mpd_qsetround(&mpd_ctx, MPD_ROUND_CEILING);
break;
case BF_RNDD:
mpd_qsetround(&mpd_ctx, MPD_ROUND_FLOOR);
break;
case BF_RNDNA:
mpd_qsetround(&mpd_ctx, MPD_ROUND_HALF_UP);
break;
case BF_RNDA:
mpd_qsetround(&mpd_ctx, MPD_ROUND_UP);
break;
default:
abort();
}
*pcycles -= get_cycles();
status = 0;
switch(op) {
case BF_OP_ADD_DEC:
mpd_qadd(r1, a1, b1, &mpd_ctx, &status);
break;
case BF_OP_MUL_DEC:
mpd_qmul(r1, a1, b1, &mpd_ctx, &status);
break;
case BF_OP_DIV_DEC:
mpd_qdiv(r1, a1, b1, &mpd_ctx, &status);
break;
case BF_OP_SQRT_DEC:
mpd_qsqrt(r1, a1, &mpd_ctx, &status);
break;
case BF_OP_FMOD_DEC:
mpd_qrem(r1, a1, b1, &mpd_ctx, &status);
break;
case BF_OP_RINT_DEC:
mpd_qround_to_intx(r1, a1, &mpd_ctx, &status);
break;
default:
abort();
}
*pcycles += get_cycles();
ret = 0;
if (status & MPD_Inexact)
ret |= BF_ST_INEXACT;
if (status & MPD_Overflow)
ret |= BF_ST_OVERFLOW;
if (status & MPD_Underflow)
ret |= BF_ST_UNDERFLOW;
if (status & MPD_Invalid_operation)
ret |= BF_ST_INVALID_OP;
mpd_to_bfdec(r, r1);
mpd_del(a1);
mpd_del(b1);
mpd_del(r1);
return ret;
}
int bf_exec_op(MPFTestOPEnum op, bf_t *r, bf_t *a, bf_t *b,
limb_t prec, bf_flags_t flags, int64_t *pcycles)
{
int ret = 0;
*pcycles -= get_cycles();
switch(op) {
case BF_OP_MUL:
ret = bf_mul(r, a, b, prec, flags);
break;
case BF_OP_ADD:
ret = bf_add(r, a, b, prec, flags);
break;
case BF_OP_SUB:
ret = bf_sub(r, a, b, prec, flags);
break;
case BF_OP_RINT:
bf_set(r, a);
ret = bf_rint(r, flags);
break;
case BF_OP_ROUND:
bf_set(r, a);
ret = bf_round(r, prec, flags);
break;
case BF_OP_CMP_EQ:
ret = bf_cmp_eq(a, b);
break;
case BF_OP_CMP_LT:
ret = bf_cmp_lt(a, b);
break;
case BF_OP_CMP_LE:
ret = bf_cmp_le(a, b);
break;
case BF_OP_DIV:
ret = bf_div(r, a, b, prec, flags);
break;
case BF_OP_FMOD:
ret = bf_rem(r, a, b, prec, flags, BF_RNDZ);
break;
case BF_OP_REM:
ret = bf_rem(r, a, b, prec, flags, BF_RNDN);
break;
case BF_OP_SQRT:
ret = bf_sqrt(r, a, prec, flags);
break;
case BF_OP_OR:
bf_logic_or(r, a, b);
break;
case BF_OP_XOR:
bf_logic_xor(r, a, b);
break;
case BF_OP_AND:
bf_logic_and(r, a, b);
break;
case BF_OP_EXP:
ret = bf_exp(r, a, prec, flags);
break;
case BF_OP_LOG:
ret = bf_log(r, a, prec, flags);
break;
case BF_OP_COS:
ret = bf_cos(r, a, prec, flags);
break;
case BF_OP_SIN:
ret = bf_sin(r, a, prec, flags);
break;
case BF_OP_TAN:
ret = bf_tan(r, a, prec, flags);
break;
case BF_OP_ATAN:
ret = bf_atan(r, a, prec, flags);
break;
case BF_OP_ATAN2:
ret = bf_atan2(r, a, b, prec, flags);
break;
case BF_OP_ASIN:
ret = bf_asin(r, a, prec, flags);
break;
case BF_OP_ACOS:
ret = bf_acos(r, a, prec, flags);
break;
case BF_OP_POW:
ret = bf_pow(r, a, b, prec, flags);
break;
default:
abort();
}
*pcycles += get_cycles();
return ret;
}
int bfdec_exec_op(MPFTestOPEnum op, bfdec_t *r,
const bfdec_t *a, const bfdec_t *b,
limb_t prec, bf_flags_t flags, int64_t *pcycles)
{
int ret;
*pcycles -= get_cycles();
switch(op) {
case BF_OP_ADD_DEC:
ret = bfdec_add(r, a, b, prec, flags);
break;
case BF_OP_MUL_DEC:
ret = bfdec_mul(r, a, b, prec, flags);
break;
case BF_OP_DIV_DEC:
ret = bfdec_div(r, a, b, prec, flags);
break;
case BF_OP_SQRT_DEC:
ret = bfdec_sqrt(r, a, prec, flags);
break;
case BF_OP_FMOD_DEC:
ret = bfdec_rem(r, a, b, prec, flags, BF_RNDZ);
break;
case BF_OP_RINT_DEC:
bfdec_set(r, a);
ret = bfdec_rint(r, flags);
break;
default:
abort();
}
*pcycles += get_cycles();
return ret;
}
void print_status(int status)
{
printf("%c%c%c%c%c",
(status & BF_ST_INVALID_OP) ? 'I' : '-',
(status & BF_ST_DIVIDE_ZERO) ? 'Z' : '-',
(status & BF_ST_OVERFLOW) ? 'O' : '-',
(status & BF_ST_UNDERFLOW) ? 'U' : '-',
(status & BF_ST_INEXACT) ? 'X' : '-');
}
static BOOL bf_is_same(const bf_t *a, const bf_t *b)
{
return a->sign == b->sign && bf_cmpu(a, b) == 0;
}
void test_atof(limb_t prec, int duration_ms,
int exp_bits, bf_rnd_t rnd_mode, int seed)