-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.yabal
More file actions
1559 lines (1269 loc) · 40.7 KB
/
triangle.yabal
File metadata and controls
1559 lines (1269 loc) · 40.7 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
const var floats = create_pointer<float16_t>(0x0, 1);
const var signedInts = create_pointer<signedint_t>(0x1111, 1);
const var screen = create_pointer(53870, 1)
const var sinTable = create_pointer(10000, 1)
const var pointsXY = create_pointer<Point>(11000, 1)
const var pointsZ = create_pointer(12000, 1)
const var RpointsX = create_pointer<signedint_t>(14000, 1)
const var RpointsY = create_pointer<signedint_t>(15000, 1)
const var RpointsZ = create_pointer<signedint_t>(16000, 1)
const var facesX = create_pointer(17000, 1);
const var facesY = create_pointer(18000, 1);
const var facesZ = create_pointer(19000, 1);
const var expansionPorts = create_pointer(53500, 1)
const var chars = create_pointer(53546, 1)
int offset = 0
var highlightColor = 0
var highlightOppositeColor = 0b1111111111111111
void draw_line(int x1, int y1, int x2, int y2, int color) {
signedint_t dx = {s:0, val:0};
signedint_t dy = {s:0, val:0};
signedint_t e = {s:0, val:0};
signedint_t incx = {s:0, val:1};
signedint_t incy = {s:0, val:1};
signedint_t inc1 = {s:0, val:0};
signedint_t inc2 = {s:0, val:0};
signedint_t x = {s:0, val:x1};
signedint_t y = {s:0, val:y1};
dx = SubTosignedint_t(x2,x1) & 32767
dy = SubTosignedint_t(y2,y1) & 32767
// ~PC 800
// dx.s = 0
// dy.s = 0
if (x2 < x1)
incx.s = 1
if (y2 < y1)
incy.s = 1
if (dx > dy) {
e = (dy - dx) * 2;
inc1 = (dy - dx) * 2;
inc2 = dy * 2;
const int maxval = ((int)dx) & 32767
for (int i = 0; i < maxval; i++) {
if (((int)e) < 32768 || ((((int)e) >= 32768) && (e.val == 0))) { // if e >= 0
y = y + incy;
e = e + inc1;
}
else{
e = e + inc2;
}
x = x + incx;
if(x.val>=107||y.val>107) // Don't draw pixels out of range
continue
SetPixel(x, y, color)
}
} else {
e = (dx * 2) - dy;
inc1 = (dx - dy) * 2;
inc2 = dx * 2;
const int maxval = ((int)dy) & 32767
for (int i = 0; i < maxval; i++) {
if (((int)e) < 32768 || ((((int)e) >= 32768) && (e.val == 0))) { // if e >= 0
x = x + incx;
e = e + inc1;
}
else{
e = e + inc2;
}
y = y + incy;
int xval = ((int)x) & 32767
int yval = ((int)y) & 32767
if(xval>=107||yval>107) // Don't draw pixels out of range
continue;
SetPixel(xval, yval, color)
}
}
}
void DrawWireTriangle(int x0, int y0,int x1, int y1,int x2, int y2, int color){
draw_line(x0, y0, x1, y1, color)
draw_line(x1, y1, x2, y2, color)
draw_line(x0, y0, x2, y2, color)
}
void DrawWireQuad(int x0, int y0,int x1, int y1,int x2, int y2,int x3, int y3, int color){
draw_line(x0, y0, x1, y1, color)
draw_line(x1, y1, x2, y2, color)
draw_line(x2, y2, x3, y3, color)
draw_line(x3, y3, x0, y0, color)
}
void DrawFilledCircle(int xCenter, int yCenter, int radius, int color){
const int radiusSquared = radius*radius;
const int radiusSquaredPlusRadius = radiusSquared + radius;
const signedint_t xOrig = NegativeSigned(radius);
signedint_t x = xOrig;
signedint_t y = NegativeSigned(radius);
int rad1 = radius+1;
int newX = 0;
int newY = 0;
while(((int)y) >= 32768 || y.val < rad1){
while(((int)x) >= 32768 || x.val < rad1){
newX = xCenter + SignedToTwosComp(x);
newY = yCenter + SignedToTwosComp(y);
// Make sure pixel is inside screen, otherwise don't render it.
if(newX >= 107 || newY >= 107){
x = x + 1;
continue;
}
if((x * x) + (y * y) <= radiusSquaredPlusRadius){ // Calculate distance between point and center of circle. If lessthan or equal to radius, color pixel.
SetPixel(newX, newY, color);
}
x = x + 1;
}
x = xOrig;
y = y + 1;
}
}
void DrawWireCircle(int xCenter, int yCenter, int radius, int color){
int radiusSquared = radius*radius
signedint_t xOrig = NegativeSigned(radius)
signedint_t x = xOrig
signedint_t y = xOrig
var rad1 = radius+1
var newX = 0
var newY = 0
while(((int)y) >= 32768 || y.val< rad1){
while(((int)x) >= 32768 || x.val< rad1){
newX = xCenter+SignedToTwosComp(x)
newY = yCenter+SignedToTwosComp(y)
// Make sure pixel is inside screen, otherwise don't render it.
if(newX>=107||newY>=107){
x = Addsignedint_ts(x, 1)
continue
}
signedint_t tot = Addsignedint_ts(x.val*x.val,y.val*y.val)
if((tot.val >= radiusSquared-radius) && (tot.val <= radiusSquared+radius)){ // Calculate distance between point and center of circle. If lessthan or equal to radius, color pixel.
SetPixel(newX, newY, color)
}
// write('x')
x = Addsignedint_ts(x, 1)
}
x = xOrig
y = Addsignedint_ts(y, 1)
}
}
var lfsr = 0b1011101011101001;
int lfsr()
{
var bit = 0
bit = ((lfsr >> 1) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 7)) & 1
lfsr = (lfsr >> 1) | (bit << 15)
return bit;
}
int rand(int bits)
{
// var start_state = 0b1100101011011100
var out = 0
int count = 0
// var period = 0
while(count < bits)
{
out = out << 1
out = out | lfsr()
count += 1
}
return out;
}
sinTable[0] = 0;
sinTable[1] = 2;
sinTable[2] = 4;
sinTable[3] = 6;
sinTable[4] = 8;
sinTable[5] = 11;
sinTable[6] = 13;
sinTable[7] = 15;
sinTable[8] = 17;
sinTable[9] = 20;
sinTable[10] = 22;
sinTable[11] = 24;
sinTable[12] = 26;
sinTable[13] = 28;
sinTable[14] = 30;
sinTable[15] = 33;
sinTable[16] = 35;
sinTable[17] = 37;
sinTable[18] = 39;
sinTable[19] = 41;
sinTable[20] = 43;
sinTable[21] = 45;
sinTable[22] = 47;
sinTable[23] = 50;
sinTable[24] = 52;
sinTable[25] = 54;
sinTable[26] = 56;
sinTable[27] = 58;
sinTable[28] = 60;
sinTable[29] = 62;
sinTable[30] = 63;
sinTable[31] = 65;
sinTable[32] = 67;
sinTable[33] = 69;
sinTable[34] = 71;
sinTable[35] = 73;
sinTable[36] = 75;
sinTable[37] = 77;
sinTable[38] = 78;
sinTable[39] = 80;
sinTable[40] = 82;
sinTable[41] = 83;
sinTable[42] = 85;
sinTable[43] = 87;
sinTable[44] = 88;
sinTable[45] = 90;
sinTable[46] = 92;
sinTable[47] = 93;
sinTable[48] = 95;
sinTable[49] = 96;
sinTable[50] = 98;
sinTable[51] = 99;
sinTable[52] = 100;
sinTable[53] = 102;
sinTable[54] = 103;
sinTable[55] = 104;
sinTable[56] = 106;
sinTable[57] = 107;
sinTable[58] = 108;
sinTable[59] = 109;
sinTable[60] = 110;
sinTable[61] = 111;
sinTable[62] = 113;
sinTable[63] = 114;
sinTable[64] = 115;
sinTable[65] = 116;
sinTable[66] = 116;
sinTable[67] = 117;
sinTable[68] = 118;
sinTable[69] = 119;
sinTable[70] = 120;
sinTable[71] = 121;
sinTable[72] = 121;
sinTable[73] = 122;
sinTable[74] = 123;
sinTable[75] = 123;
sinTable[76] = 124;
sinTable[77] = 124;
sinTable[78] = 125;
sinTable[79] = 125;
sinTable[80] = 126;
sinTable[81] = 126;
sinTable[82] = 126;
sinTable[83] = 127;
sinTable[84] = 127;
sinTable[85] = 127;
sinTable[86] = 127;
sinTable[87] = 127;
sinTable[88] = 127;
sinTable[89] = 127;
signedint_t sin(int x){
x+=360;
int loopTimes = 0;
while(x>179){
x -= 180
loopTimes += 1;
}
if(x > 89){
x = 90 - (x-89);
}
int outSinVal = sinTable[x];
signedint_t outVal = {s:0, val:(sinTable[x])}
if(loopTimes % 2 == 0){ // should be negative
outVal.s = 1
return outVal
}
// else, positive
return outVal
}
signedint_t cos(int x){
return sin(x-90)
}
int _RotatedX = 1;
int _RotatedY = 1;
int _RotatedZ = 1;
signedint_t rotationXOffset = {s:0, val:54};
signedint_t rotationYOffset = {s:0, val:54};
void RotatePoint(int index, int degrees){
Point value = pointsXY[index]
signedint_t inX = value.x
signedint_t inY = value.y
// Calculate sine and cosine of the angle using integer math
int angle = degrees
signedint_t sinVal = sin(angle);
signedint_t cosVal = cos(angle);
// Translate point to the origin
// signedint_t offset = {s:0, val:54};
signedint_t translatedX = inX - rotationXOffset;
signedint_t translatedY = inY - rotationYOffset;
// Perform rotation using integer math
signedint_t rotatedX = (translatedX * cosVal - translatedY * sinVal)/128;
signedint_t rotatedY = (translatedX * sinVal + translatedY * cosVal)/128;
// Translate point back to its original position
_RotatedX = rotatedX+rotationXOffset;
_RotatedY = rotatedY+rotationYOffset;
}
void RotateCoords(int x, int y, int degrees){
signedint_t inX = x
signedint_t inY = y
// Calculate sine and cosine of the angle using integer math
int angle = degrees
signedint_t sinVal = sin(angle);
signedint_t cosVal = cos(angle);
// Translate point to the origin
// signedint_t offset = {s:0, val:54};
signedint_t translatedX = inX - rotationXOffset;
signedint_t translatedY = inY - rotationYOffset;
// Perform rotation using integer math
signedint_t rotatedX = (translatedX * cosVal - translatedY * sinVal)/128;
signedint_t rotatedY = (translatedX * sinVal + translatedY * cosVal)/128;
// Translate point back to its original position
_RotatedX = rotatedX+rotationXOffset;
_RotatedY = rotatedY+rotationYOffset;
}
void RotatePoint3D(int index, int xRotate, int yRotate, int zRotate){
Point xy = pointsXY[index]
int zz = pointsZ[index]
signedint_t inX = xy.x
signedint_t inY = xy.y
signedint_t inZ = zz
RotateCoords(inX, inY, zRotate); // Rotate 2D around Z axis (front on)
int rX = _RotatedX&0b111111111111111
int rY = _RotatedY&0b111111111111111
RotateCoords(rX, inZ, yRotate); // Rotate 2D around Y axis (top down)
rX = _RotatedX&0b111111111111111
int rZ = _RotatedY&0b111111111111111
RotateCoords(rZ, rY, xRotate); // Rotate 2D around X axis (from right)
rY = _RotatedY&0b111111111111111
rZ = _RotatedX&0b111111111111111
_RotatedX = rX;
_RotatedY = rY;
_RotatedZ = rZ;
}
int counter = 0;
int numberOfPoints = 0;
int numberOfFaces = 0;
import "./mit.obj.yabal";
int rotationX = 0;
int rotationY = 0;
int rotationZ = 0;
while (true) {
// counter += 1;
// if(counter >= 360)
// counter = 0;
int mouseVal = expansionPorts[1];
bool leftMousePressed = (mouseVal >> 14) & 1; // Shift value from the left side to the right and get it by itself
if(leftMousePressed){
int mouseXPos = (mouseVal >> 7) & 0b1111111; // Shift X coord value from the left side to the right and get it by itself
int mouseYPos = mouseVal & 0b1111111; // Get y coord by itself
rotationY = mouseXPos*4+180;
rotationX = mouseYPos*4;
}
// ~PC 7000
// iterate all points, and rotate them.
for(int i = 0; i < numberOfPoints; i++){
RotatePoint3D(i, rotationX, rotationY, 0)
RpointsX[i] = _RotatedX;
RpointsY[i] = _RotatedY;
RpointsZ[i] = ClampInt((_RotatedZ+54), 1, 1000);
}
const int focalLength = 1;
signedint_t focalOffset = 54;
const int lineColor = 0b11111111111111111;
for(int i = 0; i < numberOfFaces; i++){
int fX = facesX[i];
int fY = facesY[i];
int fZ = facesZ[i];
int rPoi1x = ((RpointsX[fX] - focalOffset) * 54 / RpointsZ[fX]) + focalOffset;
int rPoi1y = ((RpointsY[fX] - focalOffset) * 54 / RpointsZ[fX]) + focalOffset;
int rPoi2x = ((RpointsX[fY] - focalOffset) * 54 / RpointsZ[fY]) + focalOffset;
int rPoi2y = ((RpointsY[fY] - focalOffset) * 54 / RpointsZ[fY]) + focalOffset;
int rPoi3x = ((RpointsX[fZ] - focalOffset) * 54 / RpointsZ[fZ]) + focalOffset;
int rPoi3y = ((RpointsY[fZ] - focalOffset) * 54 / RpointsZ[fZ]) + focalOffset;
DrawWireTriangle(rPoi1x, rPoi1y, rPoi2x, rPoi2y, rPoi3x, rPoi3y, lineColor);
// DrawFilledCircle(rPoi1x, rPoi1y, 1, 0b000000000011111);
// DrawFilledCircle(rPoi2x, rPoi2y, 1, 0b111110000000000);
// DrawFilledCircle(rPoi3x, rPoi3y, 1, 0b000001111100000);
}
// DrawWireTriangle(v0x, v0y, v2x, v2y, v3x, v3y, color);
// // iterate rotated points, and draw their triangles
// for(int i = 0; i < 8; i++){
// RotatePoint(i, 10)
// RpointsXY[i] = _RotatedX
// int rX = (_RotatedX & 0b1111111100000000)>>8
// int rY = _RotatedX & 0b11111111
// SetPixel(rX, rY, 0b111111)
// }
// RotatePoint(0, counter)
// int rX = (_RotatedX & 0b1111111100000000)>>8
// int rY = _RotatedX & 0b11111111
// SetPixel(rX, rY, 0b111111)
// RotatePoint(4, counter)
// int rX = (_RotatedX & 0b1111111100000000)>>8
// int rY = _RotatedX & 0b11111111
// SetPixel(rX, rY, 0b11111100000)
//SetPixel(v0x, v0y, 0b11111)
// write_int(v1x)
// newline()
// write_int(v1y)
// newline()
// // translate point back to origin:
// p.x -= cx;
// p.y -= cy;
// // rotate point
// int xnew = v1x * c - v1y * s;
// int ynew = v1x * s + v1y * c;
// // Back
// DrawWireTriangle(v4x, v4y, v5x, v5y, v7x, v7y, color); // Front right
// DrawWireTriangle(v5x, v5y, v6x, v6y, v7x, v7y, color); // Front left
// if(choice == 0)
// DrawWireCircle(x0, y0, r, color)
// else if(choice == 1)
//DrawWireTriangle(x0, y0, x1, y1, x2, y2, color)
// else if(choice == 2)
// DrawFilledCircle(x0, y0, r, color)
// else if(choice == 3)
//DrawFilledTriangle(x0, y0, x1, y1, x2, y2, color)
// break
asm{
VBUF
};
// DrawWireTriangle(rPoi1x, rPoi1y, rPoi2x, rPoi2y, rPoi3x, rPoi3y, 0);
// DrawWireTriangle(rPoi1x, rPoi1y, rPoi3x, rPoi3y, rPoi4x, rPoi4y, 0);
// // // Black out screen
// color = 0
// //int color = 23422;
// DrawWireTriangle(v0x, v0y, v1x, v1y, v2x, v2y, color)
// DrawWireTriangle(v0x, v0y, v2x, v2y, v3x, v3y, color)
for(int x = 0; x < (108*108); x++){
screen[x] = 0;
}
}
while (true){
}
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
}
void SetPixelSAFE(int x, int y, int color){
var screenOffset = (ClampInt(y, 0, 107) * 108) + ClampInt(x, 0, 107)
screen[screenOffset] = color
}
void SetPixel(int x, int y, int color){
var screenOffset = (y * 108) + x
screen[screenOffset] = color
}
int GetPixel(int x, int y){
var screenOffset = (ClampInt(y, 0, 107) * 108) + ClampInt(x, 0, 107)
return screen[screenOffset]
}
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) {
chars[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++
}
float16_t fabs(float16_t f){
float16_t outFloat = {sign:0, exponent:0, fraction:0};
outFloat.exponent = f.exponent
outFloat.fraction = f.fraction
return outFloat
}
signedint_t IntTosignedint_t(int x){
signedint_t o = {s:0, val:x};
return o
}
int SignedToTwosComp(signedint_t x){
int out = x.val
if(x.s){
out = Negative(out)
}
return out
}
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_t 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_t i) {
if(((int)i) >= 32768) // 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
}
bool WithinRange(signedint_t aI, signedint_t 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
}
int NOT(int x){
return ~x&0b1111111111111111
}
int Negative(int x){
return (~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
}
int sqrt(int x){
int counter=1
int sqroot=1
while(sqroot <= x)
{
counter += 1
sqroot = counter*counter
}
return counter - 1
}
int pow(signedint_t base, signedint_t 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(((int)exponent) >= 32768){
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)
}
float16_t 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_t 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_t 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_t Addsignedint_ts(signedint_t a, signedint_t b){
signedint_t 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)
outInt = (((int)a)&32768) | ((((int)a)+((int)b)) & 32767);
}
// Else if the signs are different and a is negative while b is positive, subtract a from b (b-a)
else if(((int)a) >= 32768){
// If the value of A is bigger than the value of B, then that means subtracting will make B negative
if((((int)a)&32767) > (((int)b)&32767)){
outInt = 32768 | ((((int)a)-((int)b)) & 32767);
}
else{
outInt.val = b.val-a.val
}
}
// Else if the signs are different and b is negative while a is positive, subtract b from a (a-b)
else if(((int)b) >= 32768){
// If the value of B is bigger than the value of A, then that means subtracting will make A negative
if((((int)b)&32767) > (((int)a)&32767)){
outInt = 32768 | ((((int)b)-((int)a)) & 32767);
}
else{
outInt.val = a.val-b.val
}
}
return outInt
}
signedint_t Subsignedint_ts(signedint_t a, signedint_t b){
// Invert b's sign bit
return Addsignedint_ts(a, (((int)(b)) ^ 0b1000000000000000));
}
signedint_t Multsignedint_ts(signedint_t a, signedint_t 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
return {s:(a.s != b.s), val:((int)(a * b)) & 32767};
}
signedint_t SubTosignedint_t(int a, int b){
// Convert each to a signed int
signedint_t aInt = {s:0, val:a};
signedint_t bInt = {s:1, val:b}; // Invert b's sign
return aInt + bInt;
}
signedint_t Signed(int a){
return {s: 0, val:a};
}
signedint_t NegativeSigned(int a){
return {s: 1, val:(a & 32767)}
}
signedint_t NegativeSigned(signedint_t a){
return {s: ~(a.s), val:a};
}
int FloatToUnsignedInt(float16_t fl){
var tmpF = fl.fraction + 0b10000000000
var tmpE = fl.exponent - 15
var ouval = deNormalizeFraction(tmpF, tmpE) // Int version
return ouval
}
signedint_t FloatToInt(float16_t 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_t out = { s:si, val:ouval }; // Add correct sign
return out
}
bool Comparesignedint_ts(signedint_t a, signedint_t 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
// (sinZe we are negative, a greater magnitude means a lower number)
else if(((int)a) >= 32768){
// 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(((int)a) < 32768){
// 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