-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterflop_vprec.c
1131 lines (1017 loc) · 43.1 KB
/
interflop_vprec.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
/*****************************************************************************\
* *\
* This file is part of the Verificarlo project, *\
* under the Apache License v2.0 with LLVM Exceptions. *\
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception. *\
* See https://llvm.org/LICENSE.txt for license information. *\
* *\
* *\
* Copyright (c) 2015 *\
* Universite de Versailles St-Quentin-en-Yvelines *\
* CMLA, Ecole Normale Superieure de Cachan *\
* *\
* Copyright (c) 2018 *\
* Universite de Versailles St-Quentin-en-Yvelines *\
* *\
* Copyright (c) 2019-2023 *\
* Verificarlo Contributors *\
* *\
****************************************************************************/
// Changelog:
//
// 2018-07-7 Initial version from scratch
//
// 2019-11-25 Code refactoring, format conversions moved to
// ../../common/vprec_tools.c
//
#include <argp.h>
#include "common/vprec_tools.h"
#include "interflop/common/float_const.h"
#include "interflop/common/float_struct.h"
#include "interflop/common/float_utils.h"
#include "interflop/fma/interflop_fma.h"
#include "interflop/hashmap/vfc_hashmap.h"
#include "interflop/interflop.h"
#include "interflop/interflop_stdlib.h"
#include "interflop/iostream/logger.h"
#include "interflop_vprec.h"
#include "interflop_vprec_function_instrumentation.h"
static const char backend_name[] = "interflop-vprec";
static const char backend_version[] = "1.x-dev";
static const char key_prec_b32_str[] = "precision-binary32";
static const char key_prec_b64_str[] = "precision-binary64";
static const char key_range_b32_str[] = "range-binary32";
static const char key_range_b64_str[] = "range-binary64";
static const char key_preset_str[] = "preset";
static const char key_mode_str[] = "mode";
static const char key_err_mode_str[] = "error-mode";
static const char key_err_exp_str[] = "max-abs-error-exponent";
static const char key_daz_str[] = "daz";
static const char key_ftz_str[] = "ftz";
/* variables that control precision, range and mode */
/* Modes' names */
static const char *VPREC_MODE_STR[] = {[vprecmode_ieee] = "ieee",
[vprecmode_full] = "full",
[vprecmode_ib] = "ib",
[vprecmode_ob] = "ob"};
static const char *VPREC_ERR_MODE_STR[] = {[vprec_err_mode_rel] = "rel",
[vprec_err_mode_abs] = "abs",
[vprec_err_mode_all] = "all"};
static const char *VPREC_PRESET_STR[] = {[vprec_preset_binary16] = "binary16",
[vprec_preset_binary32] = "binary32",
[vprec_preset_bfloat16] = "bfloat16",
[vprec_preset_tensorfloat] =
"tensorfloat",
[vprec_preset_fp24] = "fp24",
[vprec_preset_PXR24] = "PXR24"};
static float _vprec_binary32_binary_op(float a, float b,
const vprec_operation op, void *context);
static double _vprec_binary64_binary_op(double a, double b,
const vprec_operation op,
void *context);
/******************** VPREC CONTROL FUNCTIONS *******************
* The following functions are used to set virtual precision,
* VPREC mode of operation and instrumentation mode.
***************************************************************/
void _set_vprec_mode(vprec_mode mode, vprec_context_t *ctx) {
if (mode >= _vprecmode_end_) {
logger_error("invalid mode provided, must be one of: "
"{ieee, full, ib, ob}.");
} else {
ctx->mode = mode;
}
}
void _set_vprec_precision_binary32(int precision, vprec_context_t *ctx) {
if (precision < VPREC_PRECISION_BINARY32_MIN) {
logger_error("invalid precision provided for binary32. "
"Must be greater than %d",
VPREC_PRECISION_BINARY32_MIN);
} else if (VPREC_PRECISION_BINARY32_MAX < precision) {
logger_error("invalid precision provided for binary32. "
"Must be lower than %d",
VPREC_PRECISION_BINARY32_MAX);
} else {
ctx->binary32_precision = precision;
}
}
void _set_vprec_range_binary32(int range, vprec_context_t *ctx) {
if (range < VPREC_RANGE_BINARY32_MIN) {
logger_error("invalid range provided for binary32. "
"Must be greater than %d",
VPREC_RANGE_BINARY32_MIN);
} else if (VPREC_RANGE_BINARY32_MAX < range) {
logger_error("invalid range provided for binary32. "
"Must be lower than %d",
VPREC_RANGE_BINARY32_MAX);
} else {
ctx->binary32_range = range;
}
}
void _set_vprec_precision_binary64(int precision, vprec_context_t *ctx) {
if (precision < VPREC_PRECISION_BINARY64_MIN) {
logger_error("invalid precision provided for binary64 (%d). "
"Must be greater than %d",
precision, VPREC_PRECISION_BINARY64_MIN);
} else if (VPREC_PRECISION_BINARY64_MAX < precision) {
logger_error("invalid precision provided for binary64. "
"Must be lower than %d",
VPREC_PRECISION_BINARY64_MAX);
} else {
ctx->binary64_precision = precision;
}
}
void _set_vprec_range_binary64(int range, vprec_context_t *ctx) {
if (range < VPREC_RANGE_BINARY64_MIN) {
logger_error("invalid range provided for binary64. "
"Must be greater than %d",
VPREC_RANGE_BINARY64_MIN);
} else if (VPREC_RANGE_BINARY64_MAX < range) {
logger_error("invalid range provided for binary64. "
"Must be lower than %d",
VPREC_RANGE_BINARY64_MAX);
} else {
ctx->binary64_range = range;
}
}
void _set_vprec_error_mode(vprec_err_mode mode, vprec_context_t *ctx) {
if (mode >= _vprec_err_mode_end_) {
logger_error("invalid error mode provided, must be one of: "
"{rel, abs, all}.");
} else {
switch (mode) {
case vprec_err_mode_rel:
ctx->relErr = true;
ctx->absErr = false;
break;
case vprec_err_mode_abs:
ctx->relErr = false;
ctx->absErr = true;
break;
case vprec_err_mode_all:
ctx->relErr = true;
ctx->absErr = true;
default:
break;
}
}
}
void _set_vprec_max_abs_err_exp(long exponent, vprec_context_t *ctx) {
ctx->absErr_exp = exponent;
}
const char *_get_error_mode_str(vprec_context_t *ctx) {
if (ctx->relErr && ctx->absErr) {
return VPREC_ERR_MODE_STR[vprec_err_mode_all];
} else if (ctx->relErr && !ctx->absErr) {
return VPREC_ERR_MODE_STR[vprec_err_mode_rel];
} else if (!ctx->relErr && ctx->absErr) {
return VPREC_ERR_MODE_STR[vprec_err_mode_abs];
} else {
return NULL;
}
}
static vprec_preset_precision _get_vprec_preset_precision(vprec_preset preset) {
switch (preset) {
case vprec_preset_binary16:
return vprec_preset_precision_binary16;
case vprec_preset_binary32:
return vprec_preset_precision_binary32;
case vprec_preset_bfloat16:
return vprec_preset_precision_bfloat16;
case vprec_preset_tensorfloat:
return vprec_preset_precision_tensorfloat;
case vprec_preset_fp24:
return vprec_preset_precision_fp24;
case vprec_preset_PXR24:
return vprec_preset_precision_PXR24;
default:
logger_error("invalid preset provided, must be one of: "
"{binary16, binary32, binary64, bfloat16, tensorfloat, "
"fp24, PXR24}");
return _vprec_preset_precision_end_;
}
}
static vprec_preset_range _get_vprec_preset_range(vprec_preset preset) {
switch (preset) {
case vprec_preset_binary16:
return vprec_preset_range_binary16;
case vprec_preset_binary32:
return vprec_preset_range_binary32;
case vprec_preset_bfloat16:
return vprec_preset_range_bfloat16;
case vprec_preset_tensorfloat:
return vprec_preset_range_tensorfloat;
case vprec_preset_fp24:
return vprec_preset_range_fp24;
case vprec_preset_PXR24:
return vprec_preset_range_PXR24;
default:
logger_error("invalid preset provided, must be one of: "
"{binary16, binary32, binary64, bfloat16, tensorfloat, "
"fp24, PXR24}");
return _vprec_preset_range_end_;
}
}
const char *get_vprec_mode_name(vprec_mode mode) {
if (mode >= _vprecmode_end_) {
return NULL;
} else {
return VPREC_MODE_STR[mode];
}
}
void _set_vprec_daz(bool daz, vprec_context_t *ctx) { ctx->daz = daz; }
void _set_vprec_ftz(bool ftz, vprec_context_t *ctx) { ctx->ftz = ftz; }
/******************** VPREC HELPER FUNCTIONS *******************
* The following functions are used to set virtual precision,
* VPREC mode of operation and instrumentation mode.
***************************************************************/
extern int compute_absErr_vprec_binary32(bool isDenormal,
vprec_context_t *currentContext,
int expDiff, int binary32_precision);
inline int compute_absErr_vprec_binary32(bool isDenormal,
vprec_context_t *currentContext,
int expDiff, int binary32_precision) {
/* this function is used only when in vprec error mode abs and all,
* so there is no need to handle vprec error mode rel */
if (isDenormal == true) {
/* denormal, or underflow case */
if (currentContext->relErr == true) {
/* vprec error mode all */
if (abs(currentContext->absErr_exp) < binary32_precision)
return currentContext->absErr_exp;
else
return binary32_precision;
} else {
/* vprec error mode abs */
return currentContext->absErr_exp;
}
} else {
/* normal case */
if (currentContext->relErr == true) {
/* vprec error mode all */
if (expDiff < binary32_precision)
return expDiff;
else {
return binary32_precision;
}
} else {
/* vprec error mode abs */
if (expDiff < FLOAT_PMAN_SIZE) {
return expDiff;
} else {
return FLOAT_PMAN_SIZE;
}
}
}
}
extern int compute_absErr_vprec_binary64(bool isDenormal,
vprec_context_t *currentContext,
int expDiff, int binary64_precision);
inline int compute_absErr_vprec_binary64(bool isDenormal,
vprec_context_t *currentContext,
int expDiff, int binary64_precision) {
/* this function is used only when in vprec error mode abs and all,
* so there is no need to handle vprec error mode rel */
if (isDenormal == true) {
/* denormal, or underflow case */
if (currentContext->relErr == true) {
/* vprec error mode all */
if (abs(currentContext->absErr_exp) < binary64_precision)
return currentContext->absErr_exp;
else
return binary64_precision;
} else {
/* vprec error mode abs */
return currentContext->absErr_exp;
}
} else {
/* normal case */
if (currentContext->relErr == true) {
/* vprec error mode all */
if (expDiff < binary64_precision)
return expDiff;
else {
return binary64_precision;
}
} else {
/* vprec error mode abs */
if (expDiff < DOUBLE_PMAN_SIZE) {
return expDiff;
} else {
return DOUBLE_PMAN_SIZE;
}
}
}
}
extern float handle_binary32_normal_absErr(float a, int32_t aexp,
int binary32_precision,
vprec_context_t *currentContext);
inline float handle_binary32_normal_absErr(float a, int32_t aexp,
int binary32_precision,
vprec_context_t *currentContext) {
/* absolute error mode, or both absolute and relative error modes */
int expDiff = aexp - currentContext->absErr_exp;
float retVal;
if (expDiff < -1) {
/* equivalent to underflow on the precision given by absolute error */
retVal = 0;
} else if (expDiff == -1) {
/* case when the number is just below the absolute error threshold,
but will round to one ulp on the format given by the absolute error;
this needs to be handled separately, as round_binary32_normal cannot
generate this number */
retVal = copysignf(fpow2i(currentContext->absErr_exp), a);
} else {
/* normal case for the absolute error mode */
int binary32_precision_adjusted = compute_absErr_vprec_binary32(
false, currentContext, expDiff, binary32_precision);
retVal = round_binary32_normal(a, binary32_precision_adjusted);
}
return retVal;
}
extern double handle_binary64_normal_absErr(double a, int64_t aexp,
int binary64_precision,
vprec_context_t *currentContext);
inline double handle_binary64_normal_absErr(double a, int64_t aexp,
int binary64_precision,
vprec_context_t *currentContext) {
/* absolute error mode, or both absolute and relative error modes */
int expDiff = aexp - currentContext->absErr_exp;
double retVal;
if (expDiff < -1) {
/* equivalent to underflow on the precision given by absolute error */
retVal = 0;
} else if (expDiff == -1) {
/* case when the number is just below the absolute error threshold,
but will round to one ulp on the format given by the absolute error;
this needs to be handled separately, as round_binary32_normal cannot
generate this number */
retVal = copysign(pow2i(currentContext->absErr_exp), a);
} else {
/* normal case for the absolute error mode */
int binary64_precision_adjusted = compute_absErr_vprec_binary64(
false, currentContext, expDiff, binary64_precision);
retVal = round_binary64_normal(a, binary64_precision_adjusted);
}
return retVal;
}
/******************** VPREC ARITHMETIC FUNCTIONS ********************
* The following set of functions perform the VPREC operation. Operands
* are first correctly rounded to the target precison format if inbound
* is set, the operation is then perform using IEEE hw and
* correct rounding to the target precision format is done if outbound
* is set.
*******************************************************************/
#define PERFORM_FMA(A, B, C) \
_Generic(A, float \
: interflop_fma_binary32, double \
: interflop_fma_binary64, __float128 \
: interflop_fma_binary128)(A, B, C)
/* perform_binary_op: applies the binary operator (op) to (a) and (b) */
/* and stores the result in (res) */
#define perform_binary_op(op, res, a, b) \
switch (op) { \
case vprec_add: \
res = (a) + (b); \
break; \
case vprec_mul: \
res = (a) * (b); \
break; \
case vprec_sub: \
res = (a) - (b); \
break; \
case vprec_div: \
res = (a) / (b); \
break; \
default: \
logger_error("invalid operator %c", op); \
};
/* perform_ternary_op: applies the ternary operator (op) to (a), (b) and (c) */
/* and stores the result in (res) */
#define perform_ternary_op(op, res, a, b, c) \
switch (op) { \
case vprec_fma: \
res = PERFORM_FMA((a), (b), (c)); \
break; \
default: \
logger_error("invalid operator %c", op); \
};
// Round the float with the given precision
float _vprec_round_binary32(float a, char is_input, void *context,
int binary32_range, int binary32_precision) {
vprec_context_t *currentContext = (vprec_context_t *)context;
/* test if 'a' is a special case */
if (!isfinite(a)) {
return a;
}
/* round to zero or set to infinity if underflow or overflow compared to
* ctx->binary32_range */
int emax = (1 << (binary32_range - 1)) - 1;
/* here emin is the smallest exponent in the *normal* range */
int emin = 1 - emax;
binary32 aexp = {.f32 = a};
aexp.s32 = ((FLOAT_GET_EXP & aexp.u32) >> FLOAT_PMAN_SIZE) - FLOAT_EXP_COMP;
/* check for overflow in target range */
if (aexp.s32 > emax) {
a = a * INFINITY;
return a;
}
/* check for underflow in target range */
if (aexp.s32 < emin) {
/* underflow case: possibly a denormal */
if ((currentContext->daz && is_input) ||
(currentContext->ftz && !is_input)) {
return a * 0; // preserve sign
} else if (FP_ZERO == fpclassify(a)) {
return a;
} else {
if (currentContext->absErr == true) {
/* absolute error mode, or both absolute and relative error modes */
int binary32_precision_adjusted = compute_absErr_vprec_binary32(
true, currentContext, 0, binary32_precision);
a = handle_binary32_denormal(a, emin, binary32_precision_adjusted);
} else {
/* relative error mode */
a = handle_binary32_denormal(a, emin, binary32_precision);
}
}
} else {
/* else, normal case: can be executed even if a
previously rounded and truncated as denormal */
if (currentContext->absErr == true) {
/* absolute error mode, or both absolute and relative error modes */
a = handle_binary32_normal_absErr(a, aexp.s32, binary32_precision,
currentContext);
} else {
/* relative error mode */
a = round_binary32_normal(a, binary32_precision);
}
}
return a;
}
// Round the double with the given precision
double _vprec_round_binary64(double a, char is_input, void *context,
int binary64_range, int binary64_precision) {
vprec_context_t *currentContext = (vprec_context_t *)context;
/* test if 'a' is a special case */
if (!isfinite(a)) {
return a;
}
/* round to zero or set to infinity if underflow or overflow compare to
* ctx->binary64_range */
int emax = (1 << (binary64_range - 1)) - 1;
/* here emin is the smallest exponent in the *normal* range */
int emin = 1 - emax;
binary64 aexp = {.f64 = a};
aexp.s64 =
((DOUBLE_GET_EXP & aexp.u64) >> DOUBLE_PMAN_SIZE) - DOUBLE_EXP_COMP;
/* check for overflow in target range */
if (aexp.s64 > emax) {
a = a * INFINITY;
return a;
}
/* check for underflow in target range */
if (aexp.s64 < emin) {
/* underflow case: possibly a denormal */
if ((currentContext->daz && is_input) ||
(currentContext->ftz && !is_input)) {
return a * 0; // preserve sign
} else if (FP_ZERO == fpclassify(a)) {
return a;
} else {
if (currentContext->absErr == true) {
/* absolute error mode, or both absolute and relative error modes */
int binary64_precision_adjusted = compute_absErr_vprec_binary64(
true, currentContext, 0, binary64_precision);
a = handle_binary64_denormal(a, emin, binary64_precision_adjusted);
} else {
/* relative error mode */
a = handle_binary64_denormal(a, emin, binary64_precision);
}
}
} else {
/* else, normal case: can be executed even if a
previously rounded and truncated as denormal */
if (currentContext->absErr == true) {
/* absolute error mode, or both absolute and relative error modes */
a = handle_binary64_normal_absErr(a, aexp.s64, binary64_precision,
currentContext);
} else {
/* relative error mode */
a = round_binary64_normal(a, binary64_precision);
}
}
return a;
}
static inline float _vprec_binary32_binary_op(float a, float b,
const vprec_operation op,
void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
float res = 0;
if ((ctx->mode == vprecmode_full) || (ctx->mode == vprecmode_ib)) {
a = _vprec_round_binary32(a, 1, context, ctx->binary32_range,
ctx->binary32_precision);
b = _vprec_round_binary32(b, 1, context, ctx->binary32_range,
ctx->binary32_precision);
}
perform_binary_op(op, res, a, b);
if ((ctx->mode == vprecmode_full) || (ctx->mode == vprecmode_ob)) {
res = _vprec_round_binary32(res, 0, context, ctx->binary32_range,
ctx->binary32_precision);
}
return res;
}
static inline double _vprec_binary64_binary_op(double a, double b,
const vprec_operation op,
void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
double res = 0;
if ((ctx->mode == vprecmode_full) || (ctx->mode == vprecmode_ib)) {
a = _vprec_round_binary64(a, 1, context, ctx->binary64_range,
ctx->binary64_precision);
b = _vprec_round_binary64(b, 1, context, ctx->binary64_range,
ctx->binary64_precision);
}
perform_binary_op(op, res, a, b);
if ((ctx->mode == vprecmode_full) || (ctx->mode == vprecmode_ob)) {
res = _vprec_round_binary64(res, 0, context, ctx->binary64_range,
ctx->binary64_precision);
}
return res;
}
static inline float _vprec_binary32_ternary_op(float a, float b, float c,
const vprec_operation op,
void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
float res = 0;
if (ctx->mode == vprecmode_ib || ctx->mode == vprecmode_full) {
a = _vprec_round_binary32(a, 0, context, ctx->binary32_precision,
ctx->binary32_range);
b = _vprec_round_binary32(b, 0, context, ctx->binary32_precision,
ctx->binary32_range);
c = _vprec_round_binary32(c, 0, context, ctx->binary32_precision,
ctx->binary32_range);
}
perform_ternary_op(op, res, a, b, c);
if (ctx->mode == vprecmode_ob || ctx->mode == vprecmode_full) {
res = _vprec_round_binary32(res, 0, context, ctx->binary32_precision,
ctx->binary32_range);
}
return res;
}
static inline double _vprec_binary64_ternary_op(double a, double b, double c,
const vprec_operation op,
void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
double res = 0;
if (ctx->mode == vprecmode_ib || ctx->mode == vprecmode_full) {
a = _vprec_round_binary64(a, 0, context, ctx->binary64_precision,
ctx->binary64_range);
b = _vprec_round_binary64(b, 0, context, ctx->binary64_precision,
ctx->binary64_range);
c = _vprec_round_binary64(c, 0, context, ctx->binary64_precision,
ctx->binary64_range);
}
perform_ternary_op(op, res, a, b, c);
if (ctx->mode == vprecmode_ob || ctx->mode == vprecmode_full) {
res = _vprec_round_binary64(res, 0, context, ctx->binary64_precision,
ctx->binary64_range);
}
return res;
}
// Set precision for internal operations and round input arguments for a given
// function call
void INTERFLOP_VPREC_API(enter_function)(interflop_function_stack_t *stack,
void *context, int nb_args,
va_list ap) {
_vfi_enter_function(stack, context, nb_args, ap);
}
// Set precision for internal operations and round output arguments for a given
// function call
void INTERFLOP_VPREC_API(exit_function)(interflop_function_stack_t *stack,
void *context, int nb_args,
va_list ap) {
_vfi_exit_function(stack, context, nb_args, ap);
}
/************************* FPHOOKS FUNCTIONS *************************
* These functions correspond to those inserted into the source code
* during source to source compilation and are replacement to floating
* point operators
**********************************************************************/
void INTERFLOP_VPREC_API(add_float)(float a, float b, float *c, void *context) {
*c = _vprec_binary32_binary_op(a, b, vprec_add, context);
}
void INTERFLOP_VPREC_API(sub_float)(float a, float b, float *c, void *context) {
*c = _vprec_binary32_binary_op(a, b, vprec_sub, context);
}
void INTERFLOP_VPREC_API(mul_float)(float a, float b, float *c, void *context) {
*c = _vprec_binary32_binary_op(a, b, vprec_mul, context);
}
void INTERFLOP_VPREC_API(div_float)(float a, float b, float *c, void *context) {
*c = _vprec_binary32_binary_op(a, b, vprec_div, context);
}
void INTERFLOP_VPREC_API(add_double)(double a, double b, double *c,
void *context) {
*c = _vprec_binary64_binary_op(a, b, vprec_add, context);
}
void INTERFLOP_VPREC_API(sub_double)(double a, double b, double *c,
void *context) {
*c = _vprec_binary64_binary_op(a, b, vprec_sub, context);
}
void INTERFLOP_VPREC_API(mul_double)(double a, double b, double *c,
void *context) {
*c = _vprec_binary64_binary_op(a, b, vprec_mul, context);
}
void INTERFLOP_VPREC_API(div_double)(double a, double b, double *c,
void *context) {
*c = _vprec_binary64_binary_op(a, b, vprec_div, context);
}
void INTERFLOP_VPREC_API(cast_double_to_float)(double a, float *b,
void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
*b = _vprec_round_binary64(a, 0, context, ctx->binary32_precision,
ctx->binary32_range);
}
void INTERFLOP_VPREC_API(fma_float)(float a, float b, float c, float *res,
void *context) {
*res = _vprec_binary32_ternary_op(a, b, c, vprec_fma, context);
}
void INTERFLOP_VPREC_API(fma_double)(double a, double b, double c, double *res,
void *context) {
*res = _vprec_binary64_ternary_op(a, b, c, vprec_fma, context);
}
void INTERFLOP_VPREC_API(user_call)(void *context, interflop_call_id id,
va_list ap) {
vprec_context_t *ctx = (vprec_context_t *)context;
switch (id) {
case INTERFLOP_SET_PRECISION_BINARY32:
_set_vprec_precision_binary32(va_arg(ap, int), ctx);
break;
case INTERFLOP_SET_PRECISION_BINARY64:
_set_vprec_precision_binary64(va_arg(ap, int), ctx);
break;
case INTERFLOP_SET_RANGE_BINARY32:
_set_vprec_range_binary32(va_arg(ap, int), ctx);
break;
case INTERFLOP_SET_RANGE_BINARY64:
_set_vprec_range_binary64(va_arg(ap, int), ctx);
break;
default:
logger_warning("Unknown interflop_call id (=%d)", id);
break;
}
}
void INTERFLOP_VPREC_API(finalize)(void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
_vfi_finalize(ctx);
}
const char *INTERFLOP_VPREC_API(get_backend_name)(void) { return backend_name; }
const char *INTERFLOP_VPREC_API(get_backend_version)(void) {
return backend_version;
}
void _vprec_check_stdlib() {
INTERFLOP_CHECK_IMPL(malloc);
INTERFLOP_CHECK_IMPL(fopen);
INTERFLOP_CHECK_IMPL(strcmp);
INTERFLOP_CHECK_IMPL(strcasecmp);
INTERFLOP_CHECK_IMPL(strtol);
INTERFLOP_CHECK_IMPL(getenv);
INTERFLOP_CHECK_IMPL(fprintf);
INTERFLOP_CHECK_IMPL(strcpy);
INTERFLOP_CHECK_IMPL(fclose);
INTERFLOP_CHECK_IMPL(gettid);
INTERFLOP_CHECK_IMPL(strerror);
INTERFLOP_CHECK_IMPL(sprintf);
INTERFLOP_CHECK_IMPL(vwarnx);
INTERFLOP_CHECK_IMPL(vfprintf);
INTERFLOP_CHECK_IMPL(exit);
INTERFLOP_CHECK_IMPL(strtok_r);
INTERFLOP_CHECK_IMPL(fgets);
INTERFLOP_CHECK_IMPL(free);
INTERFLOP_CHECK_IMPL(calloc);
}
/* allocate the context */
void _vprec_alloc_context(void **context) {
vprec_context_t *ctx =
(vprec_context_t *)interflop_malloc(sizeof(vprec_context_t));
_vfi_alloc_context(ctx);
*context = ctx;
}
/* intialize the context */
static void _vprec_init_context(vprec_context_t *ctx) {
ctx->binary32_precision = VPREC_PRECISION_BINARY32_DEFAULT;
ctx->binary32_range = VPREC_RANGE_BINARY32_DEFAULT;
ctx->binary64_precision = VPREC_PRECISION_BINARY64_DEFAULT;
ctx->binary64_range = VPREC_RANGE_BINARY64_DEFAULT;
ctx->mode = VPREC_MODE_DEFAULT;
ctx->relErr = true;
ctx->absErr = false;
ctx->absErr_exp = -DOUBLE_EXP_MIN;
ctx->daz = false;
ctx->ftz = false;
_vfi_init_context(ctx);
}
void INTERFLOP_VPREC_API(pre_init)(interflop_panic_t panic, File *stream,
void **context) {
interflop_set_handler("panic", panic);
_vprec_check_stdlib();
/* Initialize the logger */
logger_init(panic, stream, backend_name);
/* allocate the context */
_vprec_alloc_context(context);
_vprec_init_context((vprec_context_t *)*context);
}
static struct argp_option options[] = {
/* --debug, sets the variable debug = true */
{key_prec_b32_str, KEY_PREC_B32, "PRECISION", 0,
"select precision for binary32 (PRECISION >= 0)", 0},
{key_prec_b64_str, KEY_PREC_B64, "PRECISION", 0,
"select precision for binary64 (PRECISION >= 0)", 0},
{key_range_b32_str, KEY_RANGE_B32, "RANGE", 0,
"select range for binary32 (0 < RANGE && RANGE <= 8)", 0},
{key_range_b64_str, KEY_RANGE_B64, "RANGE", 0,
"select range for binary64 (0 < RANGE && RANGE <= 11)", 0},
{key_preset_str, KEY_PRESET, "PRESET", 0,
"select a default PRESET setting among {binary16, binary32, binary64, "
"bfloat16, tensorfloat, fp24, PXR24}\n"
"Format (range, precision) : "
"binary16 (5, 10), binary32 (8, 23), "
"bfloat16 (8, 7), tensorfloat (8, 10), "
"fp24 (7, 16), PXR24 (8, 15)",
0},
{key_mode_str, KEY_MODE, "MODE", 0,
"select VPREC mode among {ieee, full, ib, ob}", 0},
{key_err_mode_str, KEY_ERR_MODE, "ERROR_MODE", 0,
"select error mode among {rel, abs, all}", 0},
{key_err_exp_str, KEY_ERR_EXP, "MAX_ABS_ERROR_EXPONENT", 0,
"select magnitude of the maximum absolute error", 0},
{key_daz_str, KEY_DAZ, 0, 0,
"denormals-are-zero: sets denormals inputs to zero", 0},
{key_ftz_str, KEY_FTZ, 0, 0, "flush-to-zero: sets denormal output to zero",
0},
{0}};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
vprec_context_t *ctx = (vprec_context_t *)state->input;
state->child_inputs[0] = ctx;
char *endptr;
int val = -1;
int precision = 0;
int range = 0;
int error = 0;
switch (key) {
case KEY_PREC_B32:
/* precision */
error = 0;
val = interflop_strtol(arg, &endptr, &error);
if (error != 0 || val < VPREC_PRECISION_BINARY32_MIN) {
logger_error("--%s invalid value provided, must be a "
"positive integer.",
key_prec_b32_str);
} else if (val > VPREC_PRECISION_BINARY32_MAX) {
logger_error("--%s invalid value provided, "
"must lower than IEEE binary32 precision (%d)",
key_prec_b32_str, VPREC_PRECISION_BINARY32_MAX);
} else {
_set_vprec_precision_binary32(val, ctx);
}
break;
case KEY_PREC_B64:
/* precision */
error = 0;
val = interflop_strtol(arg, &endptr, &error);
if (error != 0 || val < VPREC_PRECISION_BINARY64_MIN) {
logger_error("--%s invalid value provided, must be a "
"positive integer.",
key_prec_b64_str);
} else if (val > VPREC_PRECISION_BINARY64_MAX) {
logger_error("--%s invalid value provided, "
"must be lower than IEEE binary64 precision (%d)",
key_prec_b64_str, VPREC_PRECISION_BINARY64_MAX);
} else {
_set_vprec_precision_binary64(val, ctx);
}
break;
case KEY_RANGE_B32:
/* precision */
error = 0;
val = interflop_strtol(arg, &endptr, &error);
if (error != 0 || val < VPREC_RANGE_BINARY32_MIN) {
logger_error("--%s invalid value provided, must be a "
"positive integer.",
key_range_b32_str);
} else if (val > VPREC_RANGE_BINARY32_MAX) {
logger_error("--%s invalid value provided, "
"must be lower than IEEE binary32 range size (%d)",
key_range_b32_str, VPREC_RANGE_BINARY32_MAX);
} else {
_set_vprec_range_binary32(val, ctx);
}
break;
case KEY_RANGE_B64:
/* precision */
error = 0;
val = interflop_strtol(arg, &endptr, &error);
if (error != 0 || val < VPREC_RANGE_BINARY64_MIN) {
logger_error("--%s invalid value provided, must be a "
"positive integer.",
key_range_b64_str);
} else if (val > VPREC_RANGE_BINARY64_MAX) {
logger_error("--%s invalid value provided, "
"must be lower than IEEE binary64 range size (%d)",
key_range_b64_str, VPREC_RANGE_BINARY64_MAX);
} else {
_set_vprec_range_binary64(val, ctx);
}
break;
case KEY_MODE:
/* mode */
if (interflop_strcasecmp(VPREC_MODE_STR[vprecmode_ieee], arg) == 0) {
_set_vprec_mode(vprecmode_ieee, ctx);
} else if (interflop_strcasecmp(VPREC_MODE_STR[vprecmode_full], arg) == 0) {
_set_vprec_mode(vprecmode_full, ctx);
} else if (interflop_strcasecmp(VPREC_MODE_STR[vprecmode_ib], arg) == 0) {
_set_vprec_mode(vprecmode_ib, ctx);
} else if (interflop_strcasecmp(VPREC_MODE_STR[vprecmode_ob], arg) == 0) {
_set_vprec_mode(vprecmode_ob, ctx);
} else {
logger_error("--%s invalid value provided, must be one of: "
"{ieee, full, ib, ob}.",
key_mode_str);
}
break;
case KEY_ERR_MODE:
/* vprec error mode */
if (interflop_strcasecmp(VPREC_ERR_MODE_STR[vprec_err_mode_rel], arg) ==
0) {
_set_vprec_error_mode(vprec_err_mode_rel, ctx);
} else if (interflop_strcasecmp(VPREC_ERR_MODE_STR[vprec_err_mode_abs],
arg) == 0) {
_set_vprec_error_mode(vprec_err_mode_abs, ctx);
} else if (interflop_strcasecmp(VPREC_ERR_MODE_STR[vprec_err_mode_all],
arg) == 0) {
_set_vprec_error_mode(vprec_err_mode_all, ctx);
} else {
logger_error("--%s invalid value provided, must be one of: "
"{rel, abs, all}.",
key_err_mode_str);
}
break;
case KEY_ERR_EXP:
/* exponent of the maximum absolute error */
error = 0;
long exp = interflop_strtol(arg, &endptr, &error);
if (error != 0) {
logger_error("--%s invalid value provided, must be an integer",
key_err_exp_str);
} else {
_set_vprec_max_abs_err_exp(exp, ctx);
}
break;
case KEY_DAZ:
/* denormals-are-zero */
_set_vprec_daz(true, ctx);
break;
case KEY_FTZ:
/* flush-to-zero */
_set_vprec_ftz(true, ctx);
break;
case KEY_PRESET:
/* preset */
if (interflop_strcmp(VPREC_PRESET_STR[vprec_preset_binary16], arg) == 0) {
precision = vprec_preset_precision_binary16;
range = vprec_preset_range_binary16;
} else if (interflop_strcmp(VPREC_PRESET_STR[vprec_preset_binary32], arg) ==
0) {
precision = vprec_preset_precision_binary32;
range = vprec_preset_range_binary32;
} else if (interflop_strcmp(VPREC_PRESET_STR[vprec_preset_bfloat16], arg) ==
0) {
precision = vprec_preset_precision_bfloat16;
range = vprec_preset_range_bfloat16;
} else if (interflop_strcmp(VPREC_PRESET_STR[vprec_preset_tensorfloat],
arg) == 0) {
precision = vprec_preset_precision_tensorfloat;
range = vprec_preset_range_tensorfloat;
} else if (interflop_strcmp(VPREC_PRESET_STR[vprec_preset_fp24], arg) ==
0) {
precision = vprec_preset_precision_fp24;
range = vprec_preset_range_fp24;
} else if (interflop_strcmp(VPREC_PRESET_STR[vprec_preset_PXR24], arg) ==
0) {
precision = vprec_preset_precision_PXR24;
range = vprec_preset_range_PXR24;
} else {
logger_error("--%s invalid preset provided, must be one of: "
"{binary16, binary32, binary64, bfloat16, tensorfloat, "
"fp24, PXR24}",
key_preset_str);