-
Notifications
You must be signed in to change notification settings - Fork 1
/
prelude.hpp
5452 lines (4495 loc) · 159 KB
/
prelude.hpp
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
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdlib> // for free()
#include <functional>
#include <iomanip> // For setprecision and fixed
#include <iostream>
#include <math.h>
#include <random>
#include <sstream>
#include <string>
#include <vector>
// GMP and gc headers
#include "gc.h"
#include "gc_cpp.h"
#include "gmp.h"
#include "gmpxx.h"
#include <limits.h>
// hamt headers
#include "hamt.h"
#define MASK(val) (((u64)(val)) & ~(7ULL))
unsigned long long call_counter = 0;
// #define NULL_VALUE 0
#define NULL_VALUE 32
#define TRUE_VALUE 8
#define FALSE_VALUE 16
#define FLOAT_VAL 24
#define ENV_ARRAY 1
#define RANDOM_VALUE 63 // 00111111
// Making the choice not to use them because the intptr_t is available.
typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
enum DataType {
SPL = 0x0,
MPZ = 0x1,
HASH = 0x2,
STRING = 0x3,
MPF = 0x4,
CLO = 0x5,
INT = 0x6,
CONS = 0x7,
};
struct hash_struct;
std::string print_val(void *val);
void *equal_(void *arg1, void *arg2);
u64 hash_(void *val);
void *arg_buffer[999]; // This is where the arg buffer is called
long numArgs;
// Example print_val function
template <typename T> T print_val(T val) {
return val; // Just return the value for demonstration
}
template <typename T> bool isPositive(T number) { return number > 0; }
#define PRINT(val) (std::cout << (val) << std::endl);
#pragma region Types
// region Encoding and Decoding and Tags
inline void assert_type(bool cond, const char *msg) {
if (!cond) {
std::cout << "type assertion failed-> " << msg << std::endl;
exit(1);
}
}
// Checks if a floating-point value can be represented precisely as a float
inline bool is_within_float_precision(float val) {
return std::abs(val) <= 16777216.0f;
}
inline bool withinInt32BitRange(s64 val) {
return (val >= -2147483648LL) && (val <= 2147483647LL);
}
// ??why encode and decode functions for every type, just have one and make it
// take a void* and the type we want the tag variable to returns a void* that is
// tagged
inline u64 encode_int(s32 val) { return ((((u64)((u32)(val))) << 32) | INT); }
inline u64 encode_float(float val) {
u32 temp;
std::memcpy(&temp, &val, sizeof(float));
u64 encoded = ((u64)temp) << 32;
encoded |= FLOAT_VAL;
return encoded;
}
inline void *encode_mpz(mpz_t *val) {
return reinterpret_cast<void *>(((u64)val) | MPZ);
}
inline void *encode_mpf(mpf_t *val) {
return reinterpret_cast<void *>(((u64)val) | MPF);
}
inline void *encode_str(std::string *val) {
return reinterpret_cast<void *>(((u64)val) | STRING);
}
inline void *encode_clo(void **val) {
return reinterpret_cast<void *>(((u64)(val)) | CLO);
}
inline void *encode_bool(bool val) {
if (val) {
return reinterpret_cast<void *>(TRUE_VALUE);
}
return reinterpret_cast<void *>(FALSE_VALUE);
}
inline void *encode_cons(void **val) {
return reinterpret_cast<void *>(((u64)(val)) | CONS);
}
inline void *encode_hash(const hamt<hash_struct, hash_struct> *val) {
return reinterpret_cast<void *>(((u64)(val)) | HASH);
}
inline void *encode_null() { return reinterpret_cast<void *>(NULL_VALUE); }
// takes in a void * type and gets the tag, return it as an integer
inline int get_tag(void *val) {
u64 ptr = reinterpret_cast<u64>(val);
if ((ptr & 0x7) == SPL)
return (ptr & 0x38);
return (ptr & 7);
}
// for debugging purpose!
void print_mpf(mpf_t *arg) {
// std::cout << "-----start of print_mpf-----" << std::endl;
char buffer[1000];
std::string str(buffer);
gmp_sprintf(buffer, "%.10Ff", *arg);
std::cout << "print_mpf: val: " << std::string(buffer) << std::endl;
// std::cout << "-----end of print_mpf-----" << std::endl;
}
void print_mpz(const mpz_t *arg) { gmp_printf("print_mpz: val: %Zd\n", *arg); }
inline bool is_cons(void *lst) {
if (get_tag(lst) == CONS) {
return true;
}
return false;
}
inline s32 decode_int(void *val) {
u64 v = reinterpret_cast<u64>(val);
if ((v & 0x7) != INT)
assert_type(false, "Error in decode_int -> Type error: Not an Integer!");
return ((s32)((u32)(((v) & ~(7ULL)) >> 32)));
}
inline float decode_float(void *val) {
if (get_tag(val) != FLOAT_VAL)
assert_type(false, "Error in decode_float -> Type error: Not a float!");
u64 v = reinterpret_cast<u64>(val);
u32 temp = (v >> 32);
float result;
std::memcpy(&result, &temp, sizeof(float));
return result;
}
mpz_t *decode_mpz(void *val) {
// MASK does the casting to u64
if (get_tag(val) != MPZ)
assert_type(false, "Error in decode_mpz -> Type error: Not MPZ");
return reinterpret_cast<mpz_t *>(MASK(val));
}
mpf_t *decode_mpf(void *val) {
// MASK does the casting to u64
if (get_tag(val) != MPF)
assert_type(false, "Error in decode_mpf -> Type error: Not MPF");
return reinterpret_cast<mpf_t *>(MASK(val));
}
std::string *decode_str(void *val) {
// MASK does the casting to u64
if (get_tag(val) != STRING)
assert_type(false, "Error in decode_str -> Type error: Not STRING");
return reinterpret_cast<std::string *>(MASK(val));
}
inline bool decode_bool(void *val) {
// int tag = get_tag(val);
u64 tag = (u64)val;
if (tag == TRUE_VALUE) {
return true;
} else if (tag == FALSE_VALUE) {
return false;
}
assert_type(false, "Error in decode_bool -> Type error: Not BOOLEAN");
return false; // this line is just to satisfy the return type
}
inline void **decode_cons(void *val) {
// if (get_tag(val) != CONS)
// assert_type(false, "Error in decode_cons -> Type error: Not CONS");
return reinterpret_cast<void **>(MASK(val));
}
inline void **decode_clo(void *val) {
if (get_tag(val) != CLO)
assert_type(false, "Error in decode_clo -> Type error: Not CLO");
return reinterpret_cast<void **>(MASK(val));
}
const hamt<hash_struct, hash_struct> *decode_hash(void *val) {
if (get_tag(val) != HASH)
assert_type(false, "Error in decode_hash -> Type error: Not HASH");
return reinterpret_cast<const hamt<hash_struct, hash_struct> *>(MASK(val));
}
// Closure Allocation, alloc_clo
inline void **alloc_clo(void (*fptr)(), int num) {
// call_counter++;
void **obj = (void **)(GC_MALLOC((num + 1) * sizeof(void *)));
obj[0] = reinterpret_cast<void *>(fptr);
return obj;
}
// inline void **alloc_kont(void (*fptr)(), void *f_spec, int num) {
// inline void **alloc_kont(void (*fptr)(), void (*f_spec)(void*, void*), int
// num) {
inline void **alloc_kont(void (*f_spec)(void *, void *), int num) {
void **obj = (void **)(GC_MALLOC((num + 1) * sizeof(void *)));
// obj[0] = reinterpret_cast<void *>(fptr);
obj[0] = reinterpret_cast<void *>(f_spec);
return obj;
}
// template <typename Func> void **alloc_clo(Func fptr, int num) {
// void **obj = (void **)(GC_MALLOC((num + 1) * sizeof(void *)));
// obj[0] = nullptr;
// if (obj != nullptr) {
// obj[0] = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(fptr));
// }
// return obj;
// }
#pragma endregion
#pragma region ConsMethods
inline void *prim_cons(void *arg1, void *arg2) {
void **cell = (void **)GC_MALLOC(2 * sizeof(void *));
cell[0] = arg1;
cell[1] = arg2;
return encode_cons(cell);
}
inline void *apply_prim_cons_2(void *arg1, void *arg2) {
void **cell = (void **)GC_MALLOC(2 * sizeof(void *));
cell[0] = arg1;
cell[1] = arg2;
return encode_cons(cell);
}
// cons?
inline void *prim_cons_u63(void *lst) {
if (get_tag(lst) == CONS) {
return encode_bool(true);
}
return encode_bool(false);
}
inline void *prim_car(void *val) {
if (get_tag(val) != CONS)
assert_type(false, "Error in car -> contract violation: not a cons cell");
void **cell = decode_cons(val);
return cell[0];
}
inline void *apply_prim_car_1(void *arg1) { return prim_car(arg1); }
inline void *prim_cdr(void *val) {
if (get_tag(val) != CONS)
assert_type(false, "Error in cdr -> contract violation: not a cons cell");
void **cell = decode_cons(val);
return cell[1];
}
inline void *apply_prim_cdr_1(void *arg1) { return prim_cdr(arg1); }
// returns length of a list
inline int length_counter(void *lst) {
// if (lst == NULL_VALUE)
// return 0;
int tag = get_tag(lst);
PRINT("helloooo")
if (tag == NULL_VALUE)
return 0;
if (tag != CONS)
assert_type(false, "Error -> contact violation: expected list");
// void *val = prim_car(lst);
// void *rest = prim_cdr(lst);
// if (get_tag(val) == CONS)
// return length_counter(val) + length_counter(rest);
// else
// return 1 + length_counter(rest);
return 1 + length_counter(prim_cdr(lst));
}
inline bool is_null_val(void *val) {
u64 tag = (u64)val;
// int tag = get_tag(val);
if (tag == NULL_VALUE)
return true;
return false;
}
inline void *apply_prim_cons(void *arg) {
if (numArgs < 4 || numArgs > 4)
assert_type(
false,
"Error in cons -> arity mismatch: number of arguments should be 2!");
return prim_cons(arg_buffer[3], arg_buffer[4]);
}
inline void *apply_prim_cons_i(void *lst) {
int len_cnt = length_counter(lst);
if (len_cnt < 2 || len_cnt > 2)
assert_type(
false,
"Error in cons -> arity mismatch: number of arguments should be 2!");
void **cons_lst = decode_cons(lst);
void *car = cons_lst[0];
void *cadr = prim_car(cons_lst[1]);
return prim_cons(car, cadr);
}
inline void *apply_prim_car(void *lst) {
if (numArgs < 3 || numArgs > 3)
assert_type(
false,
"Error in car -> arity mismatch: number of arguments should be 1!");
return prim_car(arg_buffer[3]);
}
inline void *apply_prim_car_i(void *lst) {
int len_cnt = length_counter(lst);
if (len_cnt < 1 || len_cnt > 1)
assert_type(
false,
"Error in car -> arity mismatch: number of arguments should be 1!");
// because apply of car takes exp like, (list (list 10 2))
return prim_car(prim_car(lst));
}
inline void *apply_prim_cdr(void *lst) {
if (numArgs < 3 || numArgs > 3)
assert_type(
false,
"Error in cdr -> arity mismatch: number of arguments should be 1!");
return prim_cdr(arg_buffer[3]);
}
inline void *apply_prim_cdr_i(void *lst) {
int len_cnt = length_counter(lst);
if (len_cnt < 1 || len_cnt > 1)
assert_type(
false,
"Error in cdr -> arity mismatch: number of arguments should be 1!");
// because apply of cdr takes exp like, (list (list 10 2))
return prim_cdr(prim_car(lst));
}
std::string print_cons(void *lst) {
std::string ret_str;
ret_str.append("(list");
while (get_tag(lst) == CONS) {
void **cons_lst = decode_cons(lst);
ret_str.append(" ");
ret_str.append(print_val(cons_lst[0]));
int tag = get_tag(cons_lst[1]);
if (tag != CONS && tag != NULL_VALUE) {
// will have to see when this case get's true!
// something is not right here!
// if (get_tag(cons_lst[1]) != SPL) {
ret_str.append(" . ");
ret_str.append(print_val(cons_lst[1]));
//}
break;
}
lst = cons_lst[1];
}
ret_str.append(")");
return ret_str;
}
#pragma endregion
#pragma region ArithOpFunctions
inline bool is_true(void *val) {
int tag = get_tag(val);
// if (is_null_val(val)) {
// // kludgy way of doing this!
// return true;
// } else if (get_tag(val) == CONS) {
// return true;
// }
if (tag == NULL_VALUE || tag == CONS)
return true;
return decode_bool(val);
}
#pragma region EQUALITY
// this function assumes that both the args passed are mpz_t and doesn't perform
// a check tho decode_mpz when called by this function does the check.
inline void *mpz_equal(void *arg1, void *arg2) {
mpz_t *arg1_mpz = decode_mpz(arg1);
mpz_t *arg2_mpz = decode_mpz(arg2);
if (mpz_cmp(*arg1_mpz, *arg2_mpz) == 0) {
return encode_bool(true);
}
return encode_bool(false);
}
inline void *mpf_equal(void *arg1, void *arg2) {
mpf_t *arg1_mpf = decode_mpf(arg1);
mpf_t *arg2_mpf = decode_mpf(arg2);
if (mpf_cmp(*arg1_mpf, *arg2_mpf) == 0) {
return encode_bool(true);
}
return encode_bool(false);
}
// casting functions
mpf_t *mpz_2_mpf(mpz_t *val) {
mpf_t *ret_val = (mpf_t *)(GC_MALLOC(sizeof(mpf_t)));
mpf_init(*ret_val);
mpf_set_z(*ret_val, *val);
return ret_val;
}
mpz_t *mpf_2_mpz(mpf_t *val) {
mpz_t *ret_val = (mpz_t *)(GC_MALLOC(sizeof(mpz_t)));
mpz_init(*ret_val);
mpz_set_f(*ret_val, *val);
return ret_val;
}
inline void *str_equal(void *arg1, void *arg2) {
std::string *arg1_str = decode_str(arg1);
std::string *arg2_str = decode_str(arg2);
if (arg1_str->compare(*arg2_str) == 0) {
return encode_bool(true);
}
return encode_bool(false);
}
inline void *spl_equal(void *arg1, void *arg2) {
// we don't care if the values are bools are nulls, we just care about
// both the arguments have the same value, so we cast to u64 compare and
// return.
u64 arg1_u64 = (u64)arg1;
u64 arg2_u64 = (u64)arg2;
if (arg1_u64 == arg2_u64) {
return encode_bool(true);
}
return encode_bool(false);
}
inline void *cons_equal(void *arg1, void *arg2) {
// tags have to be equal or false
// if both are cons, then we fetch the car and call equal on them if not equal
// return false update to cdr and continue if not cons, call equal on the
// value and if not return false or return true
while ((get_tag(arg1) == get_tag(arg2))) {
if (is_cons(arg1) && is_cons(arg2)) {
void **cons_arg1 = decode_cons(arg1);
void **cons_arg2 = decode_cons(arg2);
// comparing the car values of two cons using the equal function
if (!decode_bool(equal_(cons_arg1[0], cons_arg2[0]))) {
return encode_bool(false);
}
arg1 = cons_arg1[1];
arg2 = cons_arg2[1];
} else {
if (decode_bool(equal_(arg1, arg2))) {
return encode_bool(true);
}
return encode_bool(false);
}
}
return encode_bool(false);
}
inline void *hash_equal(void *arg1, void *arg2) {
const hamt<hash_struct, hash_struct> *h_arg1 = decode_hash(arg1);
const hamt<hash_struct, hash_struct> *h_arg2 = decode_hash(arg2);
if (h_arg1->getHash() == h_arg2->getHash()) {
return encode_bool(true);
}
return encode_bool(false);
}
inline void *equal_(void *arg1, void *arg2) {
// takes in two voids,
// checks if they have the same tag, if not return false else
// switches based on the tag to the appropriate function for the type
int type_arg1 = get_tag(arg1);
// checking the tags match
if (!(type_arg1 == get_tag(arg2))) {
return encode_bool(false);
}
switch (type_arg1) {
case TRUE_VALUE:
case FALSE_VALUE:
case NULL_VALUE: {
u64 arg1_u64 = (u64)arg1;
u64 arg2_u64 = (u64)arg2;
return encode_bool(arg1_u64 == arg2_u64);
break;
}
case INT: {
if (decode_int(arg1) == decode_int(arg2))
return encode_bool(true);
else
return encode_bool(false);
break;
}
case FLOAT_VAL: {
if (decode_float(arg1) == decode_float(arg2))
return encode_bool(true);
else
return encode_bool(false);
break;
}
case MPZ: {
return mpz_equal(arg1, arg2);
break;
}
case MPF: {
return mpf_equal(arg1, arg2);
break;
}
case STRING: {
return str_equal(arg1, arg2);
break;
}
case SPL: {
return spl_equal(arg1, arg2);
break;
}
case CONS: {
return cons_equal(arg1, arg2);
break;
}
case HASH: {
return hash_equal(arg1, arg2);
break;
}
default: {
return encode_bool(false);
break;
}
}
}
#pragma endregion
#pragma region HASHING
bool is_hashable(void *arg) {
int tag = get_tag(arg);
return (tag == INT) || (tag == FLOAT_VAL) || (tag == MPZ) || (tag == MPF) ||
(tag == STRING) || (tag == HASH) || (tag == CONS);
}
u64 int_hash(void *val) {
u64 h = 0xcbf29ce484222325;
int int_val = decode_int(val);
// get the pointers to array of bytes and hash them
const uint8_t *p = reinterpret_cast<const uint8_t *>(&int_val);
for (int i = 0; i < sizeof(int); i++) {
h = h ^ static_cast<u64>(p[i]);
h = h * 0x100000001b3;
}
return h;
}
u64 float_hash(void *val) {
u64 h = 0xcbf29ce484222325;
float float_val = decode_float(val);
if (float_val == 0.0f) {
float_val = 0.0f; // -0.0f and +0.0f are equal
}
// get the pointers to array of bytes and hash them
const uint8_t *p = reinterpret_cast<const uint8_t *>(&float_val);
for (int i = 0; i < sizeof(float); i++) {
h = h ^ static_cast<u64>(p[i]);
h = h * 0x100000001b3;
}
return h;
}
// these function assume the type passed is the expected one
// all the three hash function have a very similar structure
// get the the number of limbs/chars to process, and get the array pointer to
// them for loop over them to compute FNV1A and return the hash
u64 mpz_hash(void *val) {
u64 h = 0xcbf29ce484222325;
mpz_t *mpz_val = decode_mpz(val);
// the mpz_sgn is a macro in gmp that just looks at _mp_size for sign info
int is_negative =
mpz_sgn(*mpz_val); // < 0 if negative, = 0 if zero and > 0 if_positive
u64 limb_cnt = mpz_size(*mpz_val);
const mp_limb_t *limb_ptr = mpz_limbs_read(*mpz_val);
if (is_negative < 0) {
h = h ^ 0x00000000000000ff;
h = h * 0x100000001b3;
} else {
h = h ^ 0x0000000000000000; // this doesn't do anything, not sure what to
// XOR it with for change
h = h * 0x100000001b3;
}
for (u32 i = 0; i < limb_cnt; i++) {
u64 limb = limb_ptr[i];
for (u32 j = 0; j < 8; j++) {
h = h ^ ((limb >> j * 8) & 0x00000000000000ff);
h = h * 0x100000001b3;
}
}
return h;
}
u64 mpf_hash(void *val) {
u64 h = 0xcbf29ce484222325;
mpf_t *mpf_val = decode_mpf(val);
u64 limb_cnt = mpf_size(*mpf_val);
const mp_limb_t *limb_ptr = (*mpf_val)->_mp_d;
int is_negative = mpf_sgn(*mpf_val);
if (is_negative < 0) {
h = h ^ 0x00000000000000ff;
h = h * 0x100000001b3;
} else {
h = h ^ 0x0000000000000000; // this doesn't do anything, not sure what to
// XOR it with for change
h = h * 0x100000001b3;
}
for (u32 i = 0; i < limb_cnt; i++) {
u64 limb = limb_ptr[i];
for (u32 j = 0; j < 8; j++) {
h = h ^ ((limb >> j * 8) & 0x00000000000000ff);
h = h * 0x100000001b3;
}
}
return h;
}
u64 str_hash(void *val) {
std::string *str = decode_str(val);
const u8 *data = reinterpret_cast<const u8 *>(str->data());
int length = str->length();
u64 h = 0xcbf29ce484222325;
for (u32 i = 0; i < length; ++i && ++data) {
h = h ^ *data;
h = h * 0x100000001b3;
}
return h;
}
u64 hamt_hash(void *h) {
const hamt<hash_struct, hash_struct> *h_hamt = decode_hash(h);
return h_hamt->getHash();
}
u64 cons_hash(void *lst) {
assert_type((get_tag(lst) == CONS),
"Error in cons_hash -> Type passed to cons_hash is not a CONS!");
u64 *h = (u64 *)GC_MALLOC(sizeof(u64));
*h = 0xcbf29ce484222325;
while (is_cons(lst)) {
void **cons_lst = decode_cons(lst);
bool type_check = is_hashable(cons_lst[0]);
assert_type(type_check,
"Error in cons_hash -> values in the list are not hashable!");
*h ^= hash_(cons_lst[0]) + 0x9e3779b9 + (*h << 6) + (*h >> 2);
lst = cons_lst[1];
}
return *h;
}
u64 hash_(void *val) {
switch (get_tag(val)) {
case INT: {
// PRINT(print_val(val));
return int_hash(val);
break;
}
case FLOAT_VAL: {
return float_hash(val);
break;
}
case MPZ: {
return mpz_hash(val);
break;
}
case MPF: {
return mpf_hash(val);
break;
}
case STRING: {
return str_hash(val);
break;
}
case HASH: {
return hamt_hash(val);
break;
}
case CONS: {
return cons_hash(val);
break;
}
default: {
assert_type(false, "Error in hash_ -> type passed cannot be hashed!");
}
}
return 0;
}
#pragma endregion
// Function to check if val is an integer
inline int is_integer_val(void *val) {
int tag = get_tag(val);
if (tag == INT || tag == MPZ) {
return true;
} else if (tag == FLOAT_VAL) {
float temp_val = decode_float(val);
float flr = std::floor(temp_val);
return flr == temp_val;
} else if (tag == MPF) {
mpf_t *fval = decode_mpf(val);
mpf_t flr;
mpf_init(flr);
mpf_floor(flr, *fval);
int result = mpf_cmp(*fval, flr) == 0;
mpf_clear(flr);
return result;
}
return false;
}
// helper function to calculate the modulo
mpz_t *calc_modulo(mpz_t *first, mpz_t *second) {
mpz_t *result = (mpz_t *)(GC_MALLOC(sizeof(mpz_t)));
mpz_init(*result);
// if (mpz_sgn(*mpz_arg1) >= 0 && mpz_sgn(*second) > 0)
if (mpz_sgn(*second) > 0) {
// both positive
mpz_mod(*result, *first, *second);
} else if (mpz_sgn(*first) >= 0 && mpz_sgn(*second) < 0) {
// positive, negative
mpz_mod(*result, *first, *second);
mpz_add(*result, *result, *second);
}
// else if (mpz_sgn(*first)) < 0 && mpz_sgn(*second) > 0)
// {
// // negative, positive
// mpz_mod(*result, *first), *second);
// }
else {
// both negative
mpz_mod(*result, *first, *second);
mpz_add(*result, *result, *second);
}
return result;
}
inline void *prim_modulo(void *first, void *second) {
void *result = nullptr;
int first_tag = get_tag(first);
int second_tag = get_tag(second);
// Handling INT + INT case directly
if (first_tag == INT && second_tag == INT) {
const int a1 = decode_int(first);
const int a2 = decode_int(second);
if (a2 == 0)
assert_type(false, "Error in modulo -> division by zero !");
int res = a1 % a2;
return reinterpret_cast<void *>(encode_int(static_cast<s32>(res)));
} else if (first_tag == FLOAT_VAL && second_tag == FLOAT_VAL) {
float f1 = decode_float(first);
float f2 = decode_float(second);
if (f2 == 0.0f)
assert_type(false, "Error in modulo -> division by zero !");
float f_res = std::fmod(f1, f2);
// Check if result is within float's precision and range
if (is_within_float_precision(f_res)) {
return reinterpret_cast<void *>(encode_float(f_res));
} else {
// Promote to MPF if the result exceeds float's precision or range
PRINT("prim modulo: promote to MPF");
// mpf_t *mpf_res = float_to_mpf(f_res);
// return encode_mpf(mpf_res);
}
} else if (get_tag(first) == MPZ &&
get_tag(second) == MPZ) { // both numbers are mpz
mpz_t *mpz_arg1 = decode_mpz(first);
mpz_t *mpz_arg2 = decode_mpz(second);
return encode_mpz(calc_modulo(mpz_arg1, mpz_arg2));
} else if (get_tag(first) == MPF &&
get_tag(second) == MPF) { // both number are mpf
mpz_t *mpz_arg1 = mpf_2_mpz(decode_mpf(first));
mpz_t *mpz_arg2 = mpf_2_mpz(decode_mpf(second));
return encode_mpf(mpz_2_mpf(calc_modulo(mpz_arg1, mpz_arg2)));
} else if (get_tag(first) == MPZ &&
get_tag(second) == MPF) { // first number is mpz but second is mpf
mpz_t *mpz_arg2 = mpf_2_mpz(decode_mpf(second));
return encode_mpf(mpz_2_mpf(calc_modulo(decode_mpz(first), mpz_arg2)));
} else { // second number is mpz but but first is mpf
mpz_t *mpz_arg1 = mpf_2_mpz(decode_mpf(first));
return encode_mpf(mpz_2_mpf(calc_modulo(mpz_arg1, decode_mpz(second))));
}
return result;
}
inline void *apply_prim_modulo(void *lst) {
if (numArgs < 4 || numArgs > 4)
assert_type(
false,
"Error in modulo -> arity mismatch: expected number of argument is 2!");
void * first = arg_buffer[3];
void * second = arg_buffer[4];
if (!is_integer_val(first) || !is_integer_val(second)) {
assert_type(
false,
"Error in modulo: contract violation -> expected integer arguments!");
}
return prim_modulo(first, second);
}
inline void *apply_prim_modulo_2(void *a, void *b) {
if (!is_integer_val(a) || !is_integer_val(b)) {
assert_type(
false,
"Error in modulo: contract violation -> expected integer arguments!");
}
return prim_modulo(a, b);
}
inline void *prim_equal_u63(void *arg1, void *arg2) {
return equal_(arg1, arg2);
}
inline void *apply_prim_equal_u63_2(void *x, void *y) {
return prim_equal_u63(x, y);
}
inline void *apply_prim_equal_u63(void *lst) {
if (numArgs < 4 || numArgs > 4)
assert_type(
false,
"Error in equal? -> arity mismatch: expected number of argument is 2.");
void * first = arg_buffer[3];
void * second = arg_buffer[4];
return prim_equal_u63(first, second);
}
inline void *prim_eq_u63(void *arg1, void *arg2) {
if (is_cons(arg1) || is_cons(arg2))
return encode_bool(false);
return equal_(arg1, arg2);
}
inline void *apply_prim_eq_u63_2(void *x, void *y) { return prim_eq_u63(x, y); }
inline void *apply_prim_eq_u63(void *lst) {
if (length_counter(lst) > 2)
assert_type(
false,
"Error in eq? -> arity mismatch: expected number of argument is 2.");
void **cons_lst = decode_cons(lst);
void *car = cons_lst[0];
void *cadr = prim_car(cons_lst[1]);
if (is_cons(car) || is_cons(cadr))
return encode_bool(false);
return prim_eq_u63(car, cadr);
}
// null?
inline void *prim_null_u63(void *item) {
if (get_tag(item) == NULL_VALUE) {
return encode_bool(true);
}
return encode_bool(false);
}
inline void *apply_prim_null_u63(void *lst) {
if (numArgs < 3 || numArgs > 3)
assert_type(
false,
"Error in null? -> arity mismatch: expected number of argument is 1.");
return prim_null_u63(arg_buffer[3]);