-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_tdd.cpp
More file actions
1787 lines (1632 loc) · 76.9 KB
/
test_tdd.cpp
File metadata and controls
1787 lines (1632 loc) · 76.9 KB
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
/*
* test_tdd.cpp - Measurement-driven characterization suite for FR_Math
*
* Goal:
* Measure what the library actually does, print results as a markdown
* report, and never fail the build. No pass/fail assertions. The point
* is to learn empirically which functions are wrong, by how much, and
* which are merely imprecise.
*
* Coverage goal:
* - 100% line coverage of FR_math.c and FR_math_2D.cpp
* - Every macro in FR_math.h, FR_defs.h, FR_math_2D.h exercised at least once
*
* Compare against:
* - libm (math.h) for transcendental references
* - 64-bit integer arithmetic for fixed-point references
*/
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stdint.h>
#include "../src/FR_math.h"
#include "../src/FR_math_2D.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_E
#define M_E 2.71828182845904523536
#endif
#ifndef M_LOG2E
#define M_LOG2E 1.44269504088896340736
#endif
#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif
#ifdef __clang__
#pragma clang diagnostic ignored "-Wshift-count-negative"
#pragma clang diagnostic ignored "-Wshift-negative-value"
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wshift-count-negative"
#pragma GCC diagnostic ignored "-Wshift-negative-value"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
/* ============================================================
* Helpers
* ============================================================ */
static inline double frd(s32 x, int radix) {
return (double)x / (double)(1L << radix);
}
typedef struct {
int n;
double max_abs_err;
double sum_abs_err;
double worst_input;
double worst_actual;
double worst_expected;
} stats_t;
static void stats_reset(stats_t *s) {
memset(s, 0, sizeof(*s));
}
static void stats_add(stats_t *s, double in, double actual, double expected) {
double e = actual - expected;
if (e < 0) e = -e;
if (s->n == 0 || e > s->max_abs_err) {
s->max_abs_err = e;
s->worst_input = in;
s->worst_actual = actual;
s->worst_expected = expected;
}
s->sum_abs_err += e;
s->n++;
}
static double stats_mean(const stats_t *s) {
return s->n ? s->sum_abs_err / s->n : 0.0;
}
static void md_h1(const char *t) { printf("\n# %s\n\n", t); }
static void md_h2(const char *t) { printf("\n## %s\n\n", t); }
static void md_h3(const char *t) { printf("\n### %s\n\n", t); }
static void table_header_stats(void) {
printf("| Test | N | Max abs err | Mean abs err | Worst input | Actual | Expected |\n");
printf("|---|---:|---:|---:|---:|---:|---:|\n");
}
static void table_row_stats(const char *name, const stats_t *s) {
printf("| %s | %d | %.6g | %.6g | %.6g | %.6g | %.6g |\n",
name, s->n, s->max_abs_err, stats_mean(s),
s->worst_input, s->worst_actual, s->worst_expected);
}
/* captured putchar for testing print helpers */
static char g_buf[1024];
static int g_idx;
static int captured_putchar(char c) {
if (g_idx < (int)sizeof(g_buf) - 1) g_buf[g_idx++] = c;
g_buf[g_idx] = 0;
return c;
}
static void buf_reset(void) { g_idx = 0; g_buf[0] = 0; }
/* ============================================================
* Section 0: Platform sanity
* ============================================================ */
static void section_platform(void) {
md_h2("0. Platform & Type Sizes");
printf("| Type | Size (bytes) |\n");
printf("|---|---:|\n");
printf("| s8 | %lu |\n", (unsigned long)sizeof(s8));
printf("| u8 | %lu |\n", (unsigned long)sizeof(u8));
printf("| s16 | %lu |\n", (unsigned long)sizeof(s16));
printf("| u16 | %lu |\n", (unsigned long)sizeof(u16));
printf("| s32 | %lu |\n", (unsigned long)sizeof(s32));
printf("| u32 | %lu |\n", (unsigned long)sizeof(u32));
printf("| int | %lu |\n", (unsigned long)sizeof(int));
printf("| long | %lu |\n", (unsigned long)sizeof(long));
printf("\n");
if (sizeof(s32) != 4) {
printf("> **WARNING**: `s32` is %lu bytes, not 4. `FR_defs.h` maps `s32` to "
"`int32_t` via `<stdint.h>`, so this should never fire on a C99-or-newer toolchain.\n\n",
(unsigned long)sizeof(s32));
}
}
/* ============================================================
* Section 1: Header Constants
* ============================================================ */
static void test_constant(const char *name, s32 fr_val, double expected) {
double actual = frd(fr_val, FR_kPREC);
double err = actual - expected;
if (err < 0) err = -err;
double rel = expected != 0.0 ? err / fabs(expected) : err;
printf("| %s | %ld | %.10f | %.10f | %.3e | %.3e |\n",
name, (long)fr_val, actual, expected, err, rel);
}
static void section_constants(void) {
md_h2("1. Header Constants (radix FR_kPREC = 16)");
printf("| Name | Stored | As double | True value | Abs err | Rel err |\n");
printf("|---|---:|---:|---:|---:|---:|\n");
test_constant("FR_kE", FR_kE, M_E);
test_constant("FR_krE", FR_krE, 1.0 / M_E);
test_constant("FR_kPI", FR_kPI, M_PI);
test_constant("FR_krPI", FR_krPI, 1.0 / M_PI);
test_constant("FR_kDEG2RAD", FR_kDEG2RAD, M_PI / 180.0);
test_constant("FR_kRAD2DEG", FR_kRAD2DEG, 180.0 / M_PI);
test_constant("FR_kQ2RAD", FR_kQ2RAD, M_PI / 2.0);
test_constant("FR_kRAD2Q", FR_kRAD2Q, 2.0 / M_PI);
test_constant("FR_kLOG2E", FR_kLOG2E, M_LOG2E);
test_constant("FR_krLOG2E", FR_krLOG2E, M_LN2);
test_constant("FR_kLOG2_10", FR_kLOG2_10, log2(10.0));
test_constant("FR_krLOG2_10", FR_krLOG2_10, log10(2.0));
test_constant("FR_kSQRT2", FR_kSQRT2, sqrt(2.0));
test_constant("FR_krSQRT2", FR_krSQRT2, 1.0 / sqrt(2.0));
test_constant("FR_kSQRT3", FR_kSQRT3, sqrt(3.0));
test_constant("FR_krSQRT3", FR_krSQRT3, 1.0 / sqrt(3.0));
test_constant("FR_kSQRT5", FR_kSQRT5, sqrt(5.0));
test_constant("FR_krSQRT5", FR_krSQRT5, 1.0 / sqrt(5.0));
test_constant("FR_kSQRT10", FR_kSQRT10, sqrt(10.0));
test_constant("FR_krSQRT10", FR_krSQRT10, 1.0 / sqrt(10.0));
printf("\n");
}
/* ============================================================
* Section 2: Basic Macros
* ============================================================ */
static void section_macros_basic(void) {
md_h2("2. Basic Macros");
md_h3("2.1 FR_ABS");
printf("| Input | FR_ABS | Note |\n|---:|---:|---|\n");
printf("| 100 | %d | positive |\n", FR_ABS(100));
printf("| -100 | %d | negative |\n", FR_ABS(-100));
printf("| 0 | %d | zero |\n", FR_ABS(0));
{
s32 mn = (s32)0x80000000;
printf("| INT32_MIN (0x80000000) | %ld | UB on 2s-complement: -INT_MIN overflows |\n",
(long)FR_ABS(mn));
}
md_h3("2.2 FR_SGN");
printf("| Input | FR_SGN |\n|---:|---:|\n");
printf("| 100 | %d |\n", FR_SGN(100));
printf("| -100 | %d |\n", FR_SGN(-100));
printf("| 0 | %d |\n", FR_SGN(0));
{
s32 mx = (s32)0x7fffffff;
s32 mn = (s32)0x80000000;
printf("| 0x7fffffff | %ld |\n", (long)FR_SGN(mx));
printf("| 0x80000000 | %ld |\n", (long)FR_SGN(mn));
}
md_h3("2.3 I2FR / FR2I");
printf("| Op | Result |\n|---|---:|\n");
printf("| I2FR(100, 8) | %d |\n", I2FR(100, 8));
printf("| FR2I(I2FR(100,8), 8) | %d |\n", FR2I(I2FR(100, 8), 8));
printf("| I2FR(-50, 4) | %d |\n", I2FR(-50, 4));
printf("| FR2I(I2FR(-50,4), 4) | %d |\n", FR2I(I2FR(-50, 4), 4));
md_h3("2.4 FR_NUM");
printf("| Op | Result | Expected |\n|---|---:|---:|\n");
printf("| FR_NUM(12, 34, 2, 10) | %d | 12.34 << 10 ≈ 12636 |\n", FR_NUM(12, 34, 2, 10));
printf("| FR_NUM(-3, 5, 1, 16) | %d | -3.5 << 16 = -229376 |\n", FR_NUM(-3, 5, 1, 16));
printf("| FR_NUM(0, 25, 2, 16) | %d | 0.25 << 16 = 16384 |\n", FR_NUM(0, 25, 2, 16));
printf("| FR_NUM(1, 0, 0, 8) | %d | 1.0 << 8 = 256 |\n", FR_NUM(1, 0, 0, 8));
printf("\n> Signature: `FR_NUM(int, frac_digits, num_digits, radix)`.\n\n");
md_h3("2.4b FR_numstr (string parser)");
printf("| Input | Radix | FR_numstr | FR_NUM | Match |\n|---|---:|---:|---:|---:|\n");
printf("| \"12.34\" | 10 | %d | %d | %s |\n",
FR_numstr("12.34", 10), FR_NUM(12, 34, 2, 10),
FR_numstr("12.34", 10) == FR_NUM(12, 34, 2, 10) ? "yes" : "NO");
printf("| \"-3.5\" | 16 | %d | %d | %s |\n",
FR_numstr("-3.5", 16), FR_NUM(-3, 5, 1, 16),
FR_numstr("-3.5", 16) == FR_NUM(-3, 5, 1, 16) ? "yes" : "NO");
printf("| \"0.25\" | 16 | %d | %d | %s |\n",
FR_numstr("0.25", 16), FR_NUM(0, 25, 2, 16),
FR_numstr("0.25", 16) == FR_NUM(0, 25, 2, 16) ? "yes" : "NO");
printf("| \"-0.025\" | 16 | %d | %d | %s |\n",
FR_numstr("-0.025", 16), -FR_NUM(0, 25, 3, 16),
FR_numstr("-0.025", 16) == -FR_NUM(0, 25, 3, 16) ? "yes" : "NO");
printf("| \"0.05\" | 16 | %d | %d | %s |\n",
FR_numstr("0.05", 16), FR_NUM(0, 5, 2, 16),
FR_numstr("0.05", 16) == FR_NUM(0, 5, 2, 16) ? "yes" : "NO");
printf("| \"42\" | 8 | %d | %d | %s |\n",
FR_numstr("42", 8), 42 << 8,
FR_numstr("42", 8) == (42 << 8) ? "yes" : "NO");
printf("| \"1.0\" | 8 | %d | %d | %s |\n",
FR_numstr("1.0", 8), 1 << 8,
FR_numstr("1.0", 8) == (1 << 8) ? "yes" : "NO");
printf("| \"3.14159\" | 16 | %d | %d | %s |\n",
FR_numstr("3.14159", 16), FR_NUM(3, 14159, 5, 16),
FR_numstr("3.14159", 16) == FR_NUM(3, 14159, 5, 16) ? "yes" : "NO");
printf("| \" 3.14\" | 16 | %d | %d | %s |\n",
FR_numstr(" 3.14", 16), FR_NUM(3, 14, 2, 16),
FR_numstr(" 3.14", 16) == FR_NUM(3, 14, 2, 16) ? "yes" : "NO");
printf("| \"-7.0\" | 16 | %d | %d | %s |\n",
FR_numstr("-7.0", 16), -(7 << 16),
FR_numstr("-7.0", 16) == -(7 << 16) ? "yes" : "NO");
printf("| NULL | 16 | %d | 0 | %s |\n",
FR_numstr(NULL, 16),
FR_numstr(NULL, 16) == 0 ? "yes" : "NO");
printf("| \"\" | 16 | %d | 0 | %s |\n",
FR_numstr("", 16),
FR_numstr("", 16) == 0 ? "yes" : "NO");
md_h3("2.5 FR_INT");
printf("| Op | Result |\n|---|---:|\n");
printf("| FR_INT(I2FR(50,8), 8) | %d |\n", FR_INT(I2FR(50, 8), 8));
printf("| FR_INT(I2FR(-50,8), 8) | %d |\n", FR_INT(I2FR(-50, 8), 8));
printf("| FR_INT(I2FR(50,8) + 128, 8) | %d (truncates fractional) |\n",
FR_INT(I2FR(50, 8) + 128, 8));
printf("| FR_INT(I2FR(-50,8) - 128, 8) | %d (truncates toward zero) |\n",
FR_INT(I2FR(-50, 8) - 128, 8));
md_h3("2.6 FR_CHRDX");
printf("| Op | Result |\n|---|---:|\n");
printf("| FR_CHRDX(I2FR(10,4), 4, 8) | %d |\n", (int)FR_CHRDX(I2FR(10, 4), 4, 8));
printf("| FR_CHRDX(I2FR(10,8), 8, 4) | %d |\n", (int)FR_CHRDX(I2FR(10, 8), 8, 4));
printf("| FR_CHRDX(100, 8, 8) | %d |\n", (int)FR_CHRDX(100, 8, 8));
printf("| FR_CHRDX(100, 0, 8) | %d |\n", (int)FR_CHRDX(100, 0, 8));
md_h3("2.7 FR_FRAC");
printf("| Op | Result |\n|---|---:|\n");
printf("| FR_FRAC(I2FR(10,8) + 128, 8) | %d (= 0.5) |\n", FR_FRAC(I2FR(10, 8) + 128, 8));
printf("| FR_FRAC(I2FR(-10,8) - 128, 8) | %d (uses ABS, symmetric) |\n",
FR_FRAC(I2FR(-10, 8) - 128, 8));
printf("| FR_FRAC(I2FR(10,8), 8) | %d (no fraction) |\n", FR_FRAC(I2FR(10, 8), 8));
md_h3("2.8 FR_FRACS");
printf("| Op | Result | Expected |\n|---|---:|---:|\n");
printf("| FR_FRACS(I2FR(10,8) + 64, 8, 4) | %d | 64/256 = 0.25, in r4 = 4 |\n",
(int)FR_FRACS(I2FR(10, 8) + 64, 8, 4));
md_h3("2.9 FR_ADD / FR_SUB");
printf("| Op | Result | Note |\n|---|---:|---|\n");
{
s32 a;
a = I2FR(10, 8); FR_ADD(a, 8, I2FR(5, 8), 8);
printf("| FR_ADD same radix | %d | 15 in r8 = 3840 |\n", (int)a);
a = I2FR(10, 4); FR_ADD(a, 4, I2FR(5, 8), 8);
printf("| FR_ADD mixed radix | %d | 15 in r4 = 240 |\n", (int)a);
a = I2FR(10, 8); FR_SUB(a, 8, I2FR(3, 8), 8);
printf("| FR_SUB same radix | %d | 7 in r8 = 1792 |\n", (int)a);
}
md_h3("2.10 FR_ISPOW2");
printf("| Input | Result |\n|---:|---:|\n");
int pwrs[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 3, 5, 6, 7, 0};
for (int i = 0; i < (int)(sizeof(pwrs)/sizeof(pwrs[0])); i++) {
printf("| %d | %d |\n", pwrs[i], FR_ISPOW2(pwrs[i]) ? 1 : 0);
}
printf("\n> Note: `FR_ISPOW2(0)` returns true because `!(0 & -1) == !0 == 1`.\n\n");
md_h3("2.11 FR_FLOOR / FR_CEIL");
printf("| Op | Result | Note |\n|---|---:|---|\n");
printf("| FR_FLOOR(I2FR(10,8)+200, 8) >> 8 | %d | floor(10.78) |\n",
(int)(FR_FLOOR(I2FR(10, 8) + 200, 8) >> 8));
printf("| FR_CEIL(I2FR(10,8)+50, 8) >> 8 | %d | ceil(10.19) |\n",
(int)(FR_CEIL(I2FR(10, 8) + 50, 8) >> 8));
printf("| FR_FLOOR(I2FR(-10,8)-128, 8) | %d | bitwise AND on negative |\n",
(int)FR_FLOOR(I2FR(-10, 8) - 128, 8));
printf("| FR_CEIL(I2FR(10,8), 8) | %d | already integer |\n",
(int)FR_CEIL(I2FR(10, 8), 8));
md_h3("2.12 FR_INTERP / FR_INTERPI");
printf("| delta | INTERP(0,100,d,8) | INTERPI(0,100,d,8) |\n|---:|---:|---:|\n");
int deltas[] = {0, 64, 128, 192, 256};
for (int i = 0; i < 5; i++) {
int d = deltas[i];
printf("| %d | %d | %d |\n", d,
(int)FR_INTERP(0, 100, d, 8),
(int)FR_INTERPI(0, 100, d, 8));
}
md_h3("2.13 FR2D / D2FR");
printf("| Op | Result |\n|---|---:|\n");
printf("| FR2D(I2FR(10,8), 8) | %f |\n", FR2D(I2FR(10, 8), 8));
printf("| D2FR(3.14, 8) | %d |\n", (int)D2FR(3.14, 8));
printf("| FR2D(D2FR(3.14159, 16), 16) | %f |\n", FR2D(D2FR(3.14159, 16), 16));
md_h3("2.14 Trig & misc constants");
printf("| Constant | Value |\n|---|---:|\n");
printf("| FR_TRIG_PREC | %d |\n", FR_TRIG_PREC);
printf("| FR_TRIG_MASK | %d |\n", FR_TRIG_MASK);
printf("| FR_TRIG_MAXVAL | %d |\n", FR_TRIG_MAXVAL);
printf("| FR_TRIG_MINVAL | %d |\n", FR_TRIG_MINVAL);
printf("| FR_LOG2MIN | %ld |\n", (long)FR_LOG2MIN);
printf("| FR_kPREC | %d |\n", FR_kPREC);
printf("| FR_MAT_DEFPREC | %d |\n", FR_MAT_DEFPREC);
md_h3("2.15 FR_SWAP_BYTES");
printf("| Input | Result |\n|---:|---:|\n");
printf("| 0x1234 | 0x%04x |\n", FR_SWAP_BYTES(0x1234));
printf("| 0xff00 | 0x%04x |\n", FR_SWAP_BYTES(0xff00));
md_h3("2.16 FR_TRUE / FR_FALSE");
printf("| FR_TRUE = %d, FR_FALSE = %d |\n\n", FR_TRUE, FR_FALSE);
}
/* ============================================================
* Section 3: Shift-Approximation Macros
* ============================================================ */
static void section_shift_macros(void) {
md_h2("3. Shift-Approximation Macros");
printf("> These macros do constant multiplication using only shifts and adds.\n");
printf("> They produce *approximations* with characterizable error. We measure\n");
printf("> max relative error over a sweep of typical inputs.\n\n");
/* Multipliers we expect each macro to apply */
struct {
const char *name;
double expected_factor;
const char *purpose;
} cases[] = {
{"FR_SMUL10", 10.0, "x * 10"},
{"FR_SDIV10", 0.1, "x / 10"},
{"FR_SrLOG2E", M_LN2, "log2(x) -> ln(x) (* 0.6931)"},
{"FR_SLOG2E", M_LOG2E, "ln(x) -> log2(x) (* 1.4427)"},
{"FR_SrLOG2_10", log10(2.0), "log2(x) -> log10(x) (* 0.3010)"},
{"FR_SLOG2_10", log2(10.0), "log10(x) -> log2(x) (* 3.3219)"},
{"FR_DEG2RAD", M_PI / 180.0, "* 0.01745 (v2: body unswapped)"},
{"FR_RAD2DEG", 180.0 / M_PI, "* 57.2958 (v2: body unswapped)"},
{"FR_RAD2Q", 2.0 / M_PI, "* 0.6366"},
{"FR_Q2RAD", M_PI / 2.0, "* 1.5708"},
{"FR_DEG2Q", 1.0 / 90.0, "* 0.01111"},
{"FR_Q2DEG", 90.0, "* 90"},
};
printf("| Macro | Purpose | Empirical factor | Expected factor | Match? |\n");
printf("|---|---|---:|---:|:---:|\n");
/* Use a fixed input value and back out the apparent factor */
s32 X = 1 << 18;
double xd = (double)X;
double factors[12];
factors[0] = (double)FR_SMUL10(X) / xd;
factors[1] = (double)FR_SDIV10(X) / xd;
factors[2] = (double)FR_SrLOG2E(X) / xd;
factors[3] = (double)FR_SLOG2E(X) / xd;
factors[4] = (double)FR_SrLOG2_10(X) / xd;
factors[5] = (double)FR_SLOG2_10(X) / xd;
factors[6] = (double)FR_DEG2RAD(X) / xd;
factors[7] = (double)FR_RAD2DEG(X) / xd;
factors[8] = (double)FR_RAD2Q(X) / xd;
factors[9] = (double)FR_Q2RAD(X) / xd;
factors[10] = (double)FR_DEG2Q(X) / xd;
factors[11] = (double)FR_Q2DEG(X) / xd;
for (int i = 0; i < 12; i++) {
double diff = factors[i] - cases[i].expected_factor;
if (diff < 0) diff = -diff;
double rel = diff / fabs(cases[i].expected_factor);
const char *match = rel < 0.01 ? "OK"
: rel < 0.10 ? "approx"
: "**MISMATCH**";
printf("| %s | %s | %.6f | %.6f | %s |\n",
cases[i].name, cases[i].purpose,
factors[i], cases[i].expected_factor, match);
}
printf("\n");
md_h3("3.1 FR_DEG2RAD / FR_RAD2DEG cross-check");
printf("| Input | FR_DEG2RAD(input) | input * (pi/180) | FR_RAD2DEG(input) | input * (180/pi) |\n");
printf("|---:|---:|---:|---:|---:|\n");
int xs[] = {1, 10, 100, 180, 360, 1000};
for (int i = 0; i < 6; i++) {
int x = xs[i];
printf("| %d | %d | %.4f | %d | %.4f |\n",
x,
(int)FR_DEG2RAD(x), x * (M_PI / 180.0),
(int)FR_RAD2DEG(x), x * (180.0 / M_PI));
}
printf("\n");
}
/* ============================================================
* Section 4: Arithmetic Primitives
* ============================================================ */
static int64_t sat64(int64_t v) {
if (v > (int64_t)0x7fffffff) return (int64_t)0x7fffffff;
if (v < -(int64_t)0x7fffffff) return -(int64_t)0x7fffffff;
return v;
}
/* Reference matching FR_FixMuls's intended contract:
* Take absolute values, compute (|x|*|y|) >> 16, re-apply sign. */
static int64_t fixmuls_ref(s32 x, s32 y) {
int sign = ((x < 0) != (y < 0));
int64_t ax = (x < 0) ? -(int64_t)x : (int64_t)x;
int64_t ay = (y < 0) ? -(int64_t)y : (int64_t)y;
int64_t mag = (ax * ay) >> 16;
return sign ? -mag : mag;
}
/* Reference matching FR_FixMulSat's *intended* contract:
* (x*y) >> 16, then saturate to s32 range, with sign. */
static int64_t fixmulsat_ref(s32 x, s32 y) {
int64_t v = fixmuls_ref(x, y);
return sat64(v);
}
static void section_arithmetic(void) {
md_h2("4. Arithmetic Primitives");
/* test value pairs designed to hit corners */
static const s32 vals[] = {
0, 1, -1, 2, -2,
0x100, -0x100,
0x7fff, -0x7fff,
0x10000, -0x10000,
0x7fffffff, (s32)0x80000001, (s32)0x80000000,
0x12345678, (s32)0xfedcba98,
};
const int N = (int)(sizeof(vals)/sizeof(vals[0]));
md_h3("4.1 FR_FixMuls (signed, NOT saturating)");
int muls_total = 0, muls_diff = 0;
s32 muls_worst_x = 0, muls_worst_y = 0;
int64_t muls_worst_actual = 0, muls_worst_ref = 0;
for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) {
s32 x = vals[i], y = vals[j];
s32 actual = FR_FixMuls(x, y);
int64_t ref = fixmuls_ref(x, y);
muls_total++;
if ((int64_t)actual != ref) {
if (muls_diff == 0) {
muls_worst_x = x; muls_worst_y = y;
muls_worst_actual = actual; muls_worst_ref = ref;
}
muls_diff++;
}
}
printf("| Sweep | Pairs | Differs from |x|*|y|>>16 ref | First diff |\n");
printf("|---|---:|---:|---|\n");
printf("| FR_FixMuls | %d | %d | x=0x%lx y=0x%lx actual=0x%lx ref=0x%llx |\n",
muls_total, muls_diff,
(unsigned long)muls_worst_x, (unsigned long)muls_worst_y,
(unsigned long)muls_worst_actual, (long long)muls_worst_ref);
printf("\n");
md_h3("4.2 FR_FixMulSat (signed, saturating)");
int sat_total = 0, sat_diff = 0;
s32 sat_worst_x = 0, sat_worst_y = 0;
int64_t sat_worst_actual = 0, sat_worst_ref = 0;
for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) {
s32 x = vals[i], y = vals[j];
s32 actual = FR_FixMulSat(x, y);
int64_t ref = fixmulsat_ref(x, y);
sat_total++;
if ((int64_t)actual != ref) {
if (sat_diff == 0) {
sat_worst_x = x; sat_worst_y = y;
sat_worst_actual = actual; sat_worst_ref = ref;
}
sat_diff++;
}
}
printf("| Sweep | Pairs | Differs from sat(|x|*|y|>>16) | First diff |\n");
printf("|---|---:|---:|---|\n");
printf("| FR_FixMulSat | %d | %d | x=0x%lx y=0x%lx actual=0x%lx ref=0x%llx |\n",
sat_total, sat_diff,
(unsigned long)sat_worst_x, (unsigned long)sat_worst_y,
(unsigned long)sat_worst_actual, (long long)sat_worst_ref);
printf("\n");
md_h3("4.3 FR_FixMulSat targeted overflow cases");
printf("| x | y | actual | sat64(x*y) |\n|---:|---:|---:|---:|\n");
struct { s32 x, y; } targeted[] = {
{0x7fffffff, 0x7fffffff},
{0x7fffffff, 2},
{0x40000000, 4},
{0x10000, 0x10000},
{0x10000, -0x10000},
{(s32)0x80000000, 1},
{(s32)0x80000000, -1},
{0x7fffffff, -1},
};
for (int i = 0; i < (int)(sizeof(targeted)/sizeof(targeted[0])); i++) {
s32 a = FR_FixMulSat(targeted[i].x, targeted[i].y);
int64_t r = sat64((int64_t)targeted[i].x * (int64_t)targeted[i].y);
printf("| 0x%lx | 0x%lx | 0x%lx | 0x%llx |\n",
(unsigned long)targeted[i].x, (unsigned long)targeted[i].y,
(unsigned long)a, (long long)r);
}
printf("\n");
md_h3("4.4 FR_FixAddSat (signed, saturating)");
int add_total = 0, add_diff = 0;
s32 add_worst_x = 0, add_worst_y = 0;
int64_t add_worst_actual = 0, add_worst_ref = 0;
for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) {
s32 x = vals[i], y = vals[j];
s32 actual = FR_FixAddSat(x, y);
int64_t ref = sat64((int64_t)x + (int64_t)y);
add_total++;
if ((int64_t)actual != ref) {
if (add_diff == 0) {
add_worst_x = x; add_worst_y = y;
add_worst_actual = actual; add_worst_ref = ref;
}
add_diff++;
}
}
printf("| Sweep | Pairs | Differs from sat(x+y) | First diff |\n");
printf("|---|---:|---:|---|\n");
printf("| FR_FixAddSat vs sat64(x+y) | %d | %d | x=0x%lx y=0x%lx actual=0x%lx ref=0x%llx |\n",
add_total, add_diff,
(unsigned long)add_worst_x, (unsigned long)add_worst_y,
(unsigned long)add_worst_actual, (long long)add_worst_ref);
printf("\n");
md_h3("4.5 FR_FixAddSat targeted overflow cases");
printf("| x | y | actual | sat64(x+y) |\n|---:|---:|---:|---:|\n");
struct { s32 x, y; } addt[] = {
{0x7ffffff0, 0x100},
{0x7fffffff, 1},
{(s32)0x80000010, -0x100},
{(s32)0x80000000, -1},
{1000, -500},
{-1000, 500},
{0x40000000, 0x40000000},
{(s32)0xc0000000, (s32)0xc0000000},
};
for (int i = 0; i < (int)(sizeof(addt)/sizeof(addt[0])); i++) {
s32 a = FR_FixAddSat(addt[i].x, addt[i].y);
int64_t r = sat64((int64_t)addt[i].x + (int64_t)addt[i].y);
printf("| 0x%lx | 0x%lx | 0x%lx | 0x%llx |\n",
(unsigned long)addt[i].x, (unsigned long)addt[i].y,
(unsigned long)a, (long long)r);
}
printf("\n");
md_h3("4.6 FR_DIV (64-bit) vs FR_DIV32 (32-bit) vs double");
printf("| x | y | radix | FR_DIV | FR_DIV32 | expected (double) | DIV err | DIV32 err | DIV32 overflow? |\n");
printf("|---:|---:|---:|---:|---:|---:|---:|---:|---:|\n");
struct { double xd, yd; int r; } div_cases[] = {
{10, 2, 8}, {7, 2, 8}, {-10, 3, 8}, {100, 7, 16},
{1, 3, 16}, {30000, 3, 16}, {0.5, 0.25, 16},
{-1000, -7, 12}, {1, 1, 8}, {32000, 1, 16},
};
for (int i = 0; i < (int)(sizeof(div_cases)/sizeof(div_cases[0])); i++) {
int r = div_cases[i].r;
s32 xfp = (s32)(div_cases[i].xd * (1L << r));
s32 yfp = (s32)(div_cases[i].yd * (1L << r));
double expected = div_cases[i].xd / div_cases[i].yd;
s32 d64 = FR_DIV(xfp, r, yfp, r);
s32 d32 = FR_DIV32(xfp, r, yfp, r);
double d64d = frd(d64, r);
double d32d = frd(d32, r);
double e64 = d64d - expected; if (e64 < 0) e64 = -e64;
double e32 = d32d - expected; if (e32 < 0) e32 = -e32;
int overflow32 = (d64 != d32) ? 1 : 0;
printf("| %g | %g | %d | %ld | %ld | %.6g | %.4g | %.4g | %s |\n",
div_cases[i].xd, div_cases[i].yd, r,
(long)d64, (long)d32, expected, e64, e32,
overflow32 ? "YES" : "no");
}
printf("\n");
}
/* ============================================================
* Section 5: Trig (Integer Degrees)
* ============================================================ */
static void section_trig_int(void) {
md_h2("5. Trig Functions (Integer Degrees)");
stats_t cos_stats, sin_stats;
stats_reset(&cos_stats);
stats_reset(&sin_stats);
for (int deg = -720; deg <= 720; deg++) {
double exp_cos = cos(deg * M_PI / 180.0);
double exp_sin = sin(deg * M_PI / 180.0);
double act_cos = frd(FR_CosI((s16)deg), FR_TRIG_OUT_PREC);
double act_sin = frd(FR_SinI((s16)deg), FR_TRIG_OUT_PREC);
stats_add(&cos_stats, deg, act_cos, exp_cos);
stats_add(&sin_stats, deg, act_sin, exp_sin);
}
table_header_stats();
table_row_stats("FR_CosI [-720..720]", &cos_stats);
table_row_stats("FR_SinI [-720..720]", &sin_stats);
printf("\n> Tolerance reference: 1 LSB in s15.16 = 1/65536 ≈ 1.53e-5. Poles (0,90,180,270) are exact.\n\n");
md_h3("5.1 FR_TanI vs tan() (skipping ±90n)");
stats_t tan_stats;
stats_reset(&tan_stats);
int tan_skipped = 0;
for (int deg = -89; deg <= 89; deg++) {
if (deg % 90 == 0 && deg != 0) { tan_skipped++; continue; }
double exp_tan = tan(deg * M_PI / 180.0);
double act_tan = frd(FR_TanI((s16)deg), FR_TRIG_OUT_PREC);
stats_add(&tan_stats, deg, act_tan, exp_tan);
}
table_header_stats();
table_row_stats("FR_TanI [-89..89]", &tan_stats);
printf("\n");
md_h3("5.2 FR_TanI special angles");
printf("| deg | FR_TanI | as double (s15.16) | tan(deg) |\n|---:|---:|---:|---:|\n");
int specials[] = {0, 30, 45, 60, 80, 85, 88, 89, 90, 91, 135, 180, 270, -45, -90};
for (int i = 0; i < (int)(sizeof(specials)/sizeof(specials[0])); i++) {
int d = specials[i];
s32 t = FR_TanI((s16)d);
double td = frd(t, FR_TRIG_OUT_PREC);
double ref = (d % 180 == 90) ? INFINITY : tan(d * M_PI / 180.0);
printf("| %d | %ld | %.6g | %.6g |\n", d, (long)t, td, ref);
}
printf("\n");
}
/* ============================================================
* Section 6: Trig (Fractional Degrees)
* ============================================================ */
static void section_trig_frac(void) {
md_h2("6. Trig Functions (Fractional Degrees)");
md_h3("6.1 FR_Cos / FR_Sin (interpolated, radix 8)");
stats_t cos_f, sin_f;
stats_reset(&cos_f);
stats_reset(&sin_f);
/* sweep -180 to +180 in radix-8 increments of 0.25 deg */
for (int q = -180 * 4; q <= 180 * 4; q++) {
double deg_d = q / 4.0;
s16 deg_fr = (s16)(q << (8 - 2)); /* radix 8, 0.25 step = 64 LSBs */
double exp_c = cos(deg_d * M_PI / 180.0);
double exp_s = sin(deg_d * M_PI / 180.0);
double act_c = frd(FR_Cos(deg_fr, 8), FR_TRIG_OUT_PREC);
double act_s = frd(FR_Sin(deg_fr, 8), FR_TRIG_OUT_PREC);
stats_add(&cos_f, deg_d, act_c, exp_c);
stats_add(&sin_f, deg_d, act_s, exp_s);
}
table_header_stats();
table_row_stats("FR_Cos r8 0.25 step", &cos_f);
table_row_stats("FR_Sin r8 0.25 step", &sin_f);
printf("\n");
md_h3("6.2 FR_Tan (interpolated, radix 8) — focused on steep region");
printf("| deg | FR_Tan | as double | tan(deg) | abs err |\n|---:|---:|---:|---:|---:|\n");
double check_degs[] = {30.0, 45.0, 60.0, 75.0, 80.0, 85.0, 88.0, 89.0, 89.5};
for (int i = 0; i < (int)(sizeof(check_degs)/sizeof(check_degs[0])); i++) {
s16 deg_fr = (s16)(check_degs[i] * 256);
s32 t = FR_Tan(deg_fr, 8);
double td = frd(t, FR_TRIG_OUT_PREC);
double ref = tan(check_degs[i] * M_PI / 180.0);
double e = td - ref; if (e < 0) e = -e;
printf("| %.2f | %ld | %.6g | %.6g | %.6g |\n",
check_degs[i], (long)t, td, ref, e);
}
printf("\n> v2: FR_Tan locals were widened from s16 to s32, so steep angles no longer\n");
printf("> truncate catastrophically. The values above now agree with `libm`.\n\n");
}
/* ============================================================
* Section 7: Inverse Trig
* ============================================================ */
static void section_inverse_trig(void) {
md_h2("7. Inverse Trig");
md_h3("7.1 FR_acos sweep [-1, +1]");
stats_t acos_stats;
stats_reset(&acos_stats);
/* radix 15 inputs, output radians at radix 16, 200 samples */
for (int i = -200; i <= 200; i++) {
double xd = i / 200.0;
s32 fr = (s32)(xd * (1 << 15));
s32 rad = FR_acos(fr, 15, 16);
double ref_rad = acos(xd);
stats_add(&acos_stats, xd, frd(rad, 16), ref_rad);
}
table_header_stats();
table_row_stats("FR_acos vs acos() (rad)", &acos_stats);
printf("\n");
md_h3("7.2 FR_asin sweep [-1, +1]");
stats_t asin_stats;
stats_reset(&asin_stats);
for (int i = -200; i <= 200; i++) {
double xd = i / 200.0;
s32 fr = (s32)(xd * (1 << 15));
s32 rad = FR_asin(fr, 15, 16);
double ref_rad = asin(xd);
stats_add(&asin_stats, xd, frd(rad, 16), ref_rad);
}
table_header_stats();
table_row_stats("FR_asin vs asin() (rad)", &asin_stats);
printf("\n");
md_h3("7.3 FR_atan2 (returns radians at radix 16)");
printf("| (y, x) | FR_atan2 (rad s15.16) | atan2() radians |\n|---|---:|---:|\n");
struct { s32 y, x; } pts[] = {
{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1},
{3, 4}, {-3, 4}, {3, -4}, {-3, -4}
};
for (int i = 0; i < (int)(sizeof(pts)/sizeof(pts[0])); i++) {
s32 r = FR_atan2(pts[i].y, pts[i].x, 16);
double ref = atan2((double)pts[i].y, (double)pts[i].x);
printf("| (%ld, %ld) | %.4f | %.4f |\n",
(long)pts[i].y, (long)pts[i].x, frd(r, 16), ref);
}
printf("\n> `FR_atan2` returns radians at the specified output radix.\n");
printf("> `FR_atan(x, radix, out_radix)` is implemented as `FR_atan2(x, 1<<radix, out_radix)`.\n\n");
}
/* ============================================================
* Section 8: Power & Log
* ============================================================ */
static void section_pow_log(void) {
md_h2("8. Power & Log Functions");
md_h3("8.1 FR_pow2 vs pow(2, x), radix 16");
printf("| x | FR_pow2(x*2^16, 16) | as double | pow(2,x) | abs err | rel err |\n");
printf("|---:|---:|---:|---:|---:|---:|\n");
double pow2_inputs[] = {-8, -4, -2, -1, -0.5, 0, 0.25, 0.5, 1, 1.5, 2, 3, 4, 8, 12};
stats_t pow2_stats; stats_reset(&pow2_stats);
for (int i = 0; i < (int)(sizeof(pow2_inputs)/sizeof(pow2_inputs[0])); i++) {
double x = pow2_inputs[i];
s32 fr = (s32)(x * (1L << 16));
s32 r = FR_pow2(fr, 16);
double rd = frd(r, 16);
double ref = pow(2.0, x);
double err = rd - ref; if (err < 0) err = -err;
double rel = ref != 0.0 ? err / fabs(ref) : err;
stats_add(&pow2_stats, x, rd, ref);
printf("| %.4g | %ld | %.6g | %.6g | %.4g | %.4g |\n",
x, (long)r, rd, ref, err, rel);
}
printf("\n");
table_header_stats();
table_row_stats("FR_pow2 sweep", &pow2_stats);
printf("\n");
md_h3("8.2 FR_pow2 fine sweep, radix 16");
stats_t pow2_fine; stats_reset(&pow2_fine);
for (int i = -800; i <= 800; i++) {
double x = i / 100.0;
s32 fr = (s32)(x * (1L << 16));
s32 r = FR_pow2(fr, 16);
double rd = frd(r, 16);
double ref = pow(2.0, x);
stats_add(&pow2_fine, x, rd, ref);
}
table_header_stats();
table_row_stats("FR_pow2 [-8,8] step 0.01", &pow2_fine);
printf("\n");
md_h3("8.3 FR_log2 — empirical behavior on integer powers of 2");
printf("> Implementation: leading-bit-position → normalize the remainder to s1.30 →\n");
printf("> 65-entry mantissa lookup with linear interpolation.\n\n");
printf("| input | radix | out_radix | FR_log2 | as double | log2(x) |\n");
printf("|---:|---:|---:|---:|---:|---:|\n");
struct { s32 in; u16 r; u16 or_; double ref; } log2_cases[] = {
{1, 0, 16, 0.0},
{2, 0, 16, 1.0},
{4, 0, 16, 2.0},
{8, 0, 16, 3.0},
{16, 0, 16, 4.0},
{32, 0, 16, 5.0},
{64, 0, 16, 6.0},
{128, 0, 16, 7.0},
{1024, 0, 16, 10.0},
{65536, 0, 16, 16.0},
{3, 0, 16, log2(3.0)},
{5, 0, 16, log2(5.0)},
{7, 0, 16, log2(7.0)},
{1<<16, 16, 16, 0.0}, /* 1.0 in s.16 */
{2<<16, 16, 16, 1.0}, /* 2.0 in s.16 */
};
stats_t log2_stats; stats_reset(&log2_stats);
for (int i = 0; i < (int)(sizeof(log2_cases)/sizeof(log2_cases[0])); i++) {
s32 r = FR_log2(log2_cases[i].in, log2_cases[i].r, log2_cases[i].or_);
double rd = frd(r, log2_cases[i].or_);
printf("| %ld | %u | %u | %ld | %.6g | %.6g |\n",
(long)log2_cases[i].in, log2_cases[i].r, log2_cases[i].or_,
(long)r, rd, log2_cases[i].ref);
stats_add(&log2_stats, (double)log2_cases[i].in, rd, log2_cases[i].ref);
}
printf("\n");
table_header_stats();
table_row_stats("FR_log2 vs log2()", &log2_stats);
printf("\n> v2: `FR_log2` rewritten — leading-bit-position → normalize to s1.30 → 65-entry\n");
printf("> mantissa LUT with linear interpolation. Error is now table-limited.\n\n");
md_h3("8.4 FR_ln — derived from FR_log2 (v2: fixed by inheritance)");
printf("| input | FR_ln(in,16,16) | as double | ln(input) |\n|---:|---:|---:|---:|\n");
double ln_inputs[] = {1, 2, M_E, 4, 8, 10, 100, 1000};
stats_t ln_stats; stats_reset(&ln_stats);
for (int i = 0; i < (int)(sizeof(ln_inputs)/sizeof(ln_inputs[0])); i++) {
s32 fr = (s32)(ln_inputs[i] * (1L << 16));
s32 r = FR_ln(fr, 16, 16);
double rd = frd(r, 16);
double ref = log(ln_inputs[i]);
stats_add(&ln_stats, ln_inputs[i], rd, ref);
printf("| %.4g | %ld | %.6g | %.6g |\n", ln_inputs[i], (long)r, rd, ref);
}
printf("\n");
table_header_stats();
table_row_stats("FR_ln vs ln()", &ln_stats);
printf("\n");
md_h3("8.5 FR_log10 — derived from FR_log2 (v2: fixed by inheritance)");
printf("| input | FR_log10(in,16,16) | as double | log10(input) |\n|---:|---:|---:|---:|\n");
double log10_inputs[] = {1, 2, 5, 10, 100, 1000, 10000};
stats_t log10_stats; stats_reset(&log10_stats);
for (int i = 0; i < (int)(sizeof(log10_inputs)/sizeof(log10_inputs[0])); i++) {
s32 fr = (s32)(log10_inputs[i] * (1L << 16));
s32 r = FR_log10(fr, 16, 16);
double rd = frd(r, 16);
double ref = log10(log10_inputs[i]);
stats_add(&log10_stats, log10_inputs[i], rd, ref);
printf("| %.4g | %ld | %.6g | %.6g |\n", log10_inputs[i], (long)r, rd, ref);
}
printf("\n");
table_header_stats();
table_row_stats("FR_log10 vs log10()", &log10_stats);
printf("\n");
md_h3("8.6 FR_EXP and FR_POW10 macros (wrap FR_pow2)");
printf("| Expression | Result | as double | Reference | Note |\n|---|---:|---:|---:|---|\n");
{
s32 in = (s32)(1.0 * (1L << 16));
s32 r = FR_EXP(in, 16);
double rd = frd(r, 16);
printf("| FR_EXP(1.0,16) | %ld | %.6g | %.6g | exp(1) = e |\n",
(long)r, rd, M_E);
}
{
s32 in = (s32)(2.0 * (1L << 16));
s32 r = FR_POW10(in, 16);
double rd = frd(r, 16);
printf("| FR_POW10(2.0,16) | %ld | %.6g | %.6g | 10^2 = 100 |\n",
(long)r, rd, 100.0);
}
printf("\n");
md_h3("8.7 FR_LOG2MIN sentinel");
printf("| Call | Result | FR_LOG2MIN |\n|---|---:|---:|\n");
printf("| FR_log2(0,16,16) | %ld | %ld |\n",
(long)FR_log2(0, 16, 16), (long)FR_LOG2MIN);
printf("| FR_log2(-1,16,16) | %ld | %ld |\n",
(long)FR_log2(-1, 16, 16), (long)FR_LOG2MIN);
printf("\n");
md_h3("8.8 Coverage of seldom-reached branches");
/* FR_TanI with deg in (-360, -180) hits the `deg += 360` branch */
printf("| Call | Result |\n|---|---:|\n");
printf("| FR_TanI(-200) | %ld |\n", (long)FR_TanI((s16)-200));
printf("| FR_TanI(200) | %ld |\n", (long)FR_TanI((s16)200));
/* Print helpers with NULL stream should return -1 */
printf("| FR_printNumD(NULL,1,0) | %d |\n", FR_printNumD(NULL, 1, 0));
printf("| FR_printNumH(NULL,1,0) | %d |\n", FR_printNumH(NULL, 1, 0));
printf("| FR_printNumF(NULL,1,16,0,4) | %d |\n", FR_printNumF(NULL, 1, 16, 0, 4));
printf("\n");
}
/* ============================================================
* Section 9: Print Helpers
* ============================================================ */
static void section_print(void) {
md_h2("9. Print Helpers");
md_h3("9.1 FR_printNumD (decimal)");
printf("| n | pad | output | bytes written |\n|---:|---:|---|---:|\n");
struct { int n; int pad; const char *desc; } d_cases[] = {
{0, 0, "zero"},
{1, 0, "one"},
{-1, 0, "neg one"},
{12345, 0, "positive"},
{-12345, 0, "negative"},
{12, 5, "padded right"},
{-12, 5, "padded neg"},
{INT_MAX, 0, "INT_MAX"},
{INT_MIN, 0, "INT_MIN"},
};
for (int i = 0; i < (int)(sizeof(d_cases)/sizeof(d_cases[0])); i++) {
buf_reset();
int wrote = FR_printNumD(captured_putchar, d_cases[i].n, d_cases[i].pad);
printf("| %d | %d | `%s` | %d |\n", d_cases[i].n, d_cases[i].pad, g_buf, wrote);
}
printf("\n");
md_h3("9.2 FR_printNumH (hex)");
printf("| n | showPrefix | output | bytes written |\n|---:|---:|---|---:|\n");
struct { int n; int prefix; } h_cases[] = {
{0, 0}, {0, 1},
{0xab, 0}, {0xab, 1},
{0x12345678, 1},
{-1, 1},
{INT_MIN, 1},
};
for (int i = 0; i < (int)(sizeof(h_cases)/sizeof(h_cases[0])); i++) {
buf_reset();
int wrote = FR_printNumH(captured_putchar, h_cases[i].n, h_cases[i].prefix);
printf("| %d | %d | `%s` | %d |\n", h_cases[i].n, h_cases[i].prefix, g_buf, wrote);
}
printf("\n> v2: `FR_printNumH` casts to unsigned before shifting, so the output is\n");
printf("> portable across compilers.\n\n");
md_h3("9.3 FR_printNumF (fixed-radix as float)");
printf("| value | radix | pad | prec | output | bytes |\n|---:|---:|---:|---:|---|---:|\n");
struct { s32 n; int rad; int pad; int prec; const char *desc; } f_cases[] = {
{(s32)(0.0 * 65536), 16, 0, 4, "0.0"},
{(s32)(1.0 * 65536), 16, 0, 4, "1.0"},
{(s32)(-1.0 * 65536), 16, 0, 4, "-1.0"},
{(s32)(3.14159* 65536), 16, 0, 4, "pi"},
{(s32)(-3.14159*65536), 16, 0, 4, "-pi"},
{(s32)(0.0001 * 65536), 16, 0, 4, "0.0001"},
{(s32)(1.05 * 65536), 16, 0, 4, "1.05"},
{(s32)(-1.05 * 65536), 16, 0, 4, "-1.05"},
{(s32)(12.34 * 65536), 16, 8, 2, "12.34 padded"},
{(s32)0x80000000, 16, 0, 4, "INT32_MIN"},
{(s32)0x7fffffff, 16, 0, 4, "INT32_MAX"},
};
for (int i = 0; i < (int)(sizeof(f_cases)/sizeof(f_cases[0])); i++) {
buf_reset();
int wrote = FR_printNumF(captured_putchar, f_cases[i].n,
f_cases[i].rad, f_cases[i].pad, f_cases[i].prec);
printf("| %s | %d | %d | %d | `%s` | %d |\n",
f_cases[i].desc, f_cases[i].rad, f_cases[i].pad, f_cases[i].prec,
g_buf, wrote);
}
printf("\n> v2: `FR_printNumF` and `FR_printNumD` now work in unsigned magnitude, so\n");
printf("> `INT32_MIN` / `INT_MIN` no longer trigger signed-negation UB. Fraction\n");
printf("> extraction in `FR_printNumF` was also rewritten for correctness.\n\n");
}
/* ============================================================
* Section 10: 2D Matrix
* ============================================================ */
static void section_matrix2d(void) {
md_h2("10. 2D Matrix (FR_Matrix2D_CPT)");