-
Notifications
You must be signed in to change notification settings - Fork 0
/
floats.c
1201 lines (1139 loc) · 25.9 KB
/
floats.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
/*
BSD 3-Clause License
Copyright (c) 2023, Thomas DiModica
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "floats.h"
#include <string.h>
#define CUTOFF 8
#define MAX_EXP 50
typedef byte add_buffer [5]; // One more than in float.
typedef byte mul_buffer [8]; // Twice the size of a float.
static void add_bufcpy(add_buffer dest, const x_float src)
{
dest[0] = 0;
memmove(&dest[1], &src[2], 4U);
}
#define add_lshift(x) gen_shl((x), 1U, sizeof(add_buffer))
#define mul_shl(x, y) gen_shl((x), (y), sizeof(mul_buffer))
static void gen_shl(byte* arg, byte amount, byte bytes)
{
if (amount > 1U)
{
memmove(arg, arg + (amount >> 1), bytes - (amount >> 1));
memset(arg + bytes - (amount >> 1), '\0', (amount >> 1));
}
if (amount & 1U)
{
byte carryin = 0;
byte carryout = 0;
small count = bytes - 1;
for (; count >= 0; --count)
{
carryout = arg[count] >> 4;
arg[count] = arg[count] << 4 | carryin;
carryin = carryout;
}
}
}
#define add_rshift(x, y) gen_shr((x), (y), sizeof(add_buffer))
#define div_shr(x) gen_shr((x), 1U, sizeof(mul_buffer))
static void gen_shr(byte* arg, byte amount, byte bytes)
{
if (amount > 1U)
{
memmove(arg + (amount >> 1), arg, bytes - (amount >> 1));
memset(arg, '\0', (amount >> 1));
}
if (amount & 1U)
{
byte carryin = 0;
byte carryout = 0;
small count = 0;
for (; count < bytes; ++count)
{
carryout = arg[count];
arg[count] = (arg[count] >> 4) | (carryin << 4);
carryin = carryout;
}
}
}
#define add_doadd(x, y, z, c) gen_doadd((x), (y), (z), sizeof(add_buffer) - 1, (c))
#define mul_doadd(x, y) gen_doadd((x), (x), (y), sizeof(mul_buffer) - 1, 0)
static void gen_doadd(add_buffer dest, const add_buffer lhd, add_buffer rhd, small start, byte carry)
{
for (; start >= 0; --start)
{
byte lb = lhd[start];
byte rb = rhd[start];
byte lhn = lb >> 4;
byte lln = lb & 15;
byte rhn = rb >> 4;
byte rln = rb & 15;
lln = lln + rln + carry;
if (lln > 9)
{
carry = 1;
lln -= 10;
}
else
{
carry = 0;
}
lhn = lhn + rhn + carry;
if (lhn > 9)
{
carry = 1;
lhn -= 10;
}
else
{
carry = 0;
}
dest[start] = lln | (lhn << 4);
}
}
static void add_dosub(add_buffer dest, const add_buffer lhd, add_buffer rhd)
{
rhd[0] = 0x99 - rhd[0];
rhd[1] = 0x99 - rhd[1];
rhd[2] = 0x99 - rhd[2];
rhd[3] = 0x99 - rhd[3];
rhd[4] = 0x99 - rhd[4];
add_doadd(dest, lhd, rhd, 1);
}
static int add_iszero(const add_buffer lhd)
{
static const char * const test = "\0\0\0\0\0";
return memcmp(lhd, test, sizeof(add_buffer)) == 0;
}
static void set_to_special(x_float dest, byte special)
{
memset(dest, '\0', 6);
dest[0] = 0x80;
dest[1] = special;
}
static void set_digits_to_zero(x_float dest, small digits)
{
small start = 1 + 4 - (digits >> 1);
if (digits & 1)
{
dest[start] = dest[start] & 0xF0;
}
++start;
while (start < 6)
{
dest[start] = 0;
++start;
}
}
static byte placeDigits (char dest [15], const x_float src, byte digits, byte place, byte put_separator)
{
byte digit = 3;
dest[place] = (src[2] >> 4) + '0';
++place;
--digits;
if (digits)
{
if (put_separator)
{
dest[place] = '.';
++place;
}
dest[place] = (src[2] & 0xF) + '0';
++place;
--digits;
}
while (digits)
{
dest[place] = (src[digit] >> 4) + '0';
++place;
--digits;
if (digits)
{
dest[place] = (src[digit] & 0xF) + '0';
++place;
--digits;
}
++digit;
}
return place;
}
static void placeExponent (char dest [15], small exponent, byte place)
{
small digit = 0;
small check = 0;
dest[place] = 'e';
++place;
if (exponent < 0)
{
exponent = -exponent;
dest[place] = '-';
}
else
{
dest[place] = '+';
}
++place;
while (check <= exponent)
{
check += 10;
++digit;
}
check -= 10;
--digit;
if (digit)
{
dest[place] = digit + '0';
++place;
}
dest[place] = (exponent - check) + '0';
}
static void mul_mulByDigit(add_buffer lhd, byte digit)
{
add_buffer temp;
memcpy(temp, lhd, sizeof(add_buffer));
switch(digit)
{
case 2:
add_doadd(lhd, lhd, lhd, 0);
break;
case 3:
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, temp, 0);
break;
case 4:
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, lhd, 0);
break;
case 5:
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, temp, 0);
break;
case 6:
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, temp, 0);
add_doadd(lhd, lhd, lhd, 0);
break;
case 7:
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, temp, 0);
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, temp, 0);
break;
case 8:
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, lhd, 0);
break;
case 9:
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, lhd, 0);
add_doadd(lhd, lhd, temp, 0);
break;
}
}
static small div_cmp(const byte* lhs, const byte* rhs)
{
small temp = 0;
for (; temp < 3; ++temp)
{
small ld = lhs[temp] & 0xF;
small rd = rhs[temp] >> 4;
if (ld != rd)
{
return ld - rd;
}
ld = lhs[temp + 1] >> 4;
rd = rhs[temp] & 0xF;
if (ld != rd)
{
return ld - rd;
}
}
return 0;
}
void float_cpy (x_float dest, const x_float src)
{
if (src != dest) // They COULD be the same.
{
memcpy(dest, src, sizeof(x_float));
}
}
void float_neg (x_float arg)
{
if (arg[0] == 0x80) // There is no negative zero.
{
return;
}
arg[1] ^= -128;
}
void float_abs (x_float arg)
{
if ((arg[0] == 0x80) && (arg[1] == 0x80)) // Don't change an error condition.
{
return;
}
arg[1] = 0;
}
void float_min (x_float dest, const x_float lhs, const x_float rhs)
{
x_float_impl* LHS = (x_float_impl*)lhs;
x_float_impl* RHS = (x_float_impl*)rhs;
small temp;
if (lhs[0] == 0x80)
{
if (lhs[1] == 0x80) // Err
{
float_cpy(dest, lhs);
}
else if (rhs[1] == 0x80) // RHS is - or Err
{
float_cpy(dest, rhs);
}
else
{
float_cpy(dest, lhs);
}
return;
}
if (rhs[0] == 0x80)
{
if (rhs[1] == 0x80) // Err
{
float_cpy(dest, rhs);
}
else if (lhs[1] == 0x80) // LHS is - or Err
{
float_cpy(dest, lhs);
}
else
{
float_cpy(dest, rhs);
}
return;
}
if (LHS->sign != RHS->sign)
{
if (LHS->sign < 0)
{
float_cpy(dest, lhs);
}
else
{
float_cpy(dest, rhs);
}
}
else if (LHS->exponent != RHS->exponent)
{
temp = LHS->exponent > RHS->exponent;
if (LHS->sign)
{
temp = !temp;
}
if (temp)
{
float_cpy(dest, rhs);
}
else
{
float_cpy(dest, lhs);
}
}
else
{
temp = memcmp(&lhs[2], &rhs[2], sizeof(x_float) - 2U) > 0;
if (LHS->sign)
{
temp = !temp;
}
if (temp)
{
float_cpy(dest, rhs);
}
else
{
float_cpy(dest, lhs);
}
}
}
void float_max (x_float dest, const x_float lhs, const x_float rhs)
{
x_float_impl* LHS = (x_float_impl*)lhs;
x_float_impl* RHS = (x_float_impl*)rhs;
small temp;
if (lhs[0] == 0x80)
{
if (lhs[1] == 0x80) // Err
{
float_cpy(dest, lhs);
}
else if ((rhs[1] != 0x80) || (rhs[0] == 0x80)) // RHS is + or Err
{
float_cpy(dest, rhs);
}
else
{
float_cpy(dest, lhs);
}
return;
}
if (rhs[0] == 0x80)
{
if (rhs[1] == 0x80) // Err
{
float_cpy(dest, rhs);
}
else if (lhs[1] != 0x80) // LHS is + (Err case handled above)
{
float_cpy(dest, lhs);
}
else
{
float_cpy(dest, rhs);
}
return;
}
if (LHS->sign != RHS->sign)
{
if (LHS->sign < 0)
{
float_cpy(dest, rhs);
}
else
{
float_cpy(dest, lhs);
}
}
else if (LHS->exponent != RHS->exponent)
{
temp = LHS->exponent > RHS->exponent;
if (LHS->sign)
{
temp = !temp;
}
if (temp)
{
float_cpy(dest, lhs);
}
else
{
float_cpy(dest, rhs);
}
}
else
{
temp = memcmp(&lhs[2], &rhs[2], sizeof(x_float) - 2U) > 0;
if (LHS->sign)
{
temp = !temp;
}
if (temp)
{
float_cpy(dest, lhs);
}
else
{
float_cpy(dest, rhs);
}
}
}
void float_trunc (x_float arg)
{
x_float_impl* ARG = (x_float_impl*)arg;
small digits;
if (arg[0] == 0x80) // Don't change zero or error.
{
return;
}
if (ARG->exponent < 0) // Go to zero.
{
set_to_special(arg, 0);
return;
}
if (ARG->exponent > 6) // Nothing to change.
{
return;
}
digits = 7 - ARG->exponent; // Digits to remove.
set_digits_to_zero(arg, digits);
}
void float_round (x_float arg)
{
x_float_impl* ARG = (x_float_impl*)arg;
small digits, digit;
byte carryin, temp1, temp2;
if (arg[0] == 0x80) // Don't change zero or error.
{
return;
}
if (ARG->exponent < -1)
{
set_to_special(arg, 0);
return;
}
if (ARG->exponent > 6) // Nothing to change.
{
return;
}
if (-1 == ARG->exponent)
{
if ((arg[2] & 0xF0) > 0x40)
{
arg[0] = 0;
arg[2] = 0x10;
arg[3] = 0;
arg[4] = 0;
arg[5] = 0;
}
else
{
set_to_special(arg, 0);
}
return;
}
digits = 7 - ARG->exponent; // Digits to remove.
carryin = 0;
digit = 1 + 4 - (digits >> 1);
if (digits & 1)
{
carryin = (arg[digit] & 0xF) > 4;
}
else
{
carryin = (arg[digit + 1] & 0xF0) > 0x40;
}
set_digits_to_zero(arg, digits);
if (0 != carryin)
{
if (digits & 1)
{
arg[digit] = arg[digit] + 0x10;
if (arg[digit] > 0x90)
{
arg[digit] = 0;
}
else
{
digit = 2;
carryin = 0;
}
--digit;
}
while (digit > 1)
{
temp1 = arg[digit] & 0xF;
temp2 = (arg[digit] & 0xF0) >> 4;
temp1 = temp1 + 1;
if (temp1 > 9)
{
temp1 = 0;
temp2 = temp2 + 1;
if (temp2 > 9)
{
temp2 = 0;
}
else
{
carryin = 0;
}
arg[digit] = temp1 | (temp2 << 4);
if (0 == carryin)
{
digit = 2;
}
}
else
{
arg[digit] = temp1 | (temp2 << 4);
digit = 2;
carryin = 0;
}
--digit;
}
if (1 == carryin)
{
ARG->exponent = ARG->exponent + 1;
arg[2] = 0x10;
}
}
}
void float_add (x_float dest, const x_float lhs, const x_float rhs)
{
x_float_impl* DEST = (x_float_impl*)dest;
x_float_impl* LHS = (x_float_impl*)lhs;
x_float_impl* RHS = (x_float_impl*)rhs;
small resExp, resSign, expDiff;
add_buffer lhd, rhd; // Declare temp buffers.
if (LHS->exponent == -128) // Is the lhs zero?
{
if (LHS->sign == -128) // Is it Err?
{
float_cpy(dest, lhs); // Retain Err
}
else
{
float_cpy(dest, rhs); // Just return RHS
}
return;
}
else if (RHS->exponent == -128) // Is the rhs zero?
{
if (RHS->sign == -128) // Is it Err?
{
float_cpy(dest, rhs); // Retain Err
}
else
{
float_cpy(dest, lhs); // Just return LHS
}
return;
}
resExp = LHS->exponent;
expDiff = 0;
if (LHS->exponent > RHS->exponent)
{
expDiff = LHS->exponent - RHS->exponent;
if (expDiff <= CUTOFF)
{
add_bufcpy(lhd, lhs);
add_bufcpy(rhd, rhs);
if (expDiff > 1)
{
add_rshift(rhd, expDiff - 1);
expDiff = 1;
}
add_lshift(lhd);
}
else
{
float_cpy(dest, lhs); // Result won't change
return;
}
}
else if (LHS->exponent < RHS->exponent)
{
resExp = RHS->exponent;
expDiff = RHS->exponent - LHS->exponent;
if (expDiff <= CUTOFF)
{
add_bufcpy(lhd, lhs);
add_bufcpy(rhd, rhs);
if (expDiff > 1)
{
add_rshift(lhd, expDiff - 1);
expDiff = 1;
}
add_lshift(rhd);
}
else
{
float_cpy(dest, rhs); // Result won't change
return;
}
}
else
{
add_bufcpy(lhd, lhs);
add_bufcpy(rhd, rhs);
}
resSign = LHS->sign;
if (LHS->sign == RHS->sign)
{
add_doadd(lhd, lhd, rhd, 0);
}
else
{
if (memcmp(lhd, rhd, sizeof(add_buffer)) >= 0)
{
add_dosub(lhd, lhd, rhd);
}
else
{
add_dosub(lhd, rhd, lhd);
resSign ^= -128;
}
}
if (add_iszero(lhd))
{
resExp = -128;
resSign = 0;
}
else if (0 != lhd[0])
{
if (0 == expDiff)
{
++resExp;
}
add_rshift(lhd, 1U);
if (0 != lhd[0])
{
++resExp;
add_rshift(lhd, 1U);
}
if (resExp >= MAX_EXP)
{
resExp = -128;
resSign = -128;
memset(&lhd[1], '\0', 4U);
}
}
else
{
if (1 == expDiff)
{
--resExp;
}
while (0 == (lhd[1] & 0xF0))
{
add_lshift(lhd);
--resExp;
}
if (resExp <= -MAX_EXP)
{
resExp = -128;
resSign = 0;
memset(&lhd[1], '\0', 4U);
}
}
memmove(&dest[2], &lhd[1], 4U);
DEST->sign = resSign;
DEST->exponent = resExp;
}
void float_mul (x_float dest, const x_float lhs, const x_float rhs)
{
x_float_impl* LHS = (x_float_impl*)lhs;
x_float_impl* RHS = (x_float_impl*)rhs;
byte resultSign = lhs[1] ^ rhs[1]; // Sign is computed here.
byte digit, curr;
small resultExponent;
mul_buffer sum, addend;
if (lhs[0] == 0x80) // LHS zero?
{
if ((rhs[0] == 0x80) && (rhs[1] == 0x80)) // RHS is Err, keep that
{
float_cpy(dest, rhs);
}
else // LHS is either zero or Err: 0 * x is 0 and Err * x is Err
{
float_cpy(dest, lhs);
}
return;
}
if (rhs[0] == 0x80) // RHS is either zero or Err, either way return that instead.
{
float_cpy(dest, rhs);
return;
}
resultExponent = LHS->exponent + RHS->exponent; // Compute the exponent
// Can we quit early?
if (resultExponent >= MAX_EXP) // Overflow to Err
{
set_to_special(dest, -128);
return;
}
else if (resultExponent <= (-MAX_EXP - 1)) // Underflow to zero
{
set_to_special(dest, 0);
return;
}
// Perform a multiply
memset(sum, '\0', 8);
curr = 0;
for (digit = 5; digit > 1; --digit)
{
byte mb = rhs[digit];
byte mhn = mb >> 4;
byte mln = mb & 15;
if (0U != mln)
{
memset(addend, '\0', 4);
memcpy(&addend[4], &lhs[2], sizeof(x_float) - 2U);
mul_mulByDigit(addend + 3, mln);
mul_shl(addend, curr);
mul_doadd(sum, addend);
}
++curr;
if (0U != mhn)
{
memset(addend, '\0', 4);
memcpy(&addend[4], &lhs[2], sizeof(x_float) - 2U);
mul_mulByDigit(addend + 3, mhn);
mul_shl(addend, curr);
mul_doadd(sum, addend);
}
++curr;
}
if (0 == (sum[0] & 0xF0))
{
mul_shl(sum, 1U);
}
else
{
++resultExponent;
}
if (resultExponent >= MAX_EXP) // Overflow to Err
{
set_to_special(dest, -128);
}
else if (resultExponent <= -MAX_EXP) // Underflow to zero : unable to save a result
{
set_to_special(dest, 0);
}
else
{
// set result
dest[0] = (byte)resultExponent;
dest[1] = resultSign;
memcpy(&dest[2], sum, sizeof(x_float) - 2U);
}
}
void float_div (x_float dest, const x_float lhs, const x_float rhs)
{
x_float_impl* LHS = (x_float_impl*)lhs;
x_float_impl* RHS = (x_float_impl*)rhs;
byte resultSign = lhs[1] ^ rhs[1]; // Sign is computed here.
small resultExponent;
mul_buffer sum, addend;
byte count, times;
if (rhs[0] == 0x80) // RHS is either zero or Err, either way return Err.
{
float_cpy(dest, rhs);
dest[1] = 0x80;
return;
}
if (lhs[0] == 0x80) // LHS is either zero or Err: 0 / x is 0 and Err / x is Err
{
float_cpy(dest, lhs);
return;
}
resultExponent = LHS->exponent - RHS->exponent; // Compute the exponent
// If this is true, then the exponent will be one less.
times = memcmp(&lhs[2], &rhs[2], sizeof(x_float) - 2U) < 0;
resultExponent -= times;
// Can we quit early?
if (resultExponent >= MAX_EXP) // Overflow to Err
{
set_to_special(dest, -128);
return;
}
else if (resultExponent <= -MAX_EXP) // Underflow to zero
{
set_to_special(dest, 0);
return;
}
// Perform a division
/*
If this were a standard shift-subtract algorithm, I wouldn't feel the need to attribute it.
The algorithm here is from "Methods of Operating the Comptometer" from Felt and Tarrant Mfg. Co.
I was looking at the 1921 edition for the Model H, but any edition that includes division will work.
This is the algorithm utilized by a century-old adding machine.
The quotient is developed by repeated addition of the ten's complement of the divisor.
The dividend is transformed into the quotient: only one register (variable) is used.
*/
memset(sum, '\0', 8);
memset(addend, '\0', 8);
memcpy(&addend[4], &rhs[2], sizeof(x_float) - 2U);
for (count = 4; count < 8; ++count)
{
addend[count] = 0x99 - addend[count];
}
sum[7] = 1;
mul_doadd(addend, sum);
memcpy(&sum[4], &lhs[2], sizeof(x_float) - 2U);
mul_shl(addend, 7);
mul_shl(sum, 7 + times);
for (count = 0; count < 4; ++count)
{
if (sum[count] >> 4) // I don't get this step.
{
times = 0;
while (times < (sum[count] >> 4))
{
mul_doadd(sum, addend);
++times;
}
}
while (div_cmp(sum + count, rhs + 2) >= 0)
{
mul_doadd(sum, addend);
}
div_shr(addend);
if (sum[count] & 0xF) // I don't get this step.
{
times = 0;
while (times < (sum[count] & 0xF))
{
mul_doadd(sum, addend);
++times;
}
}
while (memcmp(sum + count + 1, &rhs[2], sizeof(x_float) - 2U) >= 0)
{
mul_doadd(sum, addend);
}
div_shr(addend);
}
// set result
dest[0] = (byte)resultExponent;
dest[1] = resultSign;
memcpy(&dest[2], sum, sizeof(x_float) - 2U);
}
const char* float_from_str (x_float dest, const char* src)
{
byte resultSign = 0;
byte digits = 0;
byte realDigit = 0;
byte exponentSign = 0;
small resultExponent = -1;
small exponentValue = 0;
set_to_special(dest, 0);
if (*src == '-')
{
resultSign = 0x80;
++src;
}
while ((*src >= '0') && (*src <= '9'))
{
if (digits < CUTOFF)
{
if (realDigit || ('0' != *src))
{
add_lshift(&dest[1]);
dest[5] |= *src - '0';
realDigit = 1;
++digits;
}
}
if (realDigit)
{
++resultExponent;
}
++src;
}
if (('.' == *src) || (',' == *src))
{
++src;
}
while ((*src >= '0') && (*src <= '9'))
{