-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathlean.h
3030 lines (2529 loc) · 113 KB
/
lean.h
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
/*
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#ifdef __cplusplus
#include <atomic>
#include <stdlib.h>
#define _Atomic(t) std::atomic<t>
#define LEAN_USING_STD using namespace std; /* NOLINT */
extern "C" {
#else
#define LEAN_USING_STD
#endif
#include <lean/config.h>
#define LEAN_CLOSURE_MAX_ARGS 16
#define LEAN_OBJECT_SIZE_DELTA 8
#define LEAN_MAX_SMALL_OBJECT_SIZE 4096
#ifdef _MSC_VER
#define LEAN_ALLOCA(s) _alloca(s)
#include <stdnoreturn.h>
#define LEAN_NORETURN _Noreturn
#else
#define LEAN_ALLOCA(s) alloca(s)
#define LEAN_NORETURN __attribute__((noreturn))
#endif
#if defined(__GNUC__) || defined(__clang__)
#define LEAN_UNLIKELY(x) (__builtin_expect((x), 0))
#define LEAN_LIKELY(x) (__builtin_expect((x), 1))
#ifdef NDEBUG
#define LEAN_ALWAYS_INLINE __attribute__((always_inline))
#else
// We have observed stack frame increases from forced inlining overflowing the stack in debug builds,
// let's leave the decision to the compiler in that case
#define LEAN_ALWAYS_INLINE
#endif
#else
#define LEAN_UNLIKELY(x) (x)
#define LEAN_LIKELY(x) (x)
#define LEAN_ALWAYS_INLINE
#endif
#ifndef assert
#ifdef NDEBUG
#define assert(expr)
#else
void lean_notify_assert(const char * fileName, int line, const char * condition);
#define assert(expr) { if (LEAN_UNLIKELY(!(expr))) lean_notify_assert(__FILE__, __LINE__, #expr); }
#endif
#endif
// We set `LEAN_EXPORTING` when compiling objects of libleanshared, but not when including this header in any other context.
#ifdef LEAN_EXPORTING
#ifdef _WIN32
#define LEAN_EXPORT __declspec(dllexport)
#else
#define LEAN_EXPORT __attribute__((visibility("default")))
#endif
#else
#define LEAN_EXPORT
#endif
#define LEAN_BYTE(Var, Index) *(((uint8_t*)&Var)+Index)
#define LeanMaxCtorTag 243
#define LeanPromise 244
#define LeanClosure 245
#define LeanArray 246
#define LeanStructArray 247
#define LeanScalarArray 248
#define LeanString 249
#define LeanMPZ 250
#define LeanThunk 251
#define LeanTask 252
#define LeanRef 253
#define LeanExternal 254
#define LeanReserved 255
#define LEAN_MAX_CTOR_FIELDS 256
#define LEAN_MAX_CTOR_SCALARS_SIZE 1024
static inline bool lean_is_big_object_tag(uint8_t tag) {
return tag == LeanArray || tag == LeanStructArray || tag == LeanScalarArray || tag == LeanString;
}
#define LEAN_CASSERT(predicate) LEAN_impl_CASSERT_LINE(predicate, __LINE__, __FILE__)
#define LEAN_impl_PASTE(a, b) a##b
#define LEAN_impl_CASSERT_LINE(predicate, line, file) \
typedef char LEAN_impl_PASTE(assertion_failed_##file##_, line)[2*!!(predicate)-1];
LEAN_CASSERT(sizeof(size_t) == sizeof(void*));
/*
Lean object header.
The reference counter `m_rc` field also encodes whether the object is single threaded (> 0), multi threaded (< 0), or
reference counting is not needed (== 0). We don't use reference counting for objects stored in compact regions, or
marked as persistent.
For "small" objects stored in compact regions, the field `m_cs_sz` contains the object size. For "small" objects not
stored in compact regions, we use the page information to retrieve its size.
During deallocation and 64-bit machines, the fields `m_rc` and `m_cs_sz` store the next object in the deletion TODO list.
These two fields together have 48-bits, and this is enough for modern computers.
In 32-bit machines, the field `m_rc` is sufficient.
The field `m_other` is used to store the number of fields in a constructor object and the element size in a scalar array.
*/
typedef struct {
int m_rc;
unsigned m_cs_sz:16;
unsigned m_other:8;
unsigned m_tag:8;
} lean_object;
/*
In our runtime, a Lean function consume the reference counter (RC) of its argument or not.
We say this behavior is part of the "calling convention" for the function. We say an argument uses:
x
1- "standard" calling convention if it consumes/decrements the RC.
In this calling convention each argument should be viewed as a resource that is consumed by the function.
This is roughly equivalent to `S && a` in C++, where `S` is a smart pointer, and `a` is the argument.
When this calling convention is used for an argument `x`, then it is safe to perform destructive updates to
`x` if its RC is 1.
2- "borrowed" calling convention if it doesn't consume/decrement the RC, and it is the responsibility of the caller
to decrement the RC.
This is roughly equivalent to `S const & a` in C++, where `S` is a smart pointer, and `a` is the argument.
For returning objects, we also have two conventions
1- "standard" result. The caller is responsible for consuming the RC of the result.
This is roughly equivalent to returning a smart point `S` by value in C++.
2- "borrowed" result. The caller is not responsible for decreasing the RC.
This is roughly equivalent to returning a smart point reference `S const &` in C++.
Functions stored in closures use the "standard" calling convention.
*/
/* The following typedef's are used to document the calling convention for the primitives. */
typedef lean_object * lean_obj_arg; /* Standard object argument. */
typedef lean_object * b_lean_obj_arg; /* Borrowed object argument. */
typedef lean_object * u_lean_obj_arg; /* Unique (aka non shared) object argument. */
typedef lean_object * lean_obj_res; /* Standard object result. */
typedef lean_object * b_lean_obj_res; /* Borrowed object result. */
typedef struct {
lean_object m_header;
lean_object * m_objs[];
} lean_ctor_object;
/* Array arrays */
typedef struct {
lean_object m_header;
size_t m_size;
size_t m_capacity;
lean_object * m_data[];
} lean_array_object;
/* Scalar arrays */
typedef struct {
lean_object m_header;
size_t m_size;
size_t m_capacity;
uint8_t m_data[];
} lean_sarray_object;
typedef struct {
lean_object m_header;
size_t m_size; /* byte length including '\0' terminator */
size_t m_capacity;
size_t m_length; /* UTF8 length */
char m_data[];
} lean_string_object;
typedef struct {
lean_object m_header;
void * m_fun;
uint16_t m_arity; /* Number of arguments expected by m_fun. */
uint16_t m_num_fixed; /* Number of arguments that have been already fixed. */
lean_object * m_objs[];
} lean_closure_object;
typedef struct {
lean_object m_header;
lean_object * m_value;
} lean_ref_object;
typedef struct {
lean_object m_header;
_Atomic(lean_object *) m_value;
_Atomic(lean_object *) m_closure;
} lean_thunk_object;
struct lean_task;
/* Data required for executing a Lean task. It is released as soon as
the task terminates even if the task object itself is still referenced. */
typedef struct {
lean_object * m_closure;
struct lean_task * m_head_dep;
struct lean_task * m_next_dep;
unsigned m_prio;
uint8_t m_canceled;
// If true, task will not be freed until finished
uint8_t m_keep_alive;
uint8_t m_deleted;
} lean_task_imp;
/* Object of type `Task _`. The lifetime of a `lean_task` object can be represented as a state machine with atomic
state transitions.
In the following, `condition` describes a predicate uniquely identifying a state.
creation:
* Task.spawn ==> Queued
* Task.map/bind ==> Waiting
* Task.pure ==> Finished
* Promise.new ==> Promised
states:
* Queued
* condition: in task_manager::m_queues && m_imp != nullptr && !m_imp->m_deleted
* invariant: m_value == nullptr
* transition: RC becomes 0 ==> Deactivated (`deactivate_task` lock)
* transition: dequeued by worker thread ==> Running (`spawn_worker` lock)
* Waiting
* condition: reachable from task via `m_head_dep->m_next_dep->...` && !m_imp->m_deleted
* invariant: m_imp != nullptr && m_value == nullptr
* invariant: task dependency is Queued/Waiting/Running
* It cannot become Deactivated because this task should be holding an owned reference to it
* transition: RC becomes 0 ==> Deactivated (`deactivate_task` lock)
* transition: task dependency Finished ==> Queued (`handle_finished` under `spawn_worker` lock)
* Promised
* condition: obtained as result from promise
* invariant: m_imp != nullptr && m_value == nullptr
* transition: promise resolved ==> Finished (`resolve_core` under `spawn_worker` lock)
* transition: RC becomes 0 ==> Deactivated (`deactivate_task` lock)
* Running
* condition: m_imp != nullptr && m_imp->m_closure == nullptr
* The worker takes ownership of the closure when running it
* invariant: m_value == nullptr
* transition: RC becomes 0 ==> Deactivated (`deactivate_task` lock)
* transition: finished execution ==> Finished (`spawn_worker` lock)
* Deactivated
* condition: m_imp != nullptr && m_imp->m_deleted
* invariant: RC == 0
* invariant: m_imp->m_closure == nullptr && m_imp->m_head_dep == nullptr (both freed by `deactivate_task_core`)
* Note that all dependent tasks must have already been Deactivated by the converse of the second Waiting invariant
* invariant: m_value == nullptr
* transition: dequeued by worker thread ==> freed
* transition: finished execution ==> freed
* transition: task dependency Finished ==> freed
* We must keep the task object alive until one of these transitions because in either case, we have live
(internal, unowned) references to the task up to that point
* transition: task dependency Deactivated ==> freed
* Finished
* condition: m_value != nullptr
* invariant: m_imp == nullptr
* transition: RC becomes 0 ==> freed (`deactivate_task` lock) */
typedef struct lean_task {
lean_object m_header;
_Atomic(lean_object *) m_value;
lean_task_imp * m_imp;
} lean_task_object;
typedef struct lean_promise {
lean_object m_header;
lean_task_object * m_result;
} lean_promise_object;
typedef void (*lean_external_finalize_proc)(void *);
typedef void (*lean_external_foreach_proc)(void *, b_lean_obj_arg);
typedef struct {
lean_external_finalize_proc m_finalize;
lean_external_foreach_proc m_foreach;
} lean_external_class;
LEAN_EXPORT lean_external_class * lean_register_external_class(lean_external_finalize_proc, lean_external_foreach_proc);
/* Object for wrapping external data. */
typedef struct {
lean_object m_header;
lean_external_class * m_class;
void * m_data;
} lean_external_object;
static inline LEAN_ALWAYS_INLINE bool lean_is_scalar(lean_object * o) { return ((size_t)(o) & 1) == 1; }
static inline lean_object * lean_box(size_t n) { return (lean_object*)(((size_t)(n) << 1) | 1); }
static inline size_t lean_unbox(lean_object * o) { return (size_t)(o) >> 1; }
LEAN_EXPORT void lean_set_exit_on_panic(bool flag);
/* Enable/disable panic messages */
LEAN_EXPORT void lean_set_panic_messages(bool flag);
LEAN_EXPORT void lean_panic(char const * msg, bool force_stderr);
LEAN_EXPORT lean_object * lean_panic_fn(lean_object * default_val, lean_object * msg);
LEAN_EXPORT LEAN_NORETURN void lean_internal_panic(char const * msg);
LEAN_EXPORT LEAN_NORETURN void lean_internal_panic_out_of_memory(void);
LEAN_EXPORT LEAN_NORETURN void lean_internal_panic_unreachable(void);
LEAN_EXPORT LEAN_NORETURN void lean_internal_panic_rc_overflow(void);
static inline size_t lean_align(size_t v, size_t a) {
return (v / a)*a + a * (v % a != 0);
}
static inline unsigned lean_get_slot_idx(unsigned sz) {
assert(sz > 0);
assert(lean_align(sz, LEAN_OBJECT_SIZE_DELTA) == sz);
return sz / LEAN_OBJECT_SIZE_DELTA - 1;
}
LEAN_EXPORT void * lean_alloc_small(unsigned sz, unsigned slot_idx);
LEAN_EXPORT void lean_free_small(void * p);
LEAN_EXPORT unsigned lean_small_mem_size(void * p);
LEAN_EXPORT void lean_inc_heartbeat(void);
#ifndef __cplusplus
void * malloc(size_t); // avoid including big `stdlib.h`
#endif
static inline lean_object * lean_alloc_small_object(unsigned sz) {
#ifdef LEAN_SMALL_ALLOCATOR
sz = lean_align(sz, LEAN_OBJECT_SIZE_DELTA);
unsigned slot_idx = lean_get_slot_idx(sz);
assert(sz <= LEAN_MAX_SMALL_OBJECT_SIZE);
return (lean_object*)lean_alloc_small(sz, slot_idx);
#else
lean_inc_heartbeat();
void * mem = malloc(sizeof(size_t) + sz);
if (mem == 0) lean_internal_panic_out_of_memory();
*(size_t*)mem = sz;
return (lean_object*)((size_t*)mem + 1);
#endif
}
static inline lean_object * lean_alloc_ctor_memory(unsigned sz) {
#ifdef LEAN_SMALL_ALLOCATOR
unsigned sz1 = lean_align(sz, LEAN_OBJECT_SIZE_DELTA);
unsigned slot_idx = lean_get_slot_idx(sz1);
assert(sz1 <= LEAN_MAX_SMALL_OBJECT_SIZE);
lean_object* r = (lean_object*)lean_alloc_small(sz1, slot_idx);
if (sz1 > sz) {
/* Initialize last word.
In our runtime `lean_object_byte_size` is always
a multiple of the machine word size for constructors.
By setting the last word to 0, we make sure the sharing
maximizer procedures at `maxsharing.cpp` and `compact.cpp` are
not affected by uninitialized data at the (sz1 - sz) last bytes.
Otherwise, we may mistakenly assume to structurally equal
objects are not identical because of this uninitialized memory. */
size_t * end = (size_t*)(((char*)r) + sz1);
end[-1] = 0;
}
return r;
#else
return lean_alloc_small_object(sz);
#endif
}
static inline unsigned lean_small_object_size(lean_object * o) {
#ifdef LEAN_SMALL_ALLOCATOR
return lean_small_mem_size(o);
#else
return *((size_t*)o - 1);
#endif
}
#ifndef __cplusplus
void free(void *); // avoid including big `stdlib.h`
#endif
#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
void free_sized(void* ptr, size_t);
#endif
static inline void lean_free_small_object(lean_object * o) {
#ifdef LEAN_SMALL_ALLOCATOR
lean_free_small(o);
#else
size_t* ptr = (size_t*)o - 1;
free_sized(ptr, *ptr + sizeof(size_t));
#endif
}
LEAN_EXPORT lean_object * lean_alloc_object(size_t sz);
LEAN_EXPORT void lean_free_object(lean_object * o);
static inline uint8_t lean_ptr_tag(lean_object * o) {
return o->m_tag;
}
static inline unsigned lean_ptr_other(lean_object * o) {
return o->m_other;
}
/* The object size may be slightly bigger for constructor objects.
The runtime does not track the size of the scalar size area.
All constructor objects are "small", and allocated into pages.
We retrieve their size by accessing the page header. The size of
small objects is a multiple of LEAN_OBJECT_SIZE_DELTA */
LEAN_EXPORT size_t lean_object_byte_size(lean_object * o);
/* Returns the size of the salient part of an object's storage,
i.e. the parts that contribute to the value representation;
padding or unused capacity is excluded. Operations that read
from an object's storage must only access these parts, since
the non-salient parts may not be initialized. */
LEAN_EXPORT size_t lean_object_data_byte_size(lean_object * o);
static inline bool lean_is_mt(lean_object * o) {
return o->m_rc < 0;
}
static inline bool lean_is_st(lean_object * o) {
return o->m_rc > 0;
}
/* We never update the reference counter of objects stored in compact regions and allocated at initialization time. */
static inline bool lean_is_persistent(lean_object * o) {
return o->m_rc == 0;
}
static inline bool lean_has_rc(lean_object * o) {
return o->m_rc != 0;
}
static inline _Atomic(int) * lean_get_rc_mt_addr(lean_object* o) {
return (_Atomic(int)*)(&(o->m_rc));
}
LEAN_EXPORT void lean_inc_ref_cold(lean_object * o);
LEAN_EXPORT void lean_inc_ref_n_cold(lean_object * o, unsigned n);
static inline void lean_inc_ref(lean_object * o) {
if (LEAN_LIKELY(lean_is_st(o))) {
o->m_rc++;
} else if (o->m_rc != 0) {
lean_inc_ref_cold(o);
}
}
static inline void lean_inc_ref_n(lean_object * o, size_t n) {
if (LEAN_LIKELY(lean_is_st(o))) {
o->m_rc += n;
} else if (o->m_rc != 0) {
lean_inc_ref_n_cold(o, n);
}
}
LEAN_EXPORT void lean_dec_ref_cold(lean_object * o);
static inline LEAN_ALWAYS_INLINE void lean_dec_ref(lean_object * o) {
if (LEAN_LIKELY(o->m_rc > 1)) {
o->m_rc--;
} else if (o->m_rc != 0) {
lean_dec_ref_cold(o);
}
}
static inline void LEAN_ALWAYS_INLINE lean_inc(lean_object * o) { if (!lean_is_scalar(o)) lean_inc_ref(o); }
static inline void lean_inc_n(lean_object * o, size_t n) { if (!lean_is_scalar(o)) lean_inc_ref_n(o, n); }
static inline void LEAN_ALWAYS_INLINE lean_dec(lean_object * o) { if (!lean_is_scalar(o)) lean_dec_ref(o); }
static inline bool lean_is_ctor(lean_object * o) { return lean_ptr_tag(o) <= LeanMaxCtorTag; }
static inline bool lean_is_closure(lean_object * o) { return lean_ptr_tag(o) == LeanClosure; }
static inline bool lean_is_array(lean_object * o) { return lean_ptr_tag(o) == LeanArray; }
static inline bool lean_is_sarray(lean_object * o) { return lean_ptr_tag(o) == LeanScalarArray; }
static inline bool lean_is_string(lean_object * o) { return lean_ptr_tag(o) == LeanString; }
static inline bool lean_is_mpz(lean_object * o) { return lean_ptr_tag(o) == LeanMPZ; }
static inline bool lean_is_thunk(lean_object * o) { return lean_ptr_tag(o) == LeanThunk; }
static inline bool lean_is_task(lean_object * o) { return lean_ptr_tag(o) == LeanTask; }
static inline bool lean_is_promise(lean_object * o) { return lean_ptr_tag(o) == LeanPromise; }
static inline bool lean_is_external(lean_object * o) { return lean_ptr_tag(o) == LeanExternal; }
static inline bool lean_is_ref(lean_object * o) { return lean_ptr_tag(o) == LeanRef; }
static inline unsigned lean_obj_tag(lean_object * o) {
if (lean_is_scalar(o)) return lean_unbox(o); else return lean_ptr_tag(o);
}
static inline lean_ctor_object * lean_to_ctor(lean_object * o) { assert(lean_is_ctor(o)); return (lean_ctor_object*)(o); }
static inline lean_closure_object * lean_to_closure(lean_object * o) { assert(lean_is_closure(o)); return (lean_closure_object*)(o); }
static inline lean_array_object * lean_to_array(lean_object * o) { assert(lean_is_array(o)); return (lean_array_object*)(o); }
static inline lean_sarray_object * lean_to_sarray(lean_object * o) { assert(lean_is_sarray(o)); return (lean_sarray_object*)(o); }
static inline lean_string_object * lean_to_string(lean_object * o) { assert(lean_is_string(o)); return (lean_string_object*)(o); }
static inline lean_thunk_object * lean_to_thunk(lean_object * o) { assert(lean_is_thunk(o)); return (lean_thunk_object*)(o); }
static inline lean_task_object * lean_to_task(lean_object * o) { assert(lean_is_task(o)); return (lean_task_object*)(o); }
static inline lean_promise_object * lean_to_promise(lean_object * o) { assert(lean_is_promise(o)); return (lean_promise_object*)(o); }
static inline lean_ref_object * lean_to_ref(lean_object * o) { assert(lean_is_ref(o)); return (lean_ref_object*)(o); }
static inline lean_external_object * lean_to_external(lean_object * o) { assert(lean_is_external(o)); return (lean_external_object*)(o); }
static inline bool lean_is_exclusive(lean_object * o) {
if (LEAN_LIKELY(lean_is_st(o))) {
return o->m_rc == 1;
} else {
return false;
}
}
static inline uint8_t lean_is_exclusive_obj(lean_object * o) {
return lean_is_exclusive(o);
}
static inline bool lean_is_shared(lean_object * o) {
if (LEAN_LIKELY(lean_is_st(o))) {
return o->m_rc > 1;
} else {
return false;
}
}
LEAN_EXPORT void lean_mark_mt(lean_object * o);
LEAN_EXPORT void lean_mark_persistent(lean_object * o);
static inline void lean_set_st_header(lean_object * o, unsigned tag, unsigned other) {
o->m_rc = 1;
o->m_tag = tag;
o->m_other = other;
o->m_cs_sz = 0;
}
/* Remark: we don't need a reference counter for objects that are not stored in the heap.
Thus, we use the area to store the object size for small objects. */
static inline void lean_set_non_heap_header(lean_object * o, size_t sz, unsigned tag, unsigned other) {
assert(sz > 0);
assert(sz < (1ull << 16));
assert(sz == 1 || !lean_is_big_object_tag(tag));
o->m_rc = 0;
o->m_tag = tag;
o->m_other = other;
o->m_cs_sz = sz;
}
/* `lean_set_non_heap_header` for (potentially) big objects such as arrays and strings. */
static inline void lean_set_non_heap_header_for_big(lean_object * o, unsigned tag, unsigned other) {
lean_set_non_heap_header(o, 1, tag, other);
}
/* Constructor objects */
static inline unsigned lean_ctor_num_objs(lean_object * o) {
assert(lean_is_ctor(o));
return lean_ptr_other(o);
}
static inline lean_object ** lean_ctor_obj_cptr(lean_object * o) {
assert(lean_is_ctor(o));
return lean_to_ctor(o)->m_objs;
}
static inline uint8_t * lean_ctor_scalar_cptr(lean_object * o) {
assert(lean_is_ctor(o));
return (uint8_t*)(lean_ctor_obj_cptr(o) + lean_ctor_num_objs(o));
}
static inline lean_object * lean_alloc_ctor(unsigned tag, unsigned num_objs, unsigned scalar_sz) {
assert(tag <= LeanMaxCtorTag && num_objs < LEAN_MAX_CTOR_FIELDS && scalar_sz < LEAN_MAX_CTOR_SCALARS_SIZE);
lean_object * o = lean_alloc_ctor_memory(sizeof(lean_ctor_object) + sizeof(void*)*num_objs + scalar_sz);
lean_set_st_header(o, tag, num_objs);
return o;
}
static inline b_lean_obj_res lean_ctor_get(b_lean_obj_arg o, unsigned i) {
assert(i < lean_ctor_num_objs(o));
return lean_ctor_obj_cptr(o)[i];
}
static inline void lean_ctor_set(b_lean_obj_arg o, unsigned i, lean_obj_arg v) {
assert(i < lean_ctor_num_objs(o));
lean_ctor_obj_cptr(o)[i] = v;
}
static inline void lean_ctor_set_tag(b_lean_obj_arg o, uint8_t new_tag) {
assert(new_tag <= LeanMaxCtorTag);
o->m_tag = new_tag;
}
static inline void lean_ctor_release(b_lean_obj_arg o, unsigned i) {
assert(i < lean_ctor_num_objs(o));
lean_object ** objs = lean_ctor_obj_cptr(o);
lean_dec(objs[i]);
objs[i] = lean_box(0);
}
static inline size_t lean_ctor_get_usize(b_lean_obj_arg o, unsigned i) {
assert(i >= lean_ctor_num_objs(o));
return *((size_t*)(lean_ctor_obj_cptr(o) + i));
}
static inline uint8_t lean_ctor_get_uint8(b_lean_obj_arg o, unsigned offset) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
return *((uint8_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset));
}
static inline uint16_t lean_ctor_get_uint16(b_lean_obj_arg o, unsigned offset) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
return *((uint16_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset));
}
static inline uint32_t lean_ctor_get_uint32(b_lean_obj_arg o, unsigned offset) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
return *((uint32_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset));
}
static inline uint64_t lean_ctor_get_uint64(b_lean_obj_arg o, unsigned offset) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
return *((uint64_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset));
}
static inline double lean_ctor_get_float(b_lean_obj_arg o, unsigned offset) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
return *((double*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset));
}
static inline float lean_ctor_get_float32(b_lean_obj_arg o, unsigned offset) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
return *((float*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset));
}
static inline void lean_ctor_set_usize(b_lean_obj_arg o, unsigned i, size_t v) {
assert(i >= lean_ctor_num_objs(o));
*((size_t*)(lean_ctor_obj_cptr(o) + i)) = v;
}
static inline void lean_ctor_set_uint8(b_lean_obj_arg o, unsigned offset, uint8_t v) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
*((uint8_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset)) = v;
}
static inline void lean_ctor_set_uint16(b_lean_obj_arg o, unsigned offset, uint16_t v) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
*((uint16_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset)) = v;
}
static inline void lean_ctor_set_uint32(b_lean_obj_arg o, unsigned offset, uint32_t v) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
*((uint32_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset)) = v;
}
static inline void lean_ctor_set_uint64(b_lean_obj_arg o, unsigned offset, uint64_t v) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
*((uint64_t*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset)) = v;
}
static inline void lean_ctor_set_float(b_lean_obj_arg o, unsigned offset, double v) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
*((double*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset)) = v;
}
static inline void lean_ctor_set_float32(b_lean_obj_arg o, unsigned offset, float v) {
assert(offset >= lean_ctor_num_objs(o) * sizeof(void*));
*((float*)((uint8_t*)(lean_ctor_obj_cptr(o)) + offset)) = v;
}
/* Closures */
static inline void * lean_closure_fun(lean_object * o) { return lean_to_closure(o)->m_fun; }
static inline unsigned lean_closure_arity(lean_object * o) { return lean_to_closure(o)->m_arity; }
static inline unsigned lean_closure_num_fixed(lean_object * o) { return lean_to_closure(o)->m_num_fixed; }
static inline lean_object ** lean_closure_arg_cptr(lean_object * o) { return lean_to_closure(o)->m_objs; }
static inline lean_obj_res lean_alloc_closure(void * fun, unsigned arity, unsigned num_fixed) {
assert(arity > 0);
assert(num_fixed < arity);
lean_closure_object * o = (lean_closure_object*)lean_alloc_small_object(sizeof(lean_closure_object) + sizeof(void*)*num_fixed);
lean_set_st_header((lean_object*)o, LeanClosure, 0);
o->m_fun = fun;
o->m_arity = arity;
o->m_num_fixed = num_fixed;
return (lean_object*)o;
}
static inline b_lean_obj_res lean_closure_get(b_lean_obj_arg o, unsigned i) {
assert(i < lean_closure_num_fixed(o));
return lean_to_closure(o)->m_objs[i];
}
static inline void lean_closure_set(u_lean_obj_arg o, unsigned i, lean_obj_arg a) {
assert(i < lean_closure_num_fixed(o));
lean_to_closure(o)->m_objs[i] = a;
}
LEAN_EXPORT lean_object* lean_apply_1(lean_object* f, lean_object* a1);
LEAN_EXPORT lean_object* lean_apply_2(lean_object* f, lean_object* a1, lean_object* a2);
LEAN_EXPORT lean_object* lean_apply_3(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3);
LEAN_EXPORT lean_object* lean_apply_4(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4);
LEAN_EXPORT lean_object* lean_apply_5(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5);
LEAN_EXPORT lean_object* lean_apply_6(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6);
LEAN_EXPORT lean_object* lean_apply_7(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7);
LEAN_EXPORT lean_object* lean_apply_8(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8);
LEAN_EXPORT lean_object* lean_apply_9(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9);
LEAN_EXPORT lean_object* lean_apply_10(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9, lean_object* a10);
LEAN_EXPORT lean_object* lean_apply_11(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9, lean_object* a10, lean_object* a11);
LEAN_EXPORT lean_object* lean_apply_12(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9, lean_object* a10, lean_object* a11, lean_object* a12);
LEAN_EXPORT lean_object* lean_apply_13(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9, lean_object* a10, lean_object* a11, lean_object* a12, lean_object* a13);
LEAN_EXPORT lean_object* lean_apply_14(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9, lean_object* a10, lean_object* a11, lean_object* a12, lean_object* a13, lean_object* a14);
LEAN_EXPORT lean_object* lean_apply_15(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9, lean_object* a10, lean_object* a11, lean_object* a12, lean_object* a13, lean_object* a14, lean_object* a15);
LEAN_EXPORT lean_object* lean_apply_16(lean_object* f, lean_object* a1, lean_object* a2, lean_object* a3, lean_object* a4, lean_object* a5, lean_object* a6, lean_object* a7, lean_object* a8, lean_object* a9, lean_object* a10, lean_object* a11, lean_object* a12, lean_object* a13, lean_object* a14, lean_object* a15, lean_object* a16);
LEAN_EXPORT lean_object* lean_apply_n(lean_object* f, unsigned n, lean_object** args);
/* Pre: n > 16 */
LEAN_EXPORT lean_object* lean_apply_m(lean_object* f, unsigned n, lean_object** args);
/* Arrays of objects (low level API) */
static inline lean_obj_res lean_alloc_array(size_t size, size_t capacity) {
lean_array_object * o = (lean_array_object*)lean_alloc_object(sizeof(lean_array_object) + sizeof(void*)*capacity);
lean_set_st_header((lean_object*)o, LeanArray, 0);
o->m_size = size;
o->m_capacity = capacity;
return (lean_object*)o;
}
static inline size_t lean_array_size(b_lean_obj_arg o) { return lean_to_array(o)->m_size; }
static inline size_t lean_array_capacity(b_lean_obj_arg o) { return lean_to_array(o)->m_capacity; }
static inline size_t lean_array_byte_size(lean_object * o) {
return sizeof(lean_array_object) + sizeof(void*)*lean_array_capacity(o);
}
static inline size_t lean_array_data_byte_size(lean_object * o) {
return sizeof(lean_array_object) + sizeof(void*)*lean_array_size(o);
}
static inline lean_object ** lean_array_cptr(lean_object * o) { return lean_to_array(o)->m_data; }
static inline void lean_array_set_size(u_lean_obj_arg o, size_t sz) {
assert(lean_is_array(o));
assert(lean_is_exclusive(o));
assert(sz <= lean_array_capacity(o));
lean_to_array(o)->m_size = sz;
}
static inline b_lean_obj_res lean_array_get_core(b_lean_obj_arg o, size_t i) {
assert(i < lean_array_size(o));
return lean_to_array(o)->m_data[i];
}
static inline void lean_array_set_core(u_lean_obj_arg o, size_t i, lean_obj_arg v) {
/* Remark: we use this procedure to update non shared arrays in the heap,
and when copying objects to compact region at compact.cpp */
assert(!lean_has_rc(o) || lean_is_exclusive(o));
assert(i < lean_array_size(o));
lean_to_array(o)->m_data[i] = v;
}
LEAN_EXPORT lean_object * lean_array_mk(lean_obj_arg l);
LEAN_EXPORT lean_object * lean_array_to_list(lean_obj_arg a);
/* Arrays of objects (high level API) */
static inline lean_object * lean_array_sz(lean_obj_arg a) {
lean_object * r = lean_box(lean_array_size(a));
lean_dec(a);
return r;
}
static inline lean_object * lean_array_get_size(b_lean_obj_arg a) {
return lean_box(lean_array_size(a));
}
static inline lean_object * lean_mk_empty_array() {
return lean_alloc_array(0, 0);
}
static inline lean_object * lean_mk_empty_array_with_capacity(b_lean_obj_arg capacity) {
if (!lean_is_scalar(capacity)) lean_internal_panic_out_of_memory();
return lean_alloc_array(0, lean_unbox(capacity));
}
static inline lean_object * lean_array_uget(b_lean_obj_arg a, size_t i) {
lean_object * r = lean_array_get_core(a, i); lean_inc(r);
return r;
}
static inline lean_obj_res lean_array_fget(b_lean_obj_arg a, b_lean_obj_arg i) {
return lean_array_uget(a, lean_unbox(i));
}
LEAN_EXPORT lean_obj_res lean_array_get_panic(lean_obj_arg def_val);
static inline lean_object * lean_array_get(lean_obj_arg def_val, b_lean_obj_arg a, b_lean_obj_arg i) {
if (lean_is_scalar(i)) {
size_t idx = lean_unbox(i);
if (idx < lean_array_size(a)) {
lean_dec(def_val);
return lean_array_uget(a, idx);
}
}
/* Recall that if `i` is not a scalar, then it must be out of bounds because
i > LEAN_MAX_SMALL_NAT == MAX_UNSIGNED >> 1
but each array entry is 8 bytes in 64-bit machines and 4 in 32-bit ones.
In both cases, we would be out-of-memory. */
return lean_array_get_panic(def_val);
}
LEAN_EXPORT lean_obj_res lean_copy_expand_array(lean_obj_arg a, bool expand);
static inline lean_obj_res lean_copy_array(lean_obj_arg a) {
return lean_copy_expand_array(a, false);
}
static inline lean_obj_res lean_ensure_exclusive_array(lean_obj_arg a) {
if (lean_is_exclusive(a)) return a;
return lean_copy_array(a);
}
static inline lean_object * lean_array_uset(lean_obj_arg a, size_t i, lean_obj_arg v) {
lean_object * r = lean_ensure_exclusive_array(a);
lean_object ** it = lean_array_cptr(r) + i;
lean_dec(*it);
*it = v;
return r;
}
static inline lean_object * lean_array_fset(lean_obj_arg a, b_lean_obj_arg i, lean_obj_arg v) {
return lean_array_uset(a, lean_unbox(i), v);
}
LEAN_EXPORT lean_obj_res lean_array_set_panic(lean_obj_arg a, lean_obj_arg v);
static inline lean_object * lean_array_set(lean_obj_arg a, b_lean_obj_arg i, lean_obj_arg v) {
if (lean_is_scalar(i)) {
size_t idx = lean_unbox(i);
if (idx < lean_array_size(a))
return lean_array_uset(a, idx, v);
}
return lean_array_set_panic(a, v);
}
static inline lean_object * lean_array_pop(lean_obj_arg a) {
lean_object * r = lean_ensure_exclusive_array(a);
size_t sz = lean_to_array(r)->m_size;
lean_object ** last;
if (sz == 0) return r;
sz--;
last = lean_array_cptr(r) + sz;
lean_to_array(r)->m_size = sz;
lean_dec(*last);
return r;
}
static inline lean_object * lean_array_uswap(lean_obj_arg a, size_t i, size_t j) {
lean_object * r = lean_ensure_exclusive_array(a);
lean_object ** it = lean_array_cptr(r);
lean_object * v1 = it[i];
it[i] = it[j];
it[j] = v1;
return r;
}
static inline lean_object * lean_array_fswap(lean_obj_arg a, b_lean_obj_arg i, b_lean_obj_arg j) {
return lean_array_uswap(a, lean_unbox(i), lean_unbox(j));
}
static inline lean_object * lean_array_swap(lean_obj_arg a, b_lean_obj_arg i, b_lean_obj_arg j) {
if (!lean_is_scalar(i) || !lean_is_scalar(j)) return a;
size_t ui = lean_unbox(i);
size_t uj = lean_unbox(j);
size_t sz = lean_to_array(a)->m_size;
if (ui >= sz || uj >= sz) return a;
return lean_array_uswap(a, ui, uj);
}
LEAN_EXPORT lean_object * lean_array_push(lean_obj_arg a, lean_obj_arg v);
LEAN_EXPORT lean_object * lean_mk_array(lean_obj_arg n, lean_obj_arg v);
/* Array of scalars */
static inline lean_obj_res lean_alloc_sarray(unsigned elem_size, size_t size, size_t capacity) {
lean_sarray_object * o = (lean_sarray_object*)lean_alloc_object(sizeof(lean_sarray_object) + elem_size*capacity);
lean_set_st_header((lean_object*)o, LeanScalarArray, elem_size);
o->m_size = size;
o->m_capacity = capacity;
return (lean_object*)o;
}
static inline unsigned lean_sarray_elem_size(lean_object * o) {
assert(lean_is_sarray(o));
return lean_ptr_other(o);
}
static inline size_t lean_sarray_capacity(lean_object * o) { return lean_to_sarray(o)->m_capacity; }
static inline size_t lean_sarray_byte_size(lean_object * o) {
return sizeof(lean_sarray_object) + lean_sarray_elem_size(o)*lean_sarray_capacity(o);
}
static inline size_t lean_sarray_size(b_lean_obj_arg o) { return lean_to_sarray(o)->m_size; }
static inline size_t lean_sarray_data_byte_size(lean_object * o) {
return sizeof(lean_sarray_object) + lean_sarray_elem_size(o)*lean_sarray_size(o);
}
static inline void lean_sarray_set_size(u_lean_obj_arg o, size_t sz) {
assert(lean_is_exclusive(o));
assert(sz <= lean_sarray_capacity(o));
lean_to_sarray(o)->m_size = sz;
}
static inline uint8_t* lean_sarray_cptr(lean_object * o) { return lean_to_sarray(o)->m_data; }
/* Remark: expand sarray API after we add better support in the compiler */
/* ByteArray (special case of Array of Scalars) */
LEAN_EXPORT lean_obj_res lean_byte_array_mk(lean_obj_arg a);
LEAN_EXPORT lean_obj_res lean_byte_array_data(lean_obj_arg a);
LEAN_EXPORT lean_obj_res lean_copy_byte_array(lean_obj_arg a);
LEAN_EXPORT uint64_t lean_byte_array_hash(b_lean_obj_arg a);
static inline lean_obj_res lean_mk_empty_byte_array(b_lean_obj_arg capacity) {
if (!lean_is_scalar(capacity)) lean_internal_panic_out_of_memory();
return lean_alloc_sarray(1, 0, lean_unbox(capacity));
}
static inline lean_obj_res lean_byte_array_size(b_lean_obj_arg a) {
return lean_box(lean_sarray_size(a));
}
static inline uint8_t lean_byte_array_uget(b_lean_obj_arg a, size_t i) {
assert(i < lean_sarray_size(a));
return lean_sarray_cptr(a)[i];
}
static inline uint8_t lean_byte_array_get(b_lean_obj_arg a, b_lean_obj_arg i) {
if (lean_is_scalar(i)) {
size_t idx = lean_unbox(i);
return idx < lean_sarray_size(a) ? lean_byte_array_uget(a, idx) : 0;
} else {
/* The index must be out of bounds. Otherwise we would be out of memory. */
return 0;
}
}
static inline uint8_t lean_byte_array_fget(b_lean_obj_arg a, b_lean_obj_arg i) {
return lean_byte_array_uget(a, lean_unbox(i));
}
LEAN_EXPORT lean_obj_res lean_byte_array_push(lean_obj_arg a, uint8_t b);
static inline lean_object * lean_byte_array_uset(lean_obj_arg a, size_t i, uint8_t v) {
lean_obj_res r;
if (lean_is_exclusive(a)) r = a;
else r = lean_copy_byte_array(a);
uint8_t * it = lean_sarray_cptr(r) + i;
*it = v;
return r;
}
static inline lean_obj_res lean_byte_array_set(lean_obj_arg a, b_lean_obj_arg i, uint8_t b) {
if (!lean_is_scalar(i)) {
return a;
} else {
size_t idx = lean_unbox(i);
if (idx >= lean_sarray_size(a)) {
return a;
} else {
return lean_byte_array_uset(a, idx, b);
}
}
}
static inline lean_obj_res lean_byte_array_fset(lean_obj_arg a, b_lean_obj_arg i, uint8_t b) {
return lean_byte_array_uset(a, lean_unbox(i), b);
}
/* FloatArray (special case of Array of Scalars) */
LEAN_EXPORT lean_obj_res lean_float_array_mk(lean_obj_arg a);
LEAN_EXPORT lean_obj_res lean_float_array_data(lean_obj_arg a);
LEAN_EXPORT lean_obj_res lean_copy_float_array(lean_obj_arg a);
static inline lean_obj_res lean_mk_empty_float_array(b_lean_obj_arg capacity) {
if (!lean_is_scalar(capacity)) lean_internal_panic_out_of_memory();
return lean_alloc_sarray(sizeof(double), 0, lean_unbox(capacity)); // NOLINT
}
static inline lean_obj_res lean_float_array_size(b_lean_obj_arg a) {
return lean_box(lean_sarray_size(a));
}
static inline double * lean_float_array_cptr(b_lean_obj_arg a) {
return (double*)(lean_sarray_cptr(a)); // NOLINT
}
static inline double lean_float_array_uget(b_lean_obj_arg a, size_t i) {
return lean_float_array_cptr(a)[i];
}
static inline double lean_float_array_fget(b_lean_obj_arg a, b_lean_obj_arg i) {
return lean_float_array_uget(a, lean_unbox(i));
}
static inline double lean_float_array_get(b_lean_obj_arg a, b_lean_obj_arg i) {
if (lean_is_scalar(i)) {
size_t idx = lean_unbox(i);
return idx < lean_sarray_size(a) ? lean_float_array_uget(a, idx) : 0.0;
} else {
/* The index must be out of bounds. Otherwise we would be out of memory. */
return 0.0;
}
}
LEAN_EXPORT lean_obj_res lean_float_array_push(lean_obj_arg a, double d);
static inline lean_obj_res lean_float_array_uset(lean_obj_arg a, size_t i, double d) {