-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterflop_vprec_function_instrumentation.c
888 lines (762 loc) · 34.2 KB
/
interflop_vprec_function_instrumentation.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
/*****************************************************************************\
* *\
* 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-2022 *\
* Verificarlo Contributors *\
* *\
****************************************************************************/
#include <argp.h>
#include "common/vprec_tools.h"
#include "interflop/hashmap/vfc_hashmap.h"
#include "interflop/interflop.h"
#include "interflop/interflop_stdlib.h"
#include "interflop_vprec.h"
#include "interflop_vprec_function_instrumentation.h"
/******************** VPREC FUNCTIONS INSTRUMENTATION (VFI) **************
* The following set of functions is used to apply vprec on instrumented
* functions. For that we need a hashmap to stock data and reading and
* writing functions to get and save them. Enter and exit functions are
* called before and after the instrumented function and allow us to set
* the desired precision or to round arguments, depending on the mode.
*************************************************************************/
/* instrumentation modes' names */
static const char *VPREC_INST_MODE_STR[] = {[vprecinst_arg] = "arguments",
[vprecinst_op] = "operations",
[vprecinst_all] = "all",
[vprecinst_none] = "none"};
static const char key_instrument_str[] = "instrument";
static const char key_input_file_str[] = "prec-input-file";
static const char key_output_file_str[] = "prec-output-file";
static const char key_log_file_str[] = "prec-log-file";
#define STRING_BUFF 256
#define INIT_STRING(A, N) \
for (int i = 0; i < N; i++) \
A[i] = (char *)interflop_malloc(sizeof(char) * STRING_BUFF);
#define FREE_STRING(A, N) \
for (int i = 0; i < N; i++) \
if (A[i]) \
interflop_free(A[i]);
const int elt_to_read_header = 12;
const int elt_to_read_inputs = 7;
const int elt_to_read_outputs = 7;
char *tokens_header[12];
char *tokens_inputs[7];
char *tokens_outputs[7];
static File *_vprec_log_file = Null;
/* Setter functions for variables */
void _set_vprec_input_file(const char *input_file, void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
ctx->vfi->vprec_input_file = input_file;
}
void _set_vprec_output_file(const char *output_file, void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
ctx->vfi->vprec_output_file = output_file;
}
void _set_vprec_log_file(const char *log_file, void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
ctx->vfi->vprec_log_file = log_file;
}
void _set_vprec_inst_mode(vprec_inst_mode mode, void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
if (mode >= _vprecinst_end_) {
logger_error("invalid instrumentation mode provided, must be one of:"
"{arguments, operations, all, none}.");
} else {
ctx->vfi->vprec_inst_mode = mode;
}
}
/* Argument parser functions */
void _parse_key_instrument(char *arg, vprec_context_t *ctx) {
/* instrumentation mode */
if (interflop_strcasecmp(VPREC_INST_MODE_STR[vprecinst_arg], arg) == 0) {
_set_vprec_inst_mode(vprecinst_arg, ctx);
} else if (interflop_strcasecmp(VPREC_INST_MODE_STR[vprecinst_op], arg) ==
0) {
_set_vprec_inst_mode(vprecinst_op, ctx);
} else if (interflop_strcasecmp(VPREC_INST_MODE_STR[vprecinst_all], arg) ==
0) {
_set_vprec_inst_mode(vprecinst_all, ctx);
} else if (interflop_strcasecmp(VPREC_INST_MODE_STR[vprecinst_none], arg) ==
0) {
_set_vprec_inst_mode(vprecinst_none, ctx);
} else {
logger_error("--%s invalid value provided, must be one of: "
"{arguments, operations, all}.",
key_instrument_str);
}
}
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
vprec_context_t *ctx = (vprec_context_t *)state->input;
switch (key) {
case KEY_INPUT_FILE:
/* input file */
_set_vprec_input_file(arg, ctx);
break;
case KEY_OUTPUT_FILE:
/* output file */
_set_vprec_output_file(arg, ctx);
break;
case KEY_LOG_FILE:
/* log file */
_set_vprec_log_file(arg, ctx);
break;
case KEY_INSTRUMENT:
_parse_key_instrument(arg, ctx);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp_option options[] = {
{key_input_file_str, KEY_INPUT_FILE, "INPUT", 0,
"input file with the precision configuration to use", 0},
{key_output_file_str, KEY_OUTPUT_FILE, "OUTPUT", 0,
"output file where the precision profile is written", 0},
{key_log_file_str, KEY_LOG_FILE, "LOG", 0,
"log file where input/output informations are written", 0},
{key_instrument_str, KEY_INSTRUMENT, "INSTRUMENTATION", 0,
"select VPREC instrumentation mode among {arguments, operations, full}",
0},
{0}};
struct argp vfi_argp = {options, parse_opt, "", "", NULL, NULL, NULL};
void _vfi_print_information_header(void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
logger_info("\t%s = %s\n", key_instrument_str,
VPREC_INST_MODE_STR[ctx->vfi->vprec_inst_mode]);
logger_info("\t%s = %s\n", key_input_file_str, ctx->vfi->vprec_input_file);
logger_info("\t%s = %s\n", key_output_file_str, ctx->vfi->vprec_output_file);
logger_info("\t%s = %s\n", key_log_file_str, ctx->vfi->vprec_log_file);
}
/* Core functions */
// Write the hashmap in the given file
void _vfi_write_hasmap(FILE *fout, vprec_context_t *ctx) {
for (size_t ii = 0; ii < ctx->vfi->map->capacity; ii++) {
if (get_value_at(ctx->vfi->map->items, ii) != 0 &&
get_value_at(ctx->vfi->map->items, ii) != 0) {
_vfi_t *function = (_vfi_t *)get_value_at(ctx->vfi->map->items, ii);
interflop_fprintf(
fout, "%s\t%hd\t%hd\t%zu\t%zu\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
function->id, function->isLibraryFunction,
function->isIntrinsicFunction, function->useFloat,
function->useDouble, function->OpsPrec64, function->OpsRange64,
function->OpsPrec32, function->OpsRange32, function->nb_input_args,
function->nb_output_args, function->n_calls);
for (int i = 0; i < function->nb_input_args; i++) {
interflop_fprintf(fout, "input:\t%s\t%hd\t%d\t%d\t%d\t%d\n",
function->input_args[i].arg_id,
function->input_args[i].data_type,
function->input_args[i].mantissa_length,
function->input_args[i].exponent_length,
function->input_args[i].min_range,
function->input_args[i].max_range);
}
for (int i = 0; i < function->nb_output_args; i++) {
interflop_fprintf(fout, "output:\t%s\t%hd\t%d\t%d\t%d\t%d\n",
function->output_args[i].arg_id,
function->output_args[i].data_type,
function->output_args[i].mantissa_length,
function->output_args[i].exponent_length,
function->output_args[i].min_range,
function->output_args[i].max_range);
}
}
}
}
/* Helper function scanning an integer */
/* return the integer upon success */
/* otherwise call logger_error */
long _vfi_scan_int(char *token, const char *field) {
int error = 0;
char *endptr;
long res = interflop_strtol(token, &endptr, &error);
if (error != 0) {
logger_error("Error while reading hashmap config file (field: %s)\n",
field);
}
return res;
}
int _vfi_scan_line(FILE *fi, char **tokens) {
const int line_max_size = 2048;
char line[2048];
interflop_fgets(line, line_max_size, fi);
char *tabptr;
char *token = interflop_strtok_r(line, "\t", &tabptr);
int nb_token = 0;
while (token) {
interflop_strcpy(tokens[nb_token], token);
nb_token++;
token = interflop_strtok_r(NULL, "\t", &tabptr);
}
return nb_token;
}
int _vfi_scan_header(FILE *fi, _vfi_t *function_ptr) {
int nb_token = _vfi_scan_line(fi, tokens_header);
if (nb_token != elt_to_read_header) {
return nb_token;
}
interflop_strcpy(function_ptr->id, tokens_header[0]);
function_ptr->isLibraryFunction =
_vfi_scan_int(tokens_header[1], "isLibraryFunction");
function_ptr->isIntrinsicFunction =
_vfi_scan_int(tokens_header[2], "isIntrinsicFunction");
function_ptr->useFloat = _vfi_scan_int(tokens_header[3], "useFloat");
function_ptr->useDouble = _vfi_scan_int(tokens_header[4], "useDouble");
function_ptr->OpsPrec64 = _vfi_scan_int(tokens_header[5], "OpsPrec64");
function_ptr->OpsRange64 = _vfi_scan_int(tokens_header[6], "OpsRange64");
function_ptr->OpsPrec32 = _vfi_scan_int(tokens_header[7], "OpsPrec32");
function_ptr->OpsRange32 = _vfi_scan_int(tokens_header[8], "OpsRange32");
function_ptr->nb_input_args =
_vfi_scan_int(tokens_header[9], "nb_input_args");
function_ptr->nb_output_args =
_vfi_scan_int(tokens_header[10], "nb_output_args");
function_ptr->n_calls = _vfi_scan_int(tokens_header[11], "n_calls");
return nb_token;
}
int _vfi_scan_input(FILE *fi, _vfi_t *function_ptr, int arg_pos) {
int nb_token = _vfi_scan_line(fi, tokens_inputs);
_vfi_argument_data_t *arg_data = &function_ptr->input_args[arg_pos];
// tokens[0] == "input:"
interflop_strcpy(arg_data->arg_id, tokens_inputs[1]);
arg_data->data_type = _vfi_scan_int(tokens_inputs[2], "data_type");
arg_data->mantissa_length =
_vfi_scan_int(tokens_inputs[3], "mantissa_length");
arg_data->exponent_length =
_vfi_scan_int(tokens_inputs[4], "exponent_length");
arg_data->min_range = _vfi_scan_int(tokens_inputs[5], "min_range");
arg_data->max_range = _vfi_scan_int(tokens_inputs[6], "max_range");
return nb_token;
}
int _vfi_scan_output(FILE *fi, _vfi_t *function_ptr, int arg_pos) {
int nb_token = _vfi_scan_line(fi, tokens_outputs);
_vfi_argument_data_t *arg_data = &function_ptr->output_args[arg_pos];
// tokens[0] == "output:"
interflop_strcpy(arg_data->arg_id, tokens_outputs[1]);
arg_data->data_type = _vfi_scan_int(tokens_outputs[2], "data_type");
arg_data->mantissa_length =
_vfi_scan_int(tokens_outputs[3], "mantissa_length");
arg_data->exponent_length =
_vfi_scan_int(tokens_outputs[4], "exponent_length");
arg_data->min_range = _vfi_scan_int(tokens_outputs[5], "min_range");
arg_data->max_range = _vfi_scan_int(tokens_outputs[6], "max_range");
return nb_token;
}
// Read and initialize the hashmap from the given file
void _vfi_read_hasmap(FILE *fin, vprec_context_t *ctx) {
_vfi_t function;
while (_vfi_scan_header(fin, &function) == 12) {
// allocate space for input arguments
function.input_args =
interflop_malloc(function.nb_input_args * sizeof(_vfi_argument_data_t));
// allocate space for output arguments
function.output_args = interflop_malloc(function.nb_output_args *
sizeof(_vfi_argument_data_t));
const int elt_to_read = 7;
// get input arguments precision
for (int i = 0; i < function.nb_input_args; i++) {
if (_vfi_scan_input(fin, &function, i) != elt_to_read) {
logger_error("Can't read input arguments of %s\n", function.id);
}
}
// get output arguments precision
for (int i = 0; i < function.nb_output_args; i++) {
if (_vfi_scan_output(fin, &function, i) != elt_to_read) {
logger_error("Can't read output arguments of %s\n", function.id);
}
}
// insert in the hashmap
_vfi_t *address = interflop_malloc(sizeof(_vfi_t));
(*address) = function;
vfc_hashmap_insert(ctx->vfi->map, vfc_hashmap_str_function(function.id),
address);
}
}
// Print str in vprec_log_file with the correct offset
#define _vfi_print_log(ctx, _vprec_str, ...) \
({ \
if (_vprec_log_file != NULL) { \
for (size_t _vprec_d = 0; _vprec_d < ctx->vfi->vprec_log_depth; \
_vprec_d++) \
interflop_fprintf(_vprec_log_file, "\t"); \
interflop_fprintf(_vprec_log_file, _vprec_str, ##__VA_ARGS__); \
} \
})
/* allocate the context */
void _vfi_alloc_context(void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
ctx->vfi = (t_context_vfi *)interflop_malloc(sizeof(t_context_vfi));
}
/* initialize the context */
void _vfi_init_context(void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
ctx->vfi->map = NULL;
ctx->vfi->vprec_input_file = NULL;
ctx->vfi->vprec_output_file = NULL;
ctx->vfi->vprec_log_file = NULL;
ctx->vfi->vprec_inst_mode = VPREC_INST_MODE_DEFAULT;
ctx->vfi->vprec_log_depth = 0;
}
/* initialize the variables to run vprec function instrumentation */
void _vfi_init(void *context) {
INIT_STRING(tokens_header, elt_to_read_header);
INIT_STRING(tokens_inputs, elt_to_read_inputs);
INIT_STRING(tokens_outputs, elt_to_read_outputs);
vprec_context_t *ctx = (vprec_context_t *)context;
/* Initialize the vprec_function_map */
ctx->vfi->map = vfc_hashmap_create();
/* read the hashmap */
if (ctx->vfi->vprec_input_file != NULL) {
int error = 0;
File *f = interflop_fopen(ctx->vfi->vprec_input_file, "r", &error);
if (f != NULL) {
_vfi_read_hasmap(f, ctx);
interflop_fclose(f);
} else {
logger_error("Input file can't be found: %s", interflop_strerror(error));
}
}
if (ctx->vfi->vprec_log_file != NULL) {
int error = 0;
File *f = interflop_fopen(ctx->vfi->vprec_log_file, "w", &error);
if (f != NULL) {
_vprec_log_file = f;
} else {
logger_error("Error while opening %s: %s", ctx->vfi->vprec_log_file,
interflop_strerror(error));
}
}
}
/* free objects and close files */
void _vfi_finalize(void *context) {
vprec_context_t *ctx = (vprec_context_t *)context;
/* save the hashmap */
if (ctx->vfi->vprec_output_file != NULL) {
int error = 0;
File *f = interflop_fopen(ctx->vfi->vprec_output_file, "w", &error);
if (f != NULL) {
_vfi_write_hasmap(f, ctx);
interflop_fclose(f);
} else {
logger_error("Output file can't be written: %s",
interflop_strerror(error));
}
}
/* close log file */
if (_vprec_log_file != NULL) {
interflop_fclose(_vprec_log_file);
}
/* free vprec_function_map */
vfc_hashmap_free(ctx->vfi->map);
/* destroy vprec_function_map */
vfc_hashmap_destroy(ctx->vfi->map);
FREE_STRING(tokens_header, elt_to_read_header);
FREE_STRING(tokens_inputs, elt_to_read_inputs);
FREE_STRING(tokens_outputs, elt_to_read_outputs);
}
// vprec function instrumentation
// Set precision for internal operations and round input arguments for a given
// function call
void _vfi_enter_function(interflop_function_stack_t *stack, void *context,
int nb_args, va_list ap) {
vprec_context_t *ctx = (vprec_context_t *)context;
interflop_function_info_t *function_info = stack->array[stack->top];
if (function_info == NULL)
logger_error("Call stack error\n");
_vfi_t *function_inst = vfc_hashmap_get(
ctx->vfi->map, vfc_hashmap_str_function(function_info->id));
// if the function is not in the hashtable
if (function_inst == NULL) {
function_inst = interflop_malloc(sizeof(_vfi_t));
// initialize the structure
interflop_strcpy(function_inst->id, function_info->id);
function_inst->isLibraryFunction = function_info->isLibraryFunction;
function_inst->isIntrinsicFunction = function_info->isIntrinsicFunction;
function_inst->useFloat = function_info->useFloat;
function_inst->useDouble = function_info->useDouble;
function_inst->OpsRange64 = VPREC_RANGE_BINARY64_DEFAULT;
function_inst->OpsPrec64 = VPREC_PRECISION_BINARY64_DEFAULT;
function_inst->OpsRange32 = VPREC_RANGE_BINARY32_DEFAULT;
function_inst->OpsPrec32 = VPREC_PRECISION_BINARY32_DEFAULT;
function_inst->nb_input_args = 0;
function_inst->input_args = NULL;
function_inst->nb_output_args = 0;
function_inst->output_args = NULL;
function_inst->n_calls = 0;
// insert the function in the hashmap
vfc_hashmap_insert(ctx->vfi->map,
vfc_hashmap_str_function(function_info->id),
function_inst);
}
// increment the number of calls
function_inst->n_calls++;
// set internal operations precision with custom values depending on the mode
if (!function_info->isLibraryFunction &&
!function_info->isIntrinsicFunction &&
ctx->vfi->vprec_inst_mode != vprecinst_arg &&
ctx->vfi->vprec_inst_mode != vprecinst_none) {
_set_vprec_precision_binary64(function_inst->OpsPrec64, ctx);
_set_vprec_range_binary64(function_inst->OpsRange64, ctx);
_set_vprec_precision_binary32(function_inst->OpsPrec32, ctx);
_set_vprec_range_binary32(function_inst->OpsRange32, ctx);
}
// treatment of arguments
int new_flag = (function_inst->input_args == NULL && nb_args > 0);
// print function info in log
_vfi_print_log(ctx, "\n");
_vfi_print_log(ctx, "enter in %s\t%d\t%d\t%d\t%d\n", function_inst->id,
function_inst->OpsPrec64, function_inst->OpsRange64,
function_inst->OpsPrec32, function_inst->OpsRange32);
// allocate memory for arguments
if (new_flag) {
function_inst->input_args =
interflop_malloc(sizeof(_vfi_argument_data_t) * nb_args);
function_inst->nb_input_args = nb_args;
}
// boolean which indicates if arguments should be rounded or not depending on
// modes
int mode_flag =
(((ctx->mode == vprecmode_full) || (ctx->mode == vprecmode_ib)) &&
((ctx->vfi->vprec_inst_mode == vprecinst_all) ||
(ctx->vfi->vprec_inst_mode == vprecinst_arg)) &&
ctx->vfi->vprec_inst_mode != vprecinst_none);
for (int i = 0; i < nb_args; i++) {
// get argument type, id and size
int type = va_arg(ap, int);
char *arg_id = va_arg(ap, char *);
unsigned int size = va_arg(ap, unsigned int);
if (new_flag) {
function_inst->input_args[i].data_type = type;
interflop_strcpy(function_inst->input_args[i].arg_id, arg_id);
function_inst->input_args[i].min_range = INT_MAX;
function_inst->input_args[i].max_range = INT_MIN;
function_inst->input_args[i].exponent_length =
(type == FDOUBLE || type == FDOUBLE_PTR)
? VPREC_RANGE_BINARY64_DEFAULT
: VPREC_RANGE_BINARY32_DEFAULT;
function_inst->input_args[i].mantissa_length =
(type == FDOUBLE || type == FDOUBLE_PTR)
? VPREC_PRECISION_BINARY64_DEFAULT
: VPREC_PRECISION_BINARY32_DEFAULT;
}
if (type == FDOUBLE) {
double *value = va_arg(ap, double *);
_vfi_print_log(ctx, " - %s\tinput\tdouble\t%s\t%la\t->\t",
function_inst->id, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary64(
*value, 1, context, function_inst->input_args[i].exponent_length,
function_inst->input_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->input_args[i].min_range =
(interflop_floor(*value) < function_inst->input_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->input_args[i].min_range;
function_inst->input_args[i].max_range =
(interflop_ceil(*value) > function_inst->input_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->input_args[i].max_range;
}
_vfi_print_log(ctx, "%la\t(%d, %d)\n", *value,
function_inst->input_args[i].mantissa_length,
function_inst->input_args[i].exponent_length);
} else if (type == FFLOAT) {
float *value = va_arg(ap, float *);
_vfi_print_log(ctx, " - %s\tinput\tfloat\t%s\t%a\t->\t",
function_inst->id, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary32(
*value, 1, context, function_inst->input_args[i].exponent_length,
function_inst->input_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->input_args[i].min_range =
(interflop_floor(*value) < function_inst->input_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->input_args[i].min_range;
function_inst->input_args[i].max_range =
(interflop_ceil(*value) > function_inst->input_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->input_args[i].max_range;
}
_vfi_print_log(ctx, "%a\t(%d, %d)\n", *value,
function_inst->input_args[i].mantissa_length,
function_inst->input_args[i].exponent_length);
} else if (type == FDOUBLE_PTR) {
double *value = va_arg(ap, double *);
for (unsigned int j = 0; j < size; j++, value++) {
if (value == NULL) {
_vfi_print_log(ctx,
" - %s\tinput[%u]\tdouble_ptr\t%s\tNULL\t->\tNULL\n",
function_inst->id, j, arg_id);
continue;
}
_vfi_print_log(ctx, " - %s\tinput[%u]\tdouble_ptr\t%s\t%la\t->\t",
function_inst->id, j, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary64(
*value, 1, context, function_inst->input_args[i].exponent_length,
function_inst->input_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->input_args[i].min_range =
(interflop_floor(*value) <
function_inst->input_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->input_args[i].min_range;
function_inst->input_args[i].max_range =
(interflop_ceil(*value) >
function_inst->input_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->input_args[i].max_range;
}
_vfi_print_log(ctx, "%la\t(%d, %d)\n", *value,
function_inst->input_args[i].mantissa_length,
function_inst->input_args[i].exponent_length);
}
} else if (type == FFLOAT_PTR) {
float *value = va_arg(ap, float *);
for (unsigned int j = 0; j < size; j++, value++) {
if (value == NULL) {
_vfi_print_log(ctx,
" - %s\tinput[%u]\tfloat_ptr\t%s\tNULL\t->\tNULL\n",
function_inst->id, j, arg_id);
continue;
}
_vfi_print_log(ctx, " - %s\tinput[%u]\tfloat_ptr\t%s\t%a\t->\t",
function_inst->id, j, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary32(
*value, 1, context, function_inst->input_args[i].exponent_length,
function_inst->input_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->input_args[i].min_range =
(interflop_floor(*value) <
function_inst->input_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->input_args[i].min_range;
function_inst->input_args[i].max_range =
(interflop_ceil(*value) >
function_inst->input_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->input_args[i].max_range;
}
_vfi_print_log(ctx, "%a\t(%d, %d)\n", *value,
function_inst->input_args[i].mantissa_length,
function_inst->input_args[i].exponent_length);
}
}
}
// increment depth
ctx->vfi->vprec_log_depth++;
}
// vprec function instrumentation
// Set precision for internal operations and round output arguments for a given
// function call
void _vfi_exit_function(interflop_function_stack_t *stack, void *context,
int nb_args, va_list ap) {
vprec_context_t *ctx = (vprec_context_t *)context;
interflop_function_info_t *function_info = stack->array[stack->top];
// decrement depth
ctx->vfi->vprec_log_depth--;
if (function_info == NULL)
logger_error("Call stack error \n");
_vfi_t *function_inst = vfc_hashmap_get(
ctx->vfi->map, vfc_hashmap_str_function(function_info->id));
// set internal operations precision with parent function values
if (stack->array[stack->top + 1] != NULL) {
interflop_function_info_t *parent_info = stack->array[stack->top + 1];
if (!parent_info->isLibraryFunction && !parent_info->isIntrinsicFunction &&
ctx->vfi->vprec_inst_mode != vprecinst_arg &&
ctx->vfi->vprec_inst_mode != vprecinst_none) {
_vfi_t *function_parent = vfc_hashmap_get(
ctx->vfi->map, vfc_hashmap_str_function(parent_info->id));
if (function_parent != NULL) {
_set_vprec_precision_binary64(function_parent->OpsPrec64, ctx);
_set_vprec_range_binary64(function_parent->OpsRange64, ctx);
_set_vprec_precision_binary32(function_parent->OpsPrec32, ctx);
_set_vprec_range_binary32(function_parent->OpsRange32, ctx);
}
}
}
// treatment of arguments
int new_flag = (function_inst->output_args == NULL && nb_args > 0);
// print function info in log
_vfi_print_log(ctx, "exit of %s\t%d\t%d\t%d\t%d\n", function_inst->id,
function_inst->OpsPrec64, function_inst->OpsRange64,
function_inst->OpsPrec32, function_inst->OpsRange32);
// allocate memory for arguments
if (new_flag) {
function_inst->output_args =
interflop_malloc(sizeof(_vfi_argument_data_t) * nb_args);
function_inst->nb_output_args = nb_args;
}
// boolean which indicates if arguments should be rounded or not depending on
// modes
int mode_flag =
(((ctx->mode == vprecmode_full) || (ctx->mode == vprecmode_ob)) &&
(ctx->vfi->vprec_inst_mode == vprecinst_all ||
ctx->vfi->vprec_inst_mode == vprecinst_arg) &&
ctx->vfi->vprec_inst_mode != vprecinst_none);
for (int i = 0; i < nb_args; i++) {
int type = va_arg(ap, int);
char *arg_id = va_arg(ap, char *);
unsigned int size = va_arg(ap, unsigned int);
if (new_flag) {
// initialize arguments data
function_inst->output_args[i].data_type = type;
interflop_strcpy(function_inst->output_args[i].arg_id, arg_id);
function_inst->output_args[i].exponent_length =
(type == FDOUBLE || type == FDOUBLE_PTR)
? VPREC_RANGE_BINARY64_DEFAULT
: VPREC_RANGE_BINARY32_DEFAULT;
function_inst->output_args[i].mantissa_length =
(type == FDOUBLE || type == FDOUBLE_PTR)
? VPREC_PRECISION_BINARY64_DEFAULT
: VPREC_PRECISION_BINARY32_DEFAULT;
function_inst->output_args[i].min_range = INT_MAX;
function_inst->output_args[i].max_range = INT_MIN;
}
if (type == FDOUBLE) {
double *value = va_arg(ap, double *);
_vfi_print_log(ctx, " - %s\toutput\tdouble\t%s\t%la\t->\t",
function_inst->id, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary64(
*value, 0, context, function_inst->output_args[i].exponent_length,
function_inst->output_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->output_args[i].min_range =
(interflop_floor(*value) <
function_inst->output_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->output_args[i].min_range;
function_inst->output_args[i].max_range =
(interflop_ceil(*value) > function_inst->output_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->output_args[i].max_range;
}
_vfi_print_log(ctx, "%la\t(%d,%d)\n", *value,
function_inst->output_args[i].mantissa_length,
function_inst->output_args[i].exponent_length);
} else if (type == FFLOAT) {
float *value = va_arg(ap, float *);
_vfi_print_log(ctx, " - %s\toutput\tfloat\t%s\t%a\t->\t",
function_inst->id, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary32(
*value, 0, context, function_inst->output_args[i].exponent_length,
function_inst->output_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->output_args[i].min_range =
(interflop_floor(*value) <
function_inst->output_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->output_args[i].min_range;
function_inst->output_args[i].max_range =
(interflop_ceil(*value) > function_inst->output_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->output_args[i].max_range;
}
_vfi_print_log(ctx, "%a\t(%d, %d)\n", *value,
function_inst->output_args[i].mantissa_length,
function_inst->output_args[i].exponent_length);
} else if (type == FDOUBLE_PTR) {
double *value = va_arg(ap, double *);
for (unsigned int j = 0; j < size; j++, value++) {
if (value == NULL) {
_vfi_print_log(ctx,
" - %s\toutput[%u]\tdouble_ptr\t%s\tNULL\t->\tNULL\n",
function_inst->id, j, arg_id);
continue;
}
_vfi_print_log(ctx, " - %s\toutput[%u]\tdouble_ptr\t%s\t%la\t->\t",
function_inst->id, j, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary64(
*value, 0, context, function_inst->output_args[i].exponent_length,
function_inst->output_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->output_args[i].min_range =
(interflop_floor(*value) <
function_inst->output_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->output_args[i].min_range;
function_inst->output_args[i].max_range =
(interflop_ceil(*value) >
function_inst->output_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->output_args[i].max_range;
}
_vfi_print_log(ctx, "%la\t(%d,%d)\n", *value,
function_inst->output_args[i].mantissa_length,
function_inst->output_args[i].exponent_length);
}
} else if (type == FFLOAT_PTR) {
float *value = va_arg(ap, float *);
for (unsigned int j = 0; j < size; j++, value++) {
if (value == NULL) {
_vfi_print_log(ctx,
" - %s\toutput[%u]\tfloat_ptr\t%s\tNULL\t->\tNULL\n",
function_inst->id, j, arg_id);
continue;
}
_vfi_print_log(ctx, " - %s\toutput[%u]\tfloat_ptr\t%s\t%a\t->\t",
function_inst->id, j, arg_id, *value);
if ((!new_flag) && mode_flag) {
*value = _vprec_round_binary32(
*value, 0, context, function_inst->output_args[i].exponent_length,
function_inst->output_args[i].mantissa_length);
}
if (!(interflop_isnan(*value) || interflop_isinf(*value))) {
function_inst->output_args[i].min_range =
(interflop_floor(*value) <
function_inst->output_args[i].min_range ||
new_flag)
? interflop_floor(*value)
: function_inst->output_args[i].min_range;
function_inst->output_args[i].max_range =
(interflop_ceil(*value) >
function_inst->output_args[i].max_range ||
new_flag)
? interflop_ceil(*value)
: function_inst->output_args[i].max_range;
}
_vfi_print_log(ctx, "%a\t(%d, %d)\n", *value,
function_inst->output_args[i].mantissa_length,
function_inst->output_args[i].exponent_length);
}
}
}
_vfi_print_log(ctx, "\n");
}