-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.yabal
1408 lines (1250 loc) · 39.5 KB
/
float.yabal
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
var floats = create_pointer<Float16>(0x0, 1);
var signedInts = create_pointer<SignedInt>(0x1111, 1);
var screen = create_pointer(53871, 1)
var charMem = create_pointer(53546, 1)
int offset = 0
var highlightColor = 0
var highlightOppositeColor = 0b1111111111111111
Float16 PI = { sign : 0, exponent : 0b10000, fraction : 0b1001001000 };
Float16 ONE = { sign : 0, exponent : 0b01111, fraction : 0 };
inline int get_color(int r, int g, int b) {
return (r / 8 << 10) + (g / 8 << 5) + (b / 8);
}
int ClampInt(int x, int min, int max){
if(x<min)
return min
else if(x>max)
return max
return x
}
// Function for drawing a single pixel to the screen
void SetPixel(int x, int y, int color){
var screenOffset = (ClampInt(y, 0, 108) * 108) + ClampInt(x, 0, 108)
screen[screenOffset] = color
}
void ChangeHighlightColor(int r, int g, int b){
highlightColor = get_color(r, g, b)
highlightOppositeColor = get_color(256-r, 256-g, 256-b)
}
void write(int c) {
charMem[offset] = c
// Draw highlight behind character
var pixOffsetY = (offset/18)*6
var pixOffsetX = ((offset*6)%108)
for (var x = 0; x < 6; x++) {
for (var y = 0; y < 6; y++) {
var pixel = highlightColor
SetPixel(pixOffsetX+x, pixOffsetY+y, pixel)
}
}
// Increment location offset by 1
offset++
}
// Get the absolute value of a float value
Float16 fabs(Float16 f){
Float16 outFloat = {sign:0, exponent:0, fraction:0};
outFloat.exponent = f.exponent
outFloat.fraction = f.fraction
return outFloat
}
SignedInt IntToSignedInt(int x){
SignedInt o = {s:0, val:x};
return o
}
// void writeStr(int s) {
// var size = sizeof(s)
// for (var i = 0; i < size; i++) {
// write(s[i])
// }
// }
void newline(){
offset = offset / 18 * 18 + 18
}
void write_int(int value) {
var reverser = create_pointer(65530, 0)
var i = 0
for (var o = 0; o < 5; o++){
reverser[o] = 0
}
while (value > 0) {
var char = (value % 10) switch {
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
9 => '9',
_ => '0'
}
reverser[i] = char
value = value / 10
i += 1
}
bool atFirst = false
for (var o = 0; o < 5; o++){
// If the first non-zero number has finally been found, start writing number
if(reverser[4-o] != 0)
atFirst = true
// Otherwise it is just a trailing zero, so skip
else if(atFirst==false)
continue
write(reverser[4-o])
}
}
void write_binary(int value) {
var reverser = create_pointer(65519, 0)
var i = 0
for (var i = 0; i < 16; i++){
reverser[i] = 0
}
while (value > 0) {
var char = (value & 1) switch {
1 => '1',
_ => '0'
}
reverser[15-i] = char
value = value >> 1
i += 1
}
for (var i = 0; i < 16; i++){
write(reverser[i])
}
}
void write_float(Float16 f) {
write('e')
write(':')
var exp = f.exponent
write_binary(f.exponent)
write('f')
write(':')
var frac = f.fraction
write_binary(f.fraction)
}
void write_signed_int(SignedInt i) {
if(i.s == true) // If negative, print `-`
write('-')
int v = i.val
write_int(v)
}
int TruncateTrailingZeros(int x){
var o = x
for (var i = 0; i < 16; i++){
// If the last digit is 1, then stop shifting
if((o & 1) == 1)
break
// Otherwise shift again
o = o >> 1
}
return o
}
// void write_float(Float16 value) {
// if (value.sign == 1) {
// write('-')
// }
// write_int(value.exponent)
// write('.')
// write_int(value.fraction)
// }
// Quadratic approximation of the sin function
Float16 sin(Float16 x){
Float16 piSquared = MultFloats(PI, PI)
Float16 four = IntToFloat(4)
return MultFloats((DivFloats(MultFloats(four,x), piSquared)),SubFloats(PI,x))
}
// Returns true if values a and b are within +-range of eachother
bool WithinRange(SignedInt aI, SignedInt bI, int range){
var a = aI.val
var b = bI.val
if(a > b){
if((a-b)<=range){
// write_int(a-b)
return true
}
else{
return false
}
}
else if(a < b){
if((b-a)<=range){
// write_int(b-a)
return true
}
else{
return false
}
}
return true
}
int Delta(int a, int b){
if(a > b)
return a - b
else if(a < b)
return b - a
return 0
}
// Temporary bitwise not ~ operator function, since currently that operator
// is broken and only returns 0 unless used like this:
int NOT(int x){
return ~x&0b1111111111111111
}
// Convert integer to it's negative value in twos complement
int Negative(int x){
return NOT(x) + 1
}
inline int XOR(int a, int b){
return (a|b)&(NOT(a&b))
}
int get_shift_amnt(int man, int exp){
return 10-exp
}
// string DecimalToBinaryString(int a)
// {
// uint b = (uint)a
// string binary = ""
// uint mask = 0x80000000u
// while (mask > 0)
// {
// binary += ((b & mask) == 0) ? '0' : '1'
// mask >>= 1
// }
// return binary
// }
// Kindof inefficient square root method
int sqrt(int x){
int counter=1
int sqroot=1
while(sqroot <= x)
{
counter += 1
sqroot = counter*counter
}
return counter - 1
}
// Returns the integer base raised to the power integer exponent
int pow(SignedInt base, SignedInt exponent){
var calculated = base
var finalSign = 0
if(base.s)
finalSign = !(exponent.val%2==0)
// If the exponent is negative, we divide each time instead of multiply
if(exponent.s == 1){
for (var i = 1; i < exponent.val; i++){
calculated = calculated / base
}
}
// Otherwise it is normal multiplication exponent
else {
for (var i = 1; i < exponent.val; i++){
calculated = calculated * base
}
}
return calculated|(finalSign<<15)
}
// Adjust a float so that the 11th bit in the mantissa is 1, the whole number
Float16 normalizeFloat(int fraction, int exponent){
var frac = fraction
var exp = exponent
// If the 11th digit is already the whole number 1
if((frac & 0b1111100000000000) == 0 && (frac & 0b10000000000) == 0b10000000000){
exp += 1
}
else{
// If leftmost digit is further than mantissa max of 10, then shift right
for (var j = 0; j < 10; j++){
// If the left has no 1s, then stop shifting
if((frac & 0b1111100000000000) == 0)
break
// Otherwise shift again
frac = frac >> 1
exp += 1
}
// Shift left until 11th digit is 1
for (var i = 0; i < 12; i++){
// If the left digit is 1, then stop shifting
if((frac & 0b10000000000) == 0b10000000000)
break
// Otherwise shift again
frac = frac << 1
exp -= 1
}
}
frac = frac&0b1111111111
Float16 outFloat = { sign : 0, exponent : exp, fraction : frac };
return outFloat
}
int deNormalizeFraction(int f, int exp){
bool sn = (10-exp>60000)
var vl = 10-exp
// SignedInt powNum = { s : 0, val : 0 };
if (sn){
vl = 65535-(10-exp)
// powNum.s = 1
}
// powNum.val = vl
// return (f * pow(2, powNum))
// If negative
if(sn == 1){
return f<<vl
}
// Otherwise shift normal way
else{
return f>>vl
}
}
SignedInt AddSignedInts(SignedInt a, SignedInt b){
SignedInt outInt = {s: 0, val:0};
// If the signs are the same, just add and return that same sign
if(a.s == b.s){
outInt.s = a.s
outInt.val = (a.val+b.val)
}
// Else if the signs are different and a is negative while b is positive, subtract a from b (b-a)
else if(a.s == 1){
// If the value of A is bigger than the value of B, then that means subtracting will make B negative
if(a.val > b.val){
outInt.s = 1
outInt.val = a.val-b.val
}
else{
outInt.val = b.val-a.val
}
// outInt.val = Delta(a.val,b.val)
}
// Else if the signs are different and b is negative while a is positive, subtract b from a (a-b)
else if(b.s == 1){
// If the value of B is bigger than the value of A, then that means subtracting will make A negative
if(b.val > a.val){
outInt.s = 1
outInt.val = b.val-a.val
}
else{
outInt.val = a.val-b.val
}
// outInt.val = Delta(b.val,a.val)
}
return outInt
}
// SignedInt a = {s:1, val:13};
// SignedInt b = {s:1, val:14};
// var outint = MultSignedInts(a, b)
// write_signed_int(a)
// write('*')
// write_signed_int(b)
// write('=')
// write_signed_int(outint)
// SignedInt a = {s:1, val:13};
// SignedInt b = {s:1, val:14};
// var comp = '>'
// var outint = CompareSignedInts(a, b, comp)
// write_signed_int(a)
// write('>')
// write_signed_int(b)
// write('=')
// if(outint){
// write('t')
// write('r')
// write('u')
// write('e')
// }
// else{
// write('f')
// write('a')
// write('l')
// write('s')
// write('e')
// }
SignedInt SubSignedInts(SignedInt a, SignedInt b){
// Invert b's sign bit
SignedInt bInt = b
bInt.s = !bInt.s
SignedInt outInt = AddSignedInts(a, bInt);
return outInt
}
SignedInt MultSignedInts(SignedInt a, SignedInt b){
// Determine final sign, if they are different it is 1, if they are both 0 it is 0, and if they are both 1 it is also 0
var finalSign = 0
if(a.s != b.s)
finalSign = 1
var finalVal = a.val*b.val
SignedInt outInt = {s:finalSign, val:finalVal};
return outInt
}
// Subtract two ints, and return a signed int to allow for negative numbers
SignedInt SubToSignedInt(int a, int b){
var aval = a
var bval = b
// Convert each to a signed int
SignedInt aInt = {s:0, val:aval};
SignedInt bInt = {s:1, val:bval}; // Invert b's sign
SignedInt outInt = {s:0, val:0};
outInt = AddSignedInts(aInt, bInt);
return outInt
}
SignedInt Signed(int a){
SignedInt outInt = {s: 0, val:a};
return outInt
}
SignedInt NegativeSigned(int a){
SignedInt outInt = {s: 1, val:a};
return outInt
}
// Return the integer rounded approximation of a float
int FloatToUnsignedInt(Float16 fl){
var tmpF = fl.fraction + 0b10000000000
var tmpE = fl.exponent - 15
var ouval = deNormalizeFraction(tmpF, tmpE) // Int version
return ouval
}
// Return the signed integer rounded approximation of a float
SignedInt FloatToInt(Float16 fl){
var tmpF = fl.fraction + 0b10000000000
// tmpF = TruncateTrailingZeros(tmpF)
var tmpE = fl.exponent - 15
var si = fl.sign
// int out = tmpF >> get_shift_amnt(tmpF, tmpE)
var ouval = deNormalizeFraction(tmpF, tmpE) // Int version
SignedInt out = { s:si, val:ouval }; // Add correct sign
return out
}
bool CompareSignedInts(SignedInt a, SignedInt b, int comp){
int compresult = 0 // This stores the result of the comparison, 0 is for equal, 1 is if a > b, and 2 is if a < b
// If a is negative and b is not it is only less than
if(a.s > b.s){
compresult = 2
}
// If b is negative and a is not it is only greater than
else if(a.s < b.s){
compresult = 1
}
// If the signs are both negative, check each absolute value
// (since we are negative, a greater magnitude means a lower number)
else if(a.s == 1){
// If the values are the same
if(a.val == b.val){
compresult = 0
}
// If a is greater than b
else if(a.val > b.val){
compresult = 2
}
// If b is greater than a
else if(a.val < b.val){
compresult = 1
}
}
// If the signs are both positive, check each absolute value
else if(a.s == 0){
// If the values are the same
if(a.val == b.val){
compresult = 0
}
// If a is greater than b
else if(a.val > b.val){
compresult = 1
}
// If b is greater than a
else if(a.val < b.val){
compresult = 2
}
}
// Now that the actual comparision is done, return a true or false based upon what we are looking for in `comp`
if(comp == '='){ // Equal
if(compresult == 0)
return true
}
else if(comp == '>'){ // Greater
if(compresult == 1)
return true
}
else if(comp == '<'){ // Less
if(compresult == 2)
return true
}
return false
}
bool CompareFloats(Float16 a, Float16 b, int comp){
int compresult = 0 // This stores the result of the comparison, 0 is for equal, 1 is if a > b, and 2 is if a < b
// If a is negative and b is not it is only less than
if(a.sign > b.sign){
compresult = 2
}
// If b is negative and a is not it is only greater than
else if(a.sign < b.sign){
compresult = 1
}
// If the signs are both negative, check each absolute value
// (since we are negative, a greater magnitude means a lower number)
else if(a.sign == 1){
// If the exponents are the same, compare mantisssas
if(a.exponent == b.exponent){
// If they are the same, then these values are equal
if(a.fraction == b.fraction){
compresult = 0
}
// Else if a mantissa is greater than b
else if(a.fraction > b.fraction){
compresult = 2 // A greater mantissa means a lower negative number
}
// Else if b mantissa is greater than a
else if(b.fraction > a.fraction){
compresult = 1
}
}
// If a is greater than b
else if(a.exponent > b.exponent){
compresult = 2
}
// If b is greater than a
else if(a.exponent < b.exponent){
compresult = 1
}
}
// If the signs are both positive, check each absolute value
else if(a.sign == 0){
// If the exponents are the same, compare mantisssas
if(a.exponent == b.exponent){
// If they are the same, then these values are equal
if(a.fraction == b.fraction){
compresult = 0
}
// Else if a mantissa is greater than b
else if(a.fraction > b.fraction){
compresult = 1
}
// Else if b mantissa is greater than a
else if(b.fraction > a.fraction){
compresult = 2
}
}
// If a is greater than b
else if(a.exponent > b.exponent){
compresult = 1
}
// If b is greater than a
else if(a.exponent < b.exponent){
compresult = 2
}
}
// Now that the actual comparision is done, return a true or false based upon what we are looking for in `comp`
if(comp == '='){ // Equal
if(compresult == 0)
return true
}
else if(comp == '>'){ // Greater
if(compresult == 1)
return true
}
else if(comp == '<'){ // Less
if(compresult == 2)
return true
}
return false
}
// Return the integer rounded approximation of a float
Float16 IntToFloat(SignedInt x){
// Find the exponent, by shifting left until the largest digit is reached
var count = 1
var tmp = x.val
var mask = 1
for (var i = 0; i < 16; i++){
// If the first digit is 1, then stop shifting because we have the count
if((tmp & 0b1000000000000000) == 0b1000000000000000)
break
// Otherwise shift again
tmp = tmp << 1
count += 1
}
// Get exponent from count, then add offset of 15 for float format
var exp = (15-count)+15
var mant = x.val
// If the mantissa is >= 10 bits long already, only use first 10
if(mant > 0b11111111111){
mant = mant >> ((16-count)-11)
}
else if(mant < 0b10000000000){
mant = mant << (11-(16-count))
}
Float16 outFloat = normalizeFloat(mant, exp);
outFloat.sign = x.s
return outFloat
}
Float16 AddFloats(Float16 floatA, Float16 floatB){
Float16 outFloat = { sign : 0, exponent : 0, fraction : 0 };
// If the exponents are different, then we need to make the float with the smaller exponent the same as the larger one
if(floatA.exponent > floatB.exponent){
var tmpMB = ((floatB.fraction+0b10000000000)>>(floatA.exponent-floatB.exponent))
var tmpMA = floatA.fraction+0b10000000000
bool finalSign = false
if(floatB.sign == true){
tmpMB = Negative(tmpMB)
// Determine the final sign, it is 1 if B is > A
if(floatB.exponent > floatA.exponent)
finalSign = true
// If they have the same exponent, check mantissas as well
else if((floatB.exponent == floatA.exponent) && (floatB.fraction > floatA.fraction))
finalSign = true
}
if(floatA.sign == true){
tmpMA = Negative(tmpMA)
// Determine the final sign, it is 1 if A is > B
if(floatA.exponent > floatB.exponent)
finalSign = true
// If they have the same exponent, check mantissas as well
else if((floatA.exponent == floatB.exponent) && (floatA.fraction > floatB.fraction))
finalSign = true
}
int tmpM = tmpMA+tmpMB
int exp = floatA.exponent
outFloat = { sign : finalSign, exponent : exp, fraction : tmpM };
if ((tmpM & 0b10000000000) != 0b10000000000) // If the 11th bit is 0, then normalize
outFloat = normalizeFloat(tmpM, exp)
}
// 150+500
else if(floatA.exponent < floatB.exponent){
int tmpMB = floatB.fraction+0b10000000000
int tmpMA = ((floatA.fraction+0b10000000000)>>(floatB.exponent-floatA.exponent))
bool finalSign = false
if(floatB.sign == true){
tmpMB = Negative(tmpMB)
// Determine the final sign, it is 1 if B is > A
if(floatB.exponent > floatA.exponent)
finalSign = true
// If they have the same exponent, check mantissas as well
else if((floatB.exponent == floatA.exponent) && (floatB.fraction > floatA.fraction))
finalSign = true
}
if(floatA.sign == true){
tmpMA = Negative(tmpMA)
// Determine the final sign, it is 1 if A is > B
if(floatA.exponent > floatB.exponent)
finalSign = true
// If they have the same exponent, check mantissas as well
else if((floatA.exponent == floatB.exponent) && (floatA.fraction > floatB.fraction))
finalSign = true
}
int tmpM = tmpMA+tmpMB
int exp = floatB.exponent
outFloat = { sign : finalSign, exponent : exp, fraction : tmpM };
if ((tmpM & 0b10000000000) != 0b10000000000) // If the 11th bit is 0, then normalize
outFloat = normalizeFloat(tmpM, floatB.exponent)
}
// Else the exponents are the same, so no need to change them
else {
int tmpMB = (floatB.fraction+0b10000000000)>>1
int tmpMA = (floatA.fraction+0b10000000000)>>1
bool finalSign = false
if(floatB.sign == true){
tmpMB = Negative(tmpMB)
// Determine the final sign, it is 1 if B is > A
if(floatB.exponent > floatA.exponent)
finalSign = true
// If they have the same exponent, check mantissas as well
else if((floatB.exponent == floatA.exponent) && (floatB.fraction > floatA.fraction))
finalSign = true
}
if(floatA.sign == true){
tmpMA = Negative(tmpMA)
// Determine the final sign, it is 1 if A is > B
if(floatA.exponent > floatB.exponent)
finalSign = true
// If they have the same exponent, check mantissas as well
else if((floatA.exponent == floatB.exponent) && (floatA.fraction > floatB.fraction))
finalSign = true
}
int tmpM = tmpMA+tmpMB
int exp = floatB.exponent+1
outFloat = { sign : finalSign, exponent : exp, fraction : tmpM };
if ((tmpM & 0b10000000000) != 0b10000000000) // If the 11th bit is 0, then normalize
outFloat = normalizeFloat(tmpM, floatB.exponent)
}
return outFloat
}
Float16 SubFloats(Float16 floatA, Float16 floatB){
Float16 fb = floatB
fb.sign = !fb.sign
return AddFloats(floatA, fb)
}
Float16 MultFloats(Float16 floatA, Float16 floatB){
int tmpMA = floatA.fraction+0b10000000000
int tmpMB = floatB.fraction+0b10000000000
int snA = floatA.sign
int snB = floatB.sign
// Determine final sign, if they are different it is 1, if they are both 0 it is 0, and if they are both 1 it is also 0
var finalSign = 0
if(snA != snB)
finalSign = 1
// Get the lowest bit that is 1 in either A or B
var count = 0 // count is for the lowest index there is a 1
for (var i = 0; i < 16; i++){
// If the lowest bit in A or B is 1, stop the loop
if(((tmpMA & 1) == 1) || ((tmpMB & 1) == 1))
break
else{
// Shift right
tmpMA = tmpMA >> 1
tmpMB = tmpMB >> 1
count += 1
}
}
int tmpM = tmpMA*tmpMB
int exp = floatA.exponent+floatB.exponent-15+count-4
Float16 outFloat = { sign : finalSign, exponent : exp, fraction : tmpM };
if ((tmpM & 0b10000000000) != 0b10000000000) // If the 11th bit is 0, then normalize
outFloat = normalizeFloat(tmpM, exp)
return outFloat
}
// Credit to njuffa (https://cs.stackexchange.com/users/51535/njuffa),
// "Simple algorithm for IEEE-754 division on 8-bit CPU?"",
// URL (version: 2019-02-28): https://cs.stackexchange.com/q/104961
Float16 DivFloats (Float16 a, Float16 b)
{
int r = 0
int x = 0
int y = 0
int sign = 0
int expo_x = 0
int expo_y = 0
int i = 0
int odd = 0
int rnd = 0
int sticky = 0
Float16 outVal = {sign:0, exponent:0, fraction:0};
/* extract biased exponents and sign bits */
expo_x = a.exponent
expo_y = b.exponent
sign = XOR(a.sign, b.sign)
var processNow = false
for (var jj = 0; jj < 2; jj++){
if (((expo_x >= 1) && (expo_x <= 0b11110) &&
(expo_y >= 1) && (expo_y <= 1)) || processNow) { /* fast path */
// divide:
/* add significand leading 1 int */
x = a.fraction + 0b10000000000
y = b.fraction + 0b10000000000
/* compute exponent of result */
outVal.exponent = expo_x - expo_y + 15
/* dividend may not be smaller than divisor: normalize */
if (x < y) {
x = x << 1
outVal.exponent -= 1
}
/* generate quotient one bit at at time */
r = 0
for (i = 0; i < 12; i++) {
r = r << 1
if (x >= y) {
x = x - y
r = r | 1
}
x = x << 1
}
/* OR remainder bits into sticky bit */
sticky = (x != 0)
if ((outVal.exponent >= 1) &&
(outVal.exponent <= 0b11110)) { /* normal, may overflow to infinity*/
/* extract round and lsb bits */
rnd = (r & 1)
odd = (r & 2) != 0
/* remove round bit from quotient and round to-nearest-even */
r = (r >> 1) + (rnd & (sticky | odd))
/* set significand */
outVal.fraction = r - 0b10000000000
} else if (outVal.exponent > 0b11110) { // overflow: infinity
outVal.exponent = 0b011111
} else { /* underflow: result is zero, subnormal, or smallest normal */
int shift = (1 - outVal.exponent)
/* clamp shift count */
if (shift > 12) shift = 12
/* OR shifted-off bits of significand into sticky bit */
sticky = sticky | ((r & NOT(NOT(0) << shift)) != 0)
/* denormalize significand */
r = r >> shift
/* extract round and lsb bits */
rnd = (r & 1)
odd = (r & 2) != 0
/* remove round bit from quotient and round to-nearest-even */
// r = (r >> 1) + (rnd & (sticky | odd));
outVal.fraction = (r >> 1) + (rnd & (sticky | odd))
}
// /* combine sign bit with combo of exponent and significand */
// r = r | sign;
outVal.sign = sign
} else { /* slow path */
// /* take absolute value of arguments */
// x = a & NOT(SIGN_MASK);
// y = b & NOT(SIGN_MASK);
/* if dividend is a NaN, convert that NaN into a QNaN and return it */
if ((a.exponent == 0b11111) && (a.fraction > 0)){
outVal.sign = a.sign
outVal.fraction = a.fraction | 0b1000000000
outVal.exponent = a.exponent
return outVal
}
/* if divisor is a NaN, convert that NaN into a QNaN and return it */
if ((b.exponent == 0b11111) &&(b.fraction > 0)){
outVal.sign = b.sign
outVal.fraction = b.fraction | 0b1000000000
outVal.exponent = b.exponent
return outVal
}
/* dividend and divisor are both zero or infinity: invalid operation */
if (((a.exponent == 0 && a.fraction == 0) && (b.exponent == 0 && b.fraction == 0)) ||
((a.exponent == 0b11111) && (b.exponent == 0b11111)))
{
outVal.sign = 1
outVal.fraction = 0b1000000000
outVal.exponent = 0b11111
return outVal
}
/* 0/y or x/INF -> 0 */
if ((a.exponent == 0 && a.fraction == 0) || (a.exponent == 0b11111))
{
outVal.sign = sign
return outVal
}
/* x/0 or INF/y -> INF */
if ((b.exponent == 0 && b.fraction == 0) || (a.exponent == 0b11111))
{
outVal.sign = sign
outVal.fraction = 0
outVal.exponent = 0b11111
return outVal
}
/* if dividend is a subnormal, normalize it */
if (expo_x == 0) {
expo_x += 1
int afrac = a.fraction
for (var afs = 0; afs < 16; afs++){
if((afrac & 0b10000000000)==0b10000000000)
break
afrac = afrac << 1
expo_x -= 1
}
a.fraction = afrac
}
/* if divisor is a subnormal, normalize it */
if (expo_y == 0) {
expo_y += 1
int bfrac = b.fraction
for (var bs = 0; bs < 16; bs++){
if((bfrac & 0b10000000000)==0b10000000000)
break
bfrac = bfrac << 1
expo_y -= 1
}
b.fraction = bfrac
}
/* now that dividend and divisor are normalized, do the division */
// goto divide
processNow = true
}
}
return outVal
}
/////////////////////////
// These are the tests //
/////////////////////////
ChangeHighlightColor(0, 0, 200)
write('f')
write('l')
write('o')
write('a')
write('t')
write(' ')
write('t')
write('e')
write('s')
write('t')
write('s')
write(':')
newline()
// // herethisis:
// var valu = 0b1000000001
// newline()
// write_binary(valu)
// valu = valu^0b1100000001
// newline()
// write_binary(valu)
// // goto herethisis
// Float16 f16 = { sign : 1, exponent : 5, fraction : 10 };
// write_int(f16.exponent)
SignedInt correctVal = {s:0, val:0};
SignedInt val = {s:0, val:0};
// write_signed_int(RetSame(val))
// t1: Float to int 1022
correctVal = {s:0, val:1022};
ChangeHighlightColor(0, 128, 128)
write('t')