-
Notifications
You must be signed in to change notification settings - Fork 1
/
fxp_tester.c
1901 lines (1768 loc) · 75.9 KB
/
fxp_tester.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
/* SPDX-License-Identifier: MIT */
/*
* fxp_tester.c
* Tests the implementation of binary fixed point numbers
* (fxp.c and fxp_l.c)
*
* By Raul Saavedra, Bonn Germany
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <float.h>
#include <time.h>
#include <math.h>
#include "fxp.h"
#include "fxp_extern.h"
#include "fxp_l.h"
#include "fxp_aux.h"
#include "fxp_conv.h"
#include "print_as_bits.h"
#include "ulongy.h"
#define DASHES "============================================================\n"
// Set to 0 in order to be able to replicate runs
#define SET_RAND_SEED 0
#define TEST_WITH_RANDS 1
#define MAX_RAND_LOOPS 5
//#define MAX_RAND_LOOPS 5000
#define TEST_BASICS 1
#define TEST_SUPER_FXP_L 0
#define TEST_LOGARITHMS 1
#define TEST_LG2_MUL_L 0
#define TEST_POWERS 1
#define TEST_SQRT 1
#define TEST_POWXY 1
#define AVOID_EXTREME_INPUTS_FOR_POWXY 1
#define TEST_TRIGONOM 1
// Max allowed error will be WDELTA_MAX * tiniest (least signif frac bit)
#define WDELTA_MAX 3.0L
// Warnings will start appearing when error >= MIN_DELTA = WDELTA_MAX*tiniest / WDELTA_DIV
#define WDELTA_DIV 1.2L
static int fracbit_configs[] = {8, 11, 16, 24, 28, 31};
//static int fracbit_configs[] = {16};
/*
static int fracbit_configs[] = {
31, 30,
29, 28, 27, 26, 25, 24, 23, 22, 21, 20,
19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
9, 8, 7, 6, 5, 4
};
*/
#define NINTBITS (sizeof(int) * 8)
#define NLONGBITS (sizeof(long) * 8)
// Only test fxp_l.c functions when applicablle
const static int TEST_L = (NLONGBITS >= (2 * NINTBITS));
static unsigned int fracbit_nwarnings[NINTBITS];
static unsigned int fracbit_threshold_cases[NINTBITS];
static unsigned int fracbit_extr_powxy_cases[NINTBITS];
static long double fracbit_maxdelta_allowed[NINTBITS];
static long double fracbit_maxdelta_observed[NINTBITS];
static long double min_warn_delta = 0.0;
static long double max_warn_delta = 0.0;
static long double larger_delta = 0.0;
static long double largest_delta = 0.0;
static long double largest_madelta = 0.0;
static int largest_delta_fbits = 0;
static unsigned long twarnings = 0;
static unsigned long nwarnings = 0;
static unsigned long ttests = 0;
static int frac_bits;
static int whole_bits;
static int frac_mask;
static int frac_max;
static int frac_max_dec;
static int whole_max;
static int whole_min;
static int fxp_largest;
const long double PI_AS_LD = acosl(-1.0L);
const long double RAD_TO_GRAD = 180.0L / PI_AS_LD;
const long double GRAD_TO_RAD = PI_AS_LD / 180.0L;
static void test_fxp(char *s, long double d_assert_val, int fxp1)
{
ttests++;
printf("%s\n", s);
printf(" exp: ");
if (d_assert_val <= FXP_UNDEF_LD)
printf("UNDEF");
else if (d_assert_val <= FXP_NINF_LD)
printf("-INF");
else if (d_assert_val >= FXP_PINF_LD)
printf("+INF");
else {
printf("%17.10Le", d_assert_val);
}
printf("\n act: ");
print_fxp(fxp1);
printf("\n");
int b1 = ((fxp1 == FXP_UNDEF) && (d_assert_val == FXP_UNDEF_LD));
int b2 = ((fxp1 == FXP_NEG_INF) && (d_assert_val == FXP_NINF_LD));
int b3 = ((fxp1 == FXP_POS_INF) && (d_assert_val == FXP_PINF_LD));
if (b1 || b2 || b3) {
// Expected and actual are both same-signed infinities,
// or both undef. Either way, they're the same
//printf(" (~same)\n");
return;
}
fflush(stdout);
// Make sure it's not an UNDEF vs. NON-UNDEF case
assert((fxp1 != FXP_UNDEF) && (d_assert_val != FXP_UNDEF_LD));
long double df = fxp2ld(fxp1);
long double delta;
char * msg;
if ((fxp1 == FXP_POS_INF) || (d_assert_val == FXP_PINF_LD)) {
// One of them +infinity, but not the other ->
// Calculate delta of the latter with respect to
// the +INF threshold (FXP_max_ldx)
delta = FXP_max_ldx - ((fxp1 == FXP_POS_INF)? d_assert_val: df);
msg = ", rare case of extremely close values on either side of +INF threshold";
} else if ((fxp1 == FXP_NEG_INF) || (d_assert_val == FXP_NINF_LD)) {
// One of them -infinity, but not the other ->
// Calculate delta of the latter with respect to
// the -INF threshold (FXP_min_ldx)
delta = ((fxp1 == FXP_NEG_INF)? d_assert_val: df) - FXP_min_ldx;
msg = ", rare case of extremely close values on either side of -INF threshold";
} else {
// Values are either both within infinity thresholds
// (normal case,) or both are inf's with different signs
delta = (df >= d_assert_val)?
df - d_assert_val:
d_assert_val - df;
msg = "";
}
if (delta <= min_warn_delta) {
// No warning up to the min_warn_delta value
if (msg[0] != '\0') {
printf(" (~same%s)\n", msg);
fracbit_threshold_cases[frac_bits]++;
}
} else {
nwarnings++;
fracbit_nwarnings[frac_bits]++;
printf("\n***** Warning %lu: d=%1.2LE for %1.2LE (from %1.2LE to MAX %1.2LE allowed for %d f.bits)\n",
nwarnings, delta, df,
min_warn_delta,
max_warn_delta,
frac_bits);
if (delta > larger_delta) {
larger_delta = delta;
printf("***** For %d f.bits, that's the largest delta so far: %LE\n", \
frac_bits, larger_delta);
fracbit_maxdelta_observed[frac_bits] = larger_delta;
}
printf("\n");
}
fflush(stdout);
// Here assert that we never exceed the max_warn_delta
assert( (delta <= max_warn_delta) &&
((df >= 0 && d_assert_val >= 0) ||
(df <= 0 && d_assert_val <= 0)));
}
static long double get_target(long double x)
{
if (x <= FXP_UNDEF_LD) return FXP_UNDEF_LD;
if (x <= FXP_min_ldx) return FXP_NINF_LD;
if (x >= FXP_max_ldx) return FXP_PINF_LD;
return x;
}
static long double get_lg2_target(int x)
{
if (x < 0) return FXP_UNDEF_LD;
if (x == 0) return FXP_NINF_LD;
if (x == FXP_POS_INF) return FXP_PINF_LD;
return get_target(log2l(fxp2ld(x)));
}
static long double get_ln_target(int x)
{
if (x < 0) return FXP_UNDEF_LD;
if (x == 0) return FXP_NINF_LD;
if (x == FXP_POS_INF) return FXP_PINF_LD;
return get_target(logl(fxp2ld(x)));
}
static long double get_lg10_target(int x)
{
if (x < 0) return FXP_UNDEF_LD;
if (x == 0) return FXP_NINF_LD;
if (x == FXP_POS_INF) return FXP_PINF_LD;
return get_target(log10l(fxp2ld(x)));
}
static long double get_pow2_target(int x)
{
if (x == FXP_UNDEF) return FXP_UNDEF_LD;
if (x == FXP_NEG_INF) return 0.0L;
if (x == FXP_POS_INF) return FXP_PINF_LD;
long double ldvalue = powl(2.0L, fxp2ld(x));
long double target = get_target(ldvalue);
return target;
}
static long double get_exp_target(int x)
{
if (x == FXP_UNDEF) return FXP_UNDEF_LD;
if (x == FXP_NEG_INF) return 0.0L;
if (x == FXP_POS_INF) return FXP_PINF_LD;
return get_target(expl(fxp2ld(x)));
}
static long double get_pow10_target(int x)
{
if (x == FXP_UNDEF) return FXP_UNDEF_LD;
if (x == FXP_NEG_INF) return 0.0L;
if (x == FXP_POS_INF) return FXP_PINF_LD;
return get_target(powl(10.0L, fxp2ld(x)));
}
static long double get_sqrt_target(int x)
{
if ((x == FXP_UNDEF) || (x < 0)) return FXP_UNDEF_LD;
if (x == FXP_POS_INF) return FXP_PINF_LD;
return get_target(sqrtl(fxp2ld(x)));
}
/*
static long double get_rsqrt_target(int x)
{
if ((x == FXP_UNDEF) || (x <= 0)) return FXP_UNDEF_LD;
if (x == FXP_POS_INF) return 0;
return get_target(1.0L/sqrtl(fxp2ld(x)));
}
*/
/*
Expected output from 2^( y * lg2(x) ) given the combination of inputs x and y:
x y: Und. -INF [MIN,-1) -1 (-1,0) 0 (0,1) 1 (1,MAX] +INF
< 0 Und. Und. Und. Und. Und. Und. Und. Und. Und. Und.
0 Und. +INF +INF +INF +INF Und. 0 0 0 0
(0,1) Und. 0 * * * 1 * * * 0
1 Und. Und. 1 1 1 1 1 1 1 Und.
(1,MAX] Und. 0 * * * 1 * * * +INF
+INF Und. 0 0 0 0 Und. +INF +INF +INF +INF
*/
static long double get_powxy_target(int x, int y)
{
if ((x < 0) || (y == FXP_UNDEF)) {
return FXP_UNDEF_LD;
}
if (x == 0) {
return (y < 0)? FXP_PINF_LD: \
(y == 0)? FXP_UNDEF_LD: 0.0L;
}
if (x == FXP_POS_INF) {
return (y < 0)? 0.0L: \
(y == 0)? FXP_UNDEF_LD: FXP_PINF_LD;
}
if (y == FXP_NEG_INF) {
return (x == FXP_one)? FXP_UNDEF_LD: 0.0L;
}
if (y == FXP_POS_INF) {
return (x < FXP_one)? 0.0L: \
(x == FXP_one)? FXP_UNDEF_LD: FXP_PINF_LD;
}
return ((x == FXP_one) || (y == 0))? 1.0L: \
get_target(powl(fxp2ld(x), fxp2ld(y)));
}
static long double get_div_target(int x, int y)
{
long double target;
if ((x == FXP_UNDEF) || (y == FXP_UNDEF) \
|| ((x == 0) && (y == 0)) \
|| ((x == FXP_POS_INF) && (y == FXP_POS_INF)) \
|| ((x == FXP_POS_INF) && (y == FXP_NEG_INF)) \
|| ((x == FXP_NEG_INF) && (y == FXP_POS_INF)) \
|| ((x == FXP_NEG_INF) && (y == FXP_NEG_INF))) {
target = FXP_UNDEF_LD;
} else {
if (((x > 0) && (y == 0)) \
|| ((x == FXP_POS_INF) && (y > 0)) \
|| ((x == FXP_NEG_INF) && (y < 0))) {
target = FXP_PINF_LD;
} else {
if (((x < 0) && (y == 0)) \
|| ((x == FXP_NEG_INF) && (y > 0)) \
|| ((x == FXP_POS_INF) && (y < 0))) {
target = FXP_NINF_LD;
} else {
if ((y == FXP_POS_INF) || (y == FXP_NEG_INF)) {
target = 0;
} else {
long double ldx = fxp2ld(x);
long double ldy = fxp2ld(y);
target = ldx / ldy;
if (target >= FXP_max_ldx) {
target = FXP_PINF_LD;
} else {
if (target <= FXP_min_ldx) {
target = FXP_NINF_LD;
}
}
}
}
}
}
return target;
}
static long double get_mul_target(int x, int y)
{
long double target;
if (((x == FXP_UNDEF) || (y == FXP_UNDEF)) \
|| ((x == 0) && ((y == FXP_POS_INF) || (y == FXP_NEG_INF))) \
|| ((y == 0) && ((x == FXP_POS_INF) || (x == FXP_NEG_INF)))) {
target = FXP_UNDEF_LD;
} else {
if ((x == FXP_POS_INF) || (x == FXP_NEG_INF)) {
if (y < 0)
target = (x == FXP_POS_INF)? FXP_NINF_LD: FXP_PINF_LD;
else
target = (x == FXP_POS_INF)? FXP_PINF_LD: FXP_NINF_LD;
} else {
if ((y == FXP_POS_INF) || (y == FXP_NEG_INF)) {
if (x < 0)
target = (y == FXP_POS_INF)? FXP_NINF_LD: FXP_PINF_LD;
else
target = (y == FXP_POS_INF)? FXP_PINF_LD: FXP_NINF_LD;
} else {
target = fxp2ld(x) * fxp2ld(y);
if (target <= FXP_min_ldx)
target = FXP_NINF_LD;
else if (target >= FXP_max_ldx)
target = FXP_PINF_LD;
}
}
}
return target;
}
static long double get_f_target(int x)
{
if (x == FXP_UNDEF) return FXP_UNDEF_LD;
if (x == FXP_NEG_INF) return FXP_NINF_LD;
if (x == FXP_POS_INF) return FXP_PINF_LD;
float f = fxp2f(x);
if (f <= FXP_min_fx) return FXP_NINF_LD;
if (f >= FXP_max_fx) return FXP_PINF_LD;
return (long double) f;
}
static long double get_cos_target(int x)
{
if ((x == FXP_UNDEF) || (x == FXP_NEG_INF) || (x == FXP_POS_INF))
return FXP_UNDEF_LD;
long double ldcos = cosl(fxp2ld(x));
long double target = get_target(ldcos);
return target;
}
static long double get_sin_target(int x)
{
if ((x == FXP_UNDEF) || (x == FXP_NEG_INF) || (x == FXP_POS_INF))
return FXP_UNDEF_LD;
long double ldsin = sinl(fxp2ld(x));
long double target = get_target(ldsin);
return target;
}
static long double get_tan_target(int x)
{
if ((x == FXP_UNDEF) || (x == FXP_NEG_INF) || (x == FXP_POS_INF))
return FXP_UNDEF_LD;
long double ldtan = tanl(fxp2ld(x));
long double target = get_target(ldtan);
return target;
}
static long double get_asin_target(int x)
{
if ((x > FXP_one) || (x < FXP_minus_one)) return FXP_UNDEF_LD;
long double ldasin = asinl(fxp2ld(x));
long double target = get_target(ldasin);
return target;
}
static long double get_acos_target(int x)
{
if ((x > FXP_one) || (x < FXP_minus_one)) return FXP_UNDEF_LD;
long double ldacos = acosl(fxp2ld(x));
long double target = get_target(ldacos);
return target;
}
static long double get_atan_target(int x)
{
if (x == FXP_UNDEF) return FXP_UNDEF_LD;
long double ldatan = atanl(fxp2ld(x));
long double target = get_target(ldatan);
return target;
}
static int FXP_TINIEST = 1;
static long double dfxp_tiniest;
static int zero;
static int fxp_ten;
static int fxp_two;
static int fxp_one;
static int fxp_point5;
static int fxp_halfmax;
static int fxp_halfp2;
void tests_01()
{
printf("\nChecking extreme int values, part I, ");
printf("frac bits: %d\n", frac_bits);
test_fxp("+Inf",
FXP_PINF_LD,
FXP_POS_INF);
test_fxp("Largest",
fxp2ld(FXP_MAX),
fxp_largest);
test_fxp("HalfMax",
fxp2ld(FXP_MAX)/2,
fxp_halfmax);
test_fxp("0.5",
((long double) 0.5),
fxp_point5);
test_fxp("tiniest",
dfxp_tiniest,
FXP_TINIEST);
test_fxp("zero",
0.0,
fxp_bin(0, 0));
test_fxp("-tiniest",
fxp2ld(-FXP_TINIEST),
-FXP_TINIEST);
test_fxp("Most negative",
fxp2ld(FXP_MIN),
-fxp_largest);
test_fxp("-Inf",
FXP_NINF_LD,
FXP_NEG_INF);
test_fxp("Undefined",
FXP_UNDEF_LD,
FXP_UNDEF);
}
void tests_02()
{
printf("\nChecking extreme int values, part II, ");
printf("frac bits: %d\n", frac_bits);
test_fxp("Almost most negative",
fxp2ld(-fxp_largest) + dfxp_tiniest,
fxp_add(-fxp_largest, FXP_TINIEST));
test_fxp(" Largest -Largest",
0.0,
fxp_add(fxp_largest, -fxp_largest));
test_fxp("-Largest +Largest",
0.0,
fxp_add(-fxp_largest, fxp_largest));
test_fxp("Largest + 0",
fxp2ld(fxp_largest),
fxp_add(fxp_largest, zero));
test_fxp("-Largest - 0",
fxp2ld(-fxp_largest),
fxp_add(-fxp_largest, -zero));
test_fxp("Largest - tiniest",
fxp2ld(fxp_largest) - dfxp_tiniest,
fxp_sub(fxp_largest, FXP_TINIEST));
test_fxp("Largest + tiniest safe",
FXP_PINF_LD,
fxp_add(fxp_largest, FXP_TINIEST));
test_fxp("Largest + tiniest unsafe",
FXP_PINF_LD,
fxp_unsafe_add(fxp_largest, FXP_TINIEST));
test_fxp("-(+inf)",
FXP_NINF_LD,
-FXP_POS_INF);
test_fxp("-(-inf)",
FXP_PINF_LD,
-FXP_NEG_INF);
test_fxp("+inf + +inf",
FXP_PINF_LD,
fxp_add(FXP_POS_INF, FXP_POS_INF));
test_fxp("-inf - +inf",
FXP_NINF_LD,
fxp_sub(FXP_NEG_INF, FXP_POS_INF));
test_fxp("+inf + -inf",
FXP_UNDEF_LD,
fxp_add(FXP_POS_INF, FXP_NEG_INF));
test_fxp("-inf + -inf",
FXP_NINF_LD,
fxp_add(FXP_NEG_INF, FXP_NEG_INF));
test_fxp("-inf - -inf",
FXP_UNDEF_LD,
fxp_sub(FXP_NEG_INF, FXP_NEG_INF));
test_fxp("+inf * -inf",
FXP_NINF_LD,
fxp_mul(FXP_POS_INF, FXP_NEG_INF));
test_fxp("+inf - 0.5",
FXP_PINF_LD,
fxp_sub(FXP_POS_INF, fxp_point5));
test_fxp("-inf + 0.5",
FXP_NINF_LD,
fxp_add(FXP_NEG_INF, fxp_point5));
test_fxp("+num / zero",
FXP_PINF_LD,
fxp_div(fxp_largest, zero));
test_fxp("zero / zero",
FXP_UNDEF_LD,
fxp_div(zero, zero));
test_fxp("zero * zero",
0.0,
fxp_mul(zero, zero));
test_fxp("zero + zero",
0.0,
fxp_add(zero, zero));
test_fxp("zero - zero",
0.0,
fxp_sub(zero, zero));
test_fxp("zero - undef",
FXP_UNDEF_LD,
fxp_sub(zero, FXP_UNDEF));
test_fxp("-num / zero",
FXP_NINF_LD,
fxp_div(-fxp_largest, zero));
test_fxp("zero * +inf",
FXP_UNDEF_LD,
fxp_mul(zero, FXP_POS_INF));
test_fxp("zero * -inf",
FXP_UNDEF_LD,
fxp_mul(zero, FXP_NEG_INF));
test_fxp("zero * undef",
FXP_UNDEF_LD,
fxp_mul(zero, FXP_UNDEF));
test_fxp("-inf * undef",
FXP_UNDEF_LD,
fxp_mul(FXP_NEG_INF, FXP_UNDEF));
test_fxp("+inf * undef",
FXP_UNDEF_LD,
fxp_mul(FXP_POS_INF, FXP_UNDEF));
test_fxp("undef * undef",
FXP_UNDEF_LD,
fxp_mul(FXP_UNDEF, FXP_UNDEF));
test_fxp("tiniest * inf",
FXP_PINF_LD,
fxp_mul(FXP_TINIEST, FXP_POS_INF));
}
void tests_03()
{
printf("\nChecking extreme int values, part III, ");
printf("frac bits: %d\n", frac_bits);
test_fxp("Way Too Large whole part!",
FXP_PINF_LD,
fxp_add(fxp(whole_max), fxp(5)));
test_fxp("Largest * 1",
fxp2ld(fxp_mul(fxp_largest, fxp(1))),
fxp_mul(fxp_largest, fxp_one));
test_fxp("Largest * -1",
fxp2ld(fxp_mul(fxp_largest, fxp(-1))),
fxp_mul(fxp_largest, -fxp_one));
test_fxp("Largest + two safe",
FXP_PINF_LD,
fxp_add(fxp_largest, fxp_two));
test_fxp("Largest + two unsafe",
fxp2ld(fxp_largest + fxp_two),
fxp_unsafe_add(fxp_largest, fxp_two));
test_fxp("Safe Too neg substraction",
FXP_NINF_LD,
fxp_sub( -fxp_largest, fxp_point5));
test_fxp("Unsafe Too neg substraction",
fxp2ld(-fxp_largest - fxp_point5),
fxp_unsafe_sub( -fxp_largest, fxp_point5));
test_fxp("Largest + 0.5",
FXP_PINF_LD,
fxp_add(fxp_largest, fxp_point5));
test_fxp("-Largest - 0.5",
FXP_NINF_LD,
fxp_add(-fxp_largest, -fxp_point5));
test_fxp("+HalfMax + HMaxp2",
FXP_PINF_LD,
fxp_add(fxp_halfmax, fxp_halfp2));
test_fxp("-HalfMax - HMaxp2",
FXP_NINF_LD,
fxp_add(-fxp_halfmax, -fxp_halfp2));
test_fxp("HalfMax + HalfMax",
fxp2ld(fxp_halfmax + fxp_halfmax),
fxp_add(fxp_halfmax, fxp_halfmax));
test_fxp("FXP_MAX - HalfMax",
fxp2ld(FXP_MAX - fxp_halfmax),
fxp_sub(FXP_MAX, fxp_halfmax));
test_fxp("HalfMax + FXP_MAX",
FXP_PINF_LD,
fxp_add(fxp_halfmax, FXP_MAX));
test_fxp("-FXP_MAX - HalfMax",
FXP_NINF_LD,
fxp_sub(-FXP_MAX, fxp_halfmax));
test_fxp("HalfMax * 2",
fxp2ld(fxp_mul(fxp_halfmax, fxp(2))),
fxp_mul(fxp_halfmax, fxp(2)));
test_fxp("HalfMax * 3",
FXP_PINF_LD,
fxp_mul(fxp_halfmax, fxp(3)));
test_fxp("-HalfMax * 3",
FXP_NINF_LD,
fxp_mul(-fxp_halfmax, fxp(3)));
test_fxp("(HalfMax+0.5)*2",
FXP_PINF_LD,
fxp_mul(fxp_add(fxp_halfmax, fxp_point5), fxp(2)));
if (TEST_L) {
test_fxp("HalfMax * 2 (long)",
fxp2ld(fxp_mul_l(fxp_halfmax, fxp(2))),
fxp_mul_l(fxp_halfmax, fxp(2)));
test_fxp("(HalfMax+0.5)*2 (long)",
FXP_PINF_LD,
fxp_mul_l(fxp_add(fxp_halfmax, fxp_point5), fxp(2)));
}
}
void test_decbin_mappings()
{
printf("\nChecking decimal <=> bin mappings of frac ranges, ");
printf("frac bits: %d\n", frac_bits);
int fmbin = frac_max;
int fmdec = frac_max_dec;
printf("Max frac dec: %d (bin %d)", fmdec, fmbin);
for (int i = 0; i <= 5; i++) {
printf("\nShowing fxp for 0.%7d: ", i);
int vf = fxp_dec(0, i);
print_fxp(vf);
}
printf("\n:");
int m = (fmdec + 1) / 2;
for (int i = (m >= 2 ? m - 2: 0); i <= m + 2; i++) {
printf("\nShowing fxp for 0.%7d: ", i);
int vf = fxp_dec(0, i);
print_fxp(vf);
}
printf("\n:");
for (int i = (fmdec >= 5? fmdec - 5: 0);
i <= fmdec; i++) {
printf("\nShowing fxp for 0.%7d: ", i);
int vf = fxp_dec(0, i);
print_fxp(vf);
}
printf("\n");
}
void test_fracs()
{
printf("\nChecking sign taken from frac when whole == 0, ");
printf("frac bits: %d\n", frac_bits);
// Temporary switching to a fixed frac_max_dec for these tests,
// and relaxing the max allowed delta
long double bkp_minwd = min_warn_delta;
long double bkp_ld = larger_delta;
long double bkp_maxwd = max_warn_delta;
int fmdec = fxp_get_frac_max_dec();
fxp_set_frac_max_dec(999);
min_warn_delta = fxp2ld(fxp_dec(0,100));
max_warn_delta = min_warn_delta * 2;
int a = fxp_dec(-0, 500);
int b = fxp_dec(-0, -500);
printf("-0. 500: "); print_fxp(a); printf("\n");
printf("-0.-500: "); print_fxp(b); printf("\n");
test_fxp("-0.(+)500", 0.5, fxp_dec(-0, 500));
test_fxp("-0.(-)500", -0.5, fxp_dec(-0, -500));
printf("\nChecking truncation of longer frac decimal arguments, ");
printf("frac bits: %d\n", frac_bits);
test_fxp("0.22222", 0.222, fxp_dec(0, 22222));
test_fxp("0.444444", 0.444, fxp_dec(0, 444444));
test_fxp("0.771999", 0.771, fxp_dec(0, 771999));
test_fxp("0.999999", 0.999, fxp_dec(0, 999999));
// Restoring original frac_max_dec and delta vars
fxp_set_frac_max_dec(fmdec);
min_warn_delta = bkp_minwd;
larger_delta = bkp_ld;
max_warn_delta = bkp_maxwd;
printf("\nChecking extreme frac values, ");
printf("frac bits: %d\n", frac_bits);
test_fxp("Largest frac",
fxp2ld(frac_max),
frac_max);
test_fxp("-Largest frac",
fxp2ld(-frac_max),
-frac_max);
test_fxp("0.5 + fracmax",
((whole_bits > 1)?
0.5 + fxp2ld(frac_max):
fxp2ld(FXP_POS_INF)),
fxp_add(fxp_point5, frac_max));
test_fxp("fracmax +tiniest",
fxp2ld(fxp_one),
fxp_add(frac_max, FXP_TINIEST));
test_fxp("-fracmax -tiniest",
fxp2ld(-fxp_one),
fxp_add(-frac_max, -FXP_TINIEST));
test_fxp("fracmax - fracmax",
0.0,
fxp_add(frac_max, -frac_max));
}
void test_ops_with_whole_bits()
{
if (whole_bits < 3) return;
printf("\nChecking simple ops when using 3+ bits for whole part:\n");
int whole = 0;
int bin_frac = 1u << (frac_bits - 1); // == 0.500
int num = fxp_bin(whole, bin_frac);
int dec_frac = fxp_get_frac_part_dec(num);
int fxp1 = num;
int fxp2 = fxp(2);
long double dnum = fxp2ld(num);
test_fxp(" 1 + 1",
fxp2ld(fxp2),
fxp_add(fxp_one, fxp_one));
test_fxp("-1 - 1",
fxp2ld(-fxp2),
fxp_add(-fxp_one, -fxp_one));
test_fxp("Ok sum == 2",
fxp2ld(fxp_add(-fxp_halfmax, fxp_halfp2)),
fxp_add(-fxp_halfmax, fxp_halfp2));
test_fxp("Ok sum == -2",
fxp2ld(fxp_add(fxp_halfmax, -fxp_halfp2)),
fxp_add(fxp_halfmax, -fxp_halfp2));
test_fxp(" num", dnum, fxp1);
test_fxp(" num + 2",
dnum + 2.0,
fxp_add(fxp1, fxp2));
test_fxp(" num + -2",
dnum - 2.0,
fxp_add(fxp1, -fxp2));
test_fxp("-num + 2",
-dnum + 2.0,
fxp_add(-fxp1, fxp2));
test_fxp("-num + -2",
-dnum - 2.0,
fxp_add(-fxp1, -fxp2));
test_fxp(" num - 2",
dnum - 2.0,
fxp_sub(fxp1, fxp2));
test_fxp(" num - -2",
dnum + 2.0,
fxp_sub(fxp1, -fxp2));
test_fxp("-num - 2",
-dnum - 2.0,
fxp_sub(-fxp1, fxp2));
test_fxp("-num - -2",
-dnum + 2.0,
fxp_sub(-fxp1, -fxp2));
test_fxp(" num * 2",
dnum * 2.0,
fxp_mul(fxp1, fxp2));
test_fxp(" num * -2",
dnum * -2.0,
fxp_mul(fxp1, -fxp2));
test_fxp("-num * 2",
-dnum * 2.0,
fxp_mul(-fxp1, fxp2));
test_fxp("-num * -2",
-dnum * -2.0,
fxp_mul(-fxp1, -fxp2));
if (TEST_L) {
test_fxp(" num * 2 (long)",
dnum * 2.0,
fxp_mul_l( fxp1, fxp2));
test_fxp(" num * -2 (long)",
dnum * -2.0,
fxp_mul_l( fxp1, -fxp2));
test_fxp("-num * 2 (long)",
-dnum * 2.0,
fxp_mul_l(-fxp1, fxp2));
test_fxp("-num * -2 (long)",
-dnum * -2.0,
fxp_mul_l(-fxp1, -fxp2));
}
test_fxp(" num / 2",
dnum / 2.0,
fxp_div( fxp1, fxp2));
test_fxp(" num / -2",
dnum / -2.0,
fxp_div(fxp1, -fxp2));
test_fxp("-num / 2",
-dnum / 2.0,
fxp_div(-fxp1, fxp2));
test_fxp("-num / -2",
-dnum / -2.0,
fxp_div(-fxp1, -fxp2));
}
void test_ops_with_values_of_interest()
{
int sign1, sign2, sign3, n1, n2, n3, n4, fxp1, fxp2;
long double ldx, ldy, ldz, tgt1, tgt2;
//printf("\nVerifying multiplication with values of interest:\n");
printf("\nVerifying ops with values of interest, ");
printf("frac bits: %d\n", frac_bits);
int ax[] = {
FXP_UNDEF, FXP_NEG_INF, FXP_POS_INF, FXP_MAX,
fxp_dec(33333, 333), fxp_dec(33, 33),
fxp(2),
fxp(1),
fxp_bin(0, FXP_frac_max),
1, 0, fxp_bin(0, -1),
fxp_bin(0, -FXP_frac_max),
fxp(-1),
fxp(-2),
fxp_dec(-33, 33), fxp_dec(-33333, 333),
-1046690937,
FXP_MIN
};
int ay[] = {
FXP_UNDEF, FXP_NEG_INF, FXP_POS_INF, FXP_MAX,
fxp(2), fxp(1),
fxp_bin(0, FXP_frac_max),
2, 1, 0, -1, -2,
fxp(-1), fxp(-2),
-1094861345,
FXP_MIN
};
int x, y, posx, posy;
int ndd = (int) (sizeof(ax) / sizeof(int));
int ndr = (int) (sizeof(ay) / sizeof(int));
for (int i = 0; i < ndd; i++) {
x = ax[i];
fflush(stdout);
// Test that round trip conversions fxp -> double or
// long double -> fxp result in exactly the same fxp
assert( d2fxp(fxp2d(x)) == x );
assert( ld2fxp(fxp2ld(x)) == x );
// With float conversions we cannot expect perfect
// accurary, but we can check that the
// conversion keeps values very close
if ((i > 2) && ((x == FXP_POS_INF) \
|| (x == FXP_NEG_INF))) {
continue;
}
float fx = fxp2f(x);
int x2 = f2fxp(fx);
printf("\nRoundtrip fxp->float->fxp conversion of x:\n");
test_fxp("x: ", get_f_target(x), x2);
for (int j = 0; j<ndr; j++) {
y = ay[j];
if ((j > 2) && ((y == FXP_POS_INF) \
|| (y == FXP_NEG_INF))) {
continue;
}
printf("\nx : "); print_fxp(x); printf("\n");
printf("y : "); print_fxp(y); printf("\n");
ldx = fxp2ld(x);
ldy = fxp2ld(y);
//For multiplication
tgt1 = get_mul_target(x, y);
//n1 = fxp_mul_l(x, y);
//test_fxp("\nmul_l (x*y)", tgt1, n1);
n1 = fxp_mul(x, y);
test_fxp("\nmul (x*y)", tgt1, n1);
//For division
tgt1 = get_div_target(x, y);
n1 = fxp_div(x, y);
test_fxp("div (x/y)", tgt1, n1);
}
}
}
void test_super_fxp_l()
{
printf("\nTesting super_fxp_l\n");
int pos = fxp_bin(5, 5);
super_fxp_l spos = sfxp_l_from_fxp(pos);
int vpos = sfxp_l_2_fxp(spos);
test_fxp("5.5<->sfxp_l", fxp2ld(pos), vpos);
int v0 = sfxp_l_2_fxp(sfxp_l_from_fxp(0));
test_fxp("0<->sfxp_l", 0.0L, v0);
int neg = -pos;
super_fxp_l sneg = sfxp_l_from_fxp(neg);
int vneg = sfxp_l_2_fxp(sneg);
test_fxp("-5.5<->sfxp_l", fxp2ld(neg), vneg);
}
void test_lg2(char * msg, int x)
{
long double tgt = get_lg2_target(x);
if (TEST_L) {
printf("lg2_l(");
test_fxp(msg, tgt, fxp_lg2_l(x));
}
printf("lg2(");
test_fxp(msg, tgt, fxp_lg2(x));
}
void test_lg2_mul_l(char * msg, int x)
{
printf("lg2_mul_l(");
test_fxp(msg, get_lg2_target(x), fxp_lg2_mul_l(x));
}
void test_ln(char * msg, int x)
{
long double tgt = get_ln_target(x);
if (TEST_L) {
printf("ln_l(");
test_fxp(msg, tgt, fxp_ln_l(x));
}
printf("ln(");
test_fxp(msg, tgt, fxp_ln(x));
}
void test_lg10(char * msg, int x)
{
long double tgt = get_lg10_target(x);
if (TEST_L) {
printf("lg10_l(");
test_fxp(msg, tgt, fxp_lg10_l(x));
}
printf("lg10(");
test_fxp(msg, tgt, fxp_lg10(x));
}
void test_log(char base, char * msg, int x)
{
switch(base) {
case '2':
test_lg2(msg, x);
break;
case 'M':
test_lg2_mul_l(msg, x);
break;
case 'A':
test_lg10(msg, x);
break;
default: // 'e'
test_ln(msg, x);
break;
}
}
void test_logarithms()
{
printf("\nShowing Transcendental constants as fxp's: ");
printf("frac bits: %d\n", frac_bits);
printf("e : "); print_fxp(fxp_get_e()); printf("\n");
printf("pi : "); print_fxp(fxp_get_pi()); printf("\n");
printf("\nTesting logarithms for %d frac bits:", frac_bits);
// M is also base 2 but using the multiplication algorithm
char pbases[] = {'2', 'M', 'e', 'A'};
int npbases = sizeof(pbases) / sizeof(pbases[0]);
for (int i = 0; i < npbases; i++) {
char base = pbases[i];
if (base == 'M') {
if ((!TEST_L) || (!TEST_LG2_MUL_L)) continue;
if (whole_bits < 3) {
printf("\nOnly %d whole bit(s); 3 or more needed for lg2_mul_l; skipping.", \
whole_bits);
continue;
}
}
//printf("\nBase %c:\n", base);
printf("\n");
test_log(base, "+INF)", FXP_POS_INF);
test_log(base, "largest)", FXP_MAX);
test_log(base, "100)", fxp(100));
test_log(base, "e+1)", fxp_add(fxp_get_e(), fxp(1)));
test_log(base, "e)", fxp_get_e());
test_log(base, "2.2)", d2fxp(2.2));