Skip to main content

leanfran_runtime/
array.rs

1//! `Array` (arrays of objects) and scalar arrays (`ByteArray`, `FloatArray`).
2
3use crate::nat::nat_to_usize;
4use crate::object::*;
5use crate::panic::{lean_internal_panic, lean_internal_panic_out_of_memory, lean_panic_fn};
6use crate::string::lean_mk_ascii_string_unchecked;
7use core::mem::{offset_of, size_of};
8use core::ptr;
9
10#[inline]
11unsafe fn ao(o: Obj) -> *mut ArrayObject {
12    o as *mut ArrayObject
13}
14
15#[inline]
16unsafe fn sao(o: Obj) -> *mut SArrayObject {
17    o as *mut SArrayObject
18}
19
20/* Arrays of objects */
21
22#[inline]
23pub unsafe fn lean_alloc_array(size: usize, capacity: usize) -> Obj {
24    let bytes = capacity
25        .checked_mul(size_of::<Obj>())
26        .and_then(|b| b.checked_add(size_of::<ArrayObject>()))
27        .unwrap_or_else(|| crate::panic::lean_internal_panic_overflow());
28    let o = lean_alloc_object(bytes);
29    lean_set_st_header(o, LEAN_ARRAY as u32, 0);
30    (*ao(o)).m_size = size;
31    (*ao(o)).m_capacity = capacity;
32    o
33}
34
35#[inline]
36pub unsafe fn lean_array_size(o: Obj) -> usize {
37    debug_check_live(o, "array read");
38    (*ao(o)).m_size
39}
40
41#[inline]
42pub unsafe fn lean_array_capacity(o: Obj) -> usize {
43    (*ao(o)).m_capacity
44}
45
46#[inline]
47pub unsafe fn lean_array_byte_size(o: Obj) -> usize {
48    size_of::<ArrayObject>() + size_of::<Obj>() * lean_array_capacity(o)
49}
50
51#[inline]
52pub unsafe fn lean_array_set_size(o: Obj, sz: usize) {
53    (*ao(o)).m_size = sz;
54}
55
56#[inline]
57pub unsafe fn lean_array_get_core(o: Obj, i: usize) -> Obj {
58    debug_assert!(i < lean_array_size(o));
59    *lean_array_cptr(o).add(i)
60}
61
62#[inline]
63pub unsafe fn lean_array_set_core(o: Obj, i: usize, v: Obj) {
64    *lean_array_cptr(o).add(i) = v;
65}
66
67#[inline]
68pub unsafe fn lean_array_is_marked_linear(o: Obj) -> bool {
69    (*o).m_other & LEAN_LINEAR_MARK_MASK != 0
70}
71
72#[inline]
73pub unsafe fn lean_mk_empty_array() -> Obj {
74    lean_alloc_array(0, 0)
75}
76
77#[inline]
78pub unsafe fn lean_mk_empty_array_with_capacity(capacity: Obj) -> Obj {
79    if !lean_is_scalar(capacity) {
80        lean_internal_panic_out_of_memory();
81    }
82    lean_alloc_array(0, lean_unbox(capacity))
83}
84
85#[inline]
86pub unsafe fn lean_array_get_size(a: Obj) -> Obj {
87    lean_box(lean_array_size(a))
88}
89
90/// `Array.size` of an owned array.
91#[inline]
92pub unsafe fn lean_array_sz(a: Obj) -> Obj {
93    let r = lean_box(lean_array_size(a));
94    lean_dec(a);
95    r
96}
97
98#[inline]
99pub unsafe fn lean_array_uget(a: Obj, i: usize) -> Obj {
100    let r = lean_array_get_core(a, i);
101    lean_inc(r);
102    r
103}
104
105#[inline]
106pub unsafe fn lean_array_uget_borrowed(a: Obj, i: usize) -> Obj {
107    lean_array_get_core(a, i)
108}
109
110#[inline]
111pub unsafe fn lean_array_fget(a: Obj, i: Obj) -> Obj {
112    lean_array_uget(a, lean_unbox(i))
113}
114
115#[inline]
116pub unsafe fn lean_array_fget_borrowed(a: Obj, i: Obj) -> Obj {
117    lean_array_get_core(a, lean_unbox(i))
118}
119
120/// Prints the out-of-bounds panic and returns `def_val` (owned).
121pub unsafe fn lean_array_get_panic(def_val: Obj) -> Obj {
122    lean_panic_fn(
123        def_val,
124        lean_mk_ascii_string_unchecked("Error: index out of bounds"),
125    )
126}
127
128/// Consumes `a` and `v`; returns `a`.
129pub unsafe fn lean_array_set_panic(a: Obj, v: Obj) -> Obj {
130    lean_dec(v);
131    lean_panic_fn(
132        a,
133        lean_mk_ascii_string_unchecked("Error: index out of bounds"),
134    )
135}
136
137/// `a[i]!` (`def_val`, `a`, `i` borrowed).
138#[inline]
139pub unsafe fn lean_array_get(def_val: Obj, a: Obj, i: Obj) -> Obj {
140    if lean_is_scalar(i) {
141        let idx = lean_unbox(i);
142        if idx < lean_array_size(a) {
143            return lean_array_uget(a, idx);
144        }
145    }
146    lean_inc(def_val);
147    lean_array_get_panic(def_val)
148}
149
150/// `a[i]!` returning a borrowed element.
151#[inline]
152pub unsafe fn lean_array_get_borrowed(def_val: Obj, a: Obj, i: Obj) -> Obj {
153    if lean_is_scalar(i) {
154        let idx = lean_unbox(i);
155        if idx < lean_array_size(a) {
156            return lean_array_get_core(a, idx);
157        }
158    }
159    lean_inc(def_val);
160    lean_array_get_panic(def_val)
161}
162
163/// A copy of the array `a` (consumed), with a larger capacity when `expand`.
164pub unsafe fn lean_copy_expand_array(a: Obj, expand: bool) -> Obj {
165    let sz = lean_array_size(a);
166    let mut cap = lean_array_capacity(a);
167    if expand {
168        cap = (cap + 1) * 2;
169    }
170    let r = lean_alloc_array(sz, cap);
171    (*r).m_other |= (*a).m_other & LEAN_LINEAR_MARK_MASK;
172    let src = lean_array_cptr(a);
173    let dst = lean_array_cptr(r);
174    if lean_is_exclusive(a) {
175        ptr::copy_nonoverlapping(src, dst, sz);
176        lean_dealloc(a, lean_array_byte_size(a));
177    } else {
178        for i in 0..sz {
179            let v = *src.add(i);
180            lean_inc(v);
181            *dst.add(i) = v;
182        }
183        lean_dec(a);
184    }
185    r
186}
187
188pub unsafe fn lean_copy_expand_array_nonlinear(a: Obj, expand: bool) -> Obj {
189    lean_copy_expand_array(a, expand)
190}
191
192#[inline]
193pub unsafe fn lean_ensure_exclusive_array(a: Obj) -> Obj {
194    if lean_is_exclusive(a) {
195        a
196    } else {
197        lean_copy_expand_array(a, false)
198    }
199}
200
201pub unsafe fn lean_array_mark_linear(a: Obj) -> Obj {
202    let r = lean_ensure_exclusive_array(a);
203    (*r).m_other |= LEAN_LINEAR_MARK_MASK;
204    r
205}
206
207/// Marks `dst` (owned) linear when `src` (borrowed) is.
208pub unsafe fn lean_array_propagate_mark(src: Obj, dst: Obj) -> Obj {
209    if !lean_array_is_marked_linear(src) {
210        dst
211    } else {
212        lean_array_mark_linear(dst)
213    }
214}
215
216#[inline]
217pub unsafe fn lean_array_uset(a: Obj, i: usize, v: Obj) -> Obj {
218    let r = lean_ensure_exclusive_array(a);
219    let it = lean_array_cptr(r).add(i);
220    lean_dec(*it);
221    *it = v;
222    r
223}
224
225#[inline]
226pub unsafe fn lean_array_fset(a: Obj, i: Obj, v: Obj) -> Obj {
227    lean_array_uset(a, lean_unbox(i), v)
228}
229
230#[inline]
231pub unsafe fn lean_array_set(a: Obj, i: Obj, v: Obj) -> Obj {
232    if lean_is_scalar(i) {
233        let idx = lean_unbox(i);
234        if idx < lean_array_size(a) {
235            return lean_array_uset(a, idx, v);
236        }
237    }
238    lean_array_set_panic(a, v)
239}
240
241pub unsafe fn lean_array_pop(a: Obj) -> Obj {
242    let r = lean_ensure_exclusive_array(a);
243    let sz = lean_array_size(r);
244    if sz == 0 {
245        return r;
246    }
247    (*ao(r)).m_size = sz - 1;
248    lean_dec(*lean_array_cptr(r).add(sz - 1));
249    r
250}
251
252#[inline]
253pub unsafe fn lean_array_uswap(a: Obj, i: usize, j: usize) -> Obj {
254    let r = lean_ensure_exclusive_array(a);
255    let it = lean_array_cptr(r);
256    ptr::swap(it.add(i), it.add(j));
257    r
258}
259
260#[inline]
261pub unsafe fn lean_array_fswap(a: Obj, i: Obj, j: Obj) -> Obj {
262    lean_array_uswap(a, lean_unbox(i), lean_unbox(j))
263}
264
265pub unsafe fn lean_array_swap(a: Obj, i: Obj, j: Obj) -> Obj {
266    if !lean_is_scalar(i) || !lean_is_scalar(j) {
267        return a;
268    }
269    let (ui, uj) = (lean_unbox(i), lean_unbox(j));
270    let sz = lean_array_size(a);
271    if ui >= sz || uj >= sz {
272        return a;
273    }
274    lean_array_uswap(a, ui, uj)
275}
276
277pub unsafe fn lean_array_push(a: Obj, v: Obj) -> Obj {
278    let r = if lean_is_exclusive(a) {
279        if lean_array_capacity(a) > lean_array_size(a) {
280            a
281        } else {
282            lean_copy_expand_array(a, true)
283        }
284    } else {
285        lean_copy_expand_array(a, lean_array_capacity(a) < 2 * lean_array_size(a) + 1)
286    };
287    let sz = lean_array_size(r);
288    *lean_array_cptr(r).add(sz) = v;
289    (*ao(r)).m_size = sz + 1;
290    r
291}
292
293/// `Array.replicate n v` (both consumed).
294pub unsafe fn lean_mk_array(n: Obj, v: Obj) -> Obj {
295    let sz = match nat_to_usize(n) {
296        Some(s) => s,
297        None => lean_internal_panic_out_of_memory(),
298    };
299    lean_dec(n);
300    let r = lean_alloc_array(sz, sz);
301    let it = lean_array_cptr(r);
302    for i in 0..sz {
303        *it.add(i) = v;
304    }
305    if sz == 0 {
306        lean_dec(v);
307    } else if sz > 1 {
308        lean_inc_n(v, sz - 1);
309    }
310    r
311}
312
313/// `Array.mk` / `List.toArray` (consumes the list).
314pub unsafe fn lean_array_mk(lst: Obj) -> Obj {
315    let mut n = 0;
316    let mut o = lst;
317    while !lean_is_scalar(o) {
318        n += 1;
319        o = lean_ctor_get(o, 1);
320    }
321    let r = lean_alloc_array(n, n);
322    let it = lean_array_cptr(r);
323    let mut o = lst;
324    for i in 0..n {
325        let h = lean_ctor_get(o, 0);
326        lean_inc(h);
327        *it.add(i) = h;
328        o = lean_ctor_get(o, 1);
329    }
330    lean_dec(lst);
331    r
332}
333
334/// `Array.toList` (consumes the array).
335pub unsafe fn lean_array_to_list(a: Obj) -> Obj {
336    let sz = lean_array_size(a);
337    let it = lean_array_cptr(a);
338    let mut r = lean_box(0);
339    for i in (0..sz).rev() {
340        let h = *it.add(i);
341        lean_inc(h);
342        let c = lean_alloc_ctor(1, 2, 0);
343        lean_ctor_set(c, 0, h);
344        lean_ctor_set(c, 1, r);
345        r = c;
346    }
347    lean_dec(a);
348    r
349}
350
351/* Scalar arrays */
352
353pub unsafe fn lean_alloc_sarray(elem_size: u32, size: usize, capacity: usize) -> Obj {
354    if elem_size >= LEAN_LINEAR_MARK_MASK as u32 {
355        lean_internal_panic("scalar array element size is larger than 2^7");
356    }
357    let bytes = capacity
358        .checked_mul(elem_size as usize)
359        .and_then(|b| b.checked_add(size_of::<SArrayObject>()))
360        .unwrap_or_else(|| crate::panic::lean_internal_panic_overflow());
361    let o = lean_alloc_object(bytes);
362    lean_set_st_header(o, LEAN_SCALAR_ARRAY as u32, elem_size);
363    (*sao(o)).m_size = size;
364    (*sao(o)).m_capacity = capacity;
365    o
366}
367
368#[inline]
369pub unsafe fn lean_sarray_capacity(o: Obj) -> usize {
370    (*sao(o)).m_capacity
371}
372
373#[inline]
374pub unsafe fn lean_sarray_byte_size(o: Obj) -> usize {
375    size_of::<SArrayObject>() + lean_sarray_elem_size(o) as usize * lean_sarray_capacity(o)
376}
377
378/// Number of elements (`ByteArray.usize` and friends).
379#[inline]
380pub unsafe fn lean_sarray_size(o: Obj) -> usize {
381    debug_check_live(o, "array read");
382    (*sao(o)).m_size
383}
384
385#[inline]
386pub unsafe fn lean_sarray_set_size(o: Obj, sz: usize) {
387    (*sao(o)).m_size = sz;
388}
389
390#[inline]
391pub unsafe fn lean_sarray_cptr(o: Obj) -> *mut u8 {
392    (o as *mut u8).add(offset_of!(SArrayObject, m_data))
393}
394
395/// The bytes of a `ByteArray`.
396#[inline]
397pub(crate) unsafe fn sarray_bytes<'a>(o: Obj) -> &'a [u8] {
398    core::slice::from_raw_parts(
399        lean_sarray_cptr(o),
400        lean_sarray_size(o) * lean_sarray_elem_size(o) as usize,
401    )
402}
403
404pub unsafe fn lean_copy_sarray(a: Obj, cap: usize) -> Obj {
405    let esz = lean_sarray_elem_size(a);
406    let sz = lean_sarray_size(a);
407    let r = lean_alloc_sarray(esz, sz, cap);
408    (*r).m_other |= (*a).m_other & LEAN_LINEAR_MARK_MASK;
409    ptr::copy_nonoverlapping(lean_sarray_cptr(a), lean_sarray_cptr(r), sz * esz as usize);
410    lean_dec(a);
411    r
412}
413
414pub unsafe fn lean_copy_sarray_nonlinear(a: Obj, cap: usize) -> Obj {
415    lean_copy_sarray(a, cap)
416}
417
418#[inline]
419pub unsafe fn lean_sarray_ensure_exclusive(a: Obj) -> Obj {
420    if lean_is_exclusive(a) {
421        a
422    } else {
423        lean_copy_sarray(a, lean_sarray_capacity(a))
424    }
425}
426
427/// `a` (consumed) with room for `min_cap` elements (twice that unless `exact`).
428unsafe fn sarray_ensure_capacity(a: Obj, min_cap: usize, exact: bool) -> Obj {
429    let cap = lean_sarray_capacity(a);
430    if min_cap <= cap {
431        return a;
432    }
433    let esz = lean_sarray_elem_size(a);
434    let sz = lean_sarray_size(a);
435    let new_cap = if exact { min_cap } else { min_cap.max(2 * cap) };
436    let r = lean_alloc_sarray(esz, sz, new_cap);
437    (*r).m_other |= (*a).m_other & LEAN_LINEAR_MARK_MASK;
438    ptr::copy_nonoverlapping(lean_sarray_cptr(a), lean_sarray_cptr(r), sz * esz as usize);
439    lean_dec(a);
440    r
441}
442
443pub unsafe fn lean_sarray_mark_linear(a: Obj) -> Obj {
444    let r = lean_sarray_ensure_exclusive(a);
445    (*r).m_other |= LEAN_LINEAR_MARK_MASK;
446    r
447}
448
449pub unsafe fn lean_sarray_propagate_mark(src: Obj, dst: Obj) -> Obj {
450    if (*src).m_other & LEAN_LINEAR_MARK_MASK == 0 {
451        dst
452    } else {
453        lean_sarray_mark_linear(dst)
454    }
455}
456
457#[inline]
458pub unsafe fn lean_sarray_eq(a1: Obj, a2: Obj) -> bool {
459    a1 == a2 || sarray_bytes(a1) == sarray_bytes(a2)
460}
461
462#[inline]
463pub unsafe fn lean_sarray_dec_eq(a1: Obj, a2: Obj) -> u8 {
464    lean_sarray_eq(a1, a2) as u8
465}
466
467/* ByteArray */
468
469/// `ByteArray.mk` (consumes the array of boxed bytes).
470pub unsafe fn lean_byte_array_mk(a: Obj) -> Obj {
471    let sz = lean_array_size(a);
472    let r = lean_alloc_sarray(1, sz, sz);
473    let src = lean_array_cptr(a);
474    let dst = lean_sarray_cptr(r);
475    for i in 0..sz {
476        *dst.add(i) = lean_unbox(*src.add(i)) as u8;
477    }
478    lean_dec(a);
479    r
480}
481
482/// `ByteArray.data` (consumes the byte array).
483pub unsafe fn lean_byte_array_data(a: Obj) -> Obj {
484    let sz = lean_sarray_size(a);
485    let r = lean_alloc_array(sz, sz);
486    let src = lean_sarray_cptr(a);
487    let dst = lean_array_cptr(r);
488    for i in 0..sz {
489        *dst.add(i) = lean_box(*src.add(i) as usize);
490    }
491    lean_dec(a);
492    r
493}
494
495pub unsafe fn lean_byte_array_hash(a: Obj) -> u64 {
496    crate::string::murmur_hash_64a(sarray_bytes(a), 11)
497}
498
499#[inline]
500pub unsafe fn lean_mk_empty_byte_array(capacity: Obj) -> Obj {
501    if !lean_is_scalar(capacity) {
502        lean_internal_panic_out_of_memory();
503    }
504    lean_alloc_sarray(1, 0, lean_unbox(capacity))
505}
506
507#[inline]
508pub unsafe fn lean_byte_array_size(a: Obj) -> Obj {
509    lean_box(lean_sarray_size(a))
510}
511
512#[inline]
513pub unsafe fn lean_byte_array_uget(a: Obj, i: usize) -> u8 {
514    *lean_sarray_cptr(a).add(i)
515}
516
517#[inline]
518pub unsafe fn lean_byte_array_get(a: Obj, i: Obj) -> u8 {
519    if lean_is_scalar(i) {
520        let idx = lean_unbox(i);
521        if idx < lean_sarray_size(a) {
522            return lean_byte_array_uget(a, idx);
523        }
524    }
525    0
526}
527
528#[inline]
529pub unsafe fn lean_byte_array_fget(a: Obj, i: Obj) -> u8 {
530    lean_byte_array_uget(a, lean_unbox(i))
531}
532
533pub unsafe fn lean_byte_array_push(a: Obj, b: u8) -> Obj {
534    let sz = lean_sarray_size(a);
535    let r = lean_sarray_ensure_exclusive(sarray_ensure_capacity(a, sz + 1, false));
536    *lean_sarray_cptr(r).add(sz) = b;
537    (*sao(r)).m_size = sz + 1;
538    r
539}
540
541#[inline]
542pub unsafe fn lean_byte_array_uset(a: Obj, i: usize, v: u8) -> Obj {
543    let r = lean_sarray_ensure_exclusive(a);
544    *lean_sarray_cptr(r).add(i) = v;
545    r
546}
547
548#[inline]
549pub unsafe fn lean_byte_array_set(a: Obj, i: Obj, b: u8) -> Obj {
550    if !lean_is_scalar(i) {
551        return a;
552    }
553    let idx = lean_unbox(i);
554    if idx >= lean_sarray_size(a) {
555        a
556    } else {
557        lean_byte_array_uset(a, idx, b)
558    }
559}
560
561#[inline]
562pub unsafe fn lean_byte_array_fset(a: Obj, i: Obj, b: u8) -> Obj {
563    lean_byte_array_uset(a, lean_unbox(i), b)
564}
565
566/// `ByteArray.copySlice src srcOff dest destOff len exact` (`src` borrowed, the rest owned).
567pub unsafe fn lean_byte_array_copy_slice(
568    src: Obj,
569    o_src_off: Obj,
570    dest: Obj,
571    o_dest_off: Obj,
572    o_len: Obj,
573    exact: u8,
574) -> Obj {
575    let ssz = lean_sarray_size(src);
576    let dsz = lean_sarray_size(dest);
577    let src_off = nat_to_usize(o_src_off).unwrap_or(usize::MAX);
578    let len0 = nat_to_usize(o_len).unwrap_or(usize::MAX);
579    let mut dest_off = nat_to_usize(o_dest_off).unwrap_or(usize::MAX);
580    lean_dec(o_src_off);
581    lean_dec(o_len);
582    lean_dec(o_dest_off);
583    if src_off > ssz {
584        return dest;
585    }
586    let len = len0.min(ssz - src_off);
587    if dest_off > dsz {
588        dest_off = dsz;
589    }
590    let new_dsz = dsz.max(dest_off + len);
591    let r = lean_sarray_ensure_exclusive(sarray_ensure_capacity(dest, new_dsz, exact != 0));
592    (*sao(r)).m_size = new_dsz;
593    ptr::copy(
594        lean_sarray_cptr(src).add(src_off),
595        lean_sarray_cptr(r).add(dest_off),
596        len,
597    );
598    r
599}
600
601/// `ByteArray.toUInt64LE!` style helpers are Lean code; this is `ByteArray.size` as `USize`.
602#[inline]
603pub unsafe fn lean_byte_array_usize(a: Obj) -> usize {
604    lean_sarray_size(a)
605}
606
607/* FloatArray */
608
609#[inline]
610unsafe fn float_cptr(a: Obj) -> *mut f64 {
611    lean_sarray_cptr(a) as *mut f64
612}
613
614pub unsafe fn lean_float_array_mk(a: Obj) -> Obj {
615    let sz = lean_array_size(a);
616    let r = lean_alloc_sarray(size_of::<f64>() as u32, sz, sz);
617    let src = lean_array_cptr(a);
618    for i in 0..sz {
619        ptr::write_unaligned(float_cptr(r).add(i), lean_unbox_float(*src.add(i)));
620    }
621    lean_dec(a);
622    r
623}
624
625pub unsafe fn lean_float_array_data(a: Obj) -> Obj {
626    let sz = lean_sarray_size(a);
627    let r = lean_alloc_array(sz, sz);
628    let dst = lean_array_cptr(r);
629    for i in 0..sz {
630        *dst.add(i) = lean_box_float(ptr::read_unaligned(float_cptr(a).add(i)));
631    }
632    lean_dec(a);
633    r
634}
635
636#[inline]
637pub unsafe fn lean_mk_empty_float_array(capacity: Obj) -> Obj {
638    if !lean_is_scalar(capacity) {
639        lean_internal_panic_out_of_memory();
640    }
641    lean_alloc_sarray(size_of::<f64>() as u32, 0, lean_unbox(capacity))
642}
643
644#[inline]
645pub unsafe fn lean_float_array_size(a: Obj) -> Obj {
646    lean_box(lean_sarray_size(a))
647}
648
649#[inline]
650pub unsafe fn lean_float_array_cptr(a: Obj) -> *mut f64 {
651    float_cptr(a)
652}
653
654#[inline]
655pub unsafe fn lean_float_array_uget(a: Obj, i: usize) -> f64 {
656    ptr::read_unaligned(float_cptr(a).add(i))
657}
658
659#[inline]
660pub unsafe fn lean_float_array_fget(a: Obj, i: Obj) -> f64 {
661    lean_float_array_uget(a, lean_unbox(i))
662}
663
664#[inline]
665pub unsafe fn lean_float_array_get(a: Obj, i: Obj) -> f64 {
666    if lean_is_scalar(i) {
667        let idx = lean_unbox(i);
668        if idx < lean_sarray_size(a) {
669            return lean_float_array_uget(a, idx);
670        }
671    }
672    0.0
673}
674
675pub unsafe fn lean_float_array_push(a: Obj, d: f64) -> Obj {
676    let sz = lean_sarray_size(a);
677    let r = lean_sarray_ensure_exclusive(sarray_ensure_capacity(a, sz + 1, false));
678    ptr::write_unaligned(float_cptr(r).add(sz), d);
679    (*sao(r)).m_size = sz + 1;
680    r
681}
682
683#[inline]
684pub unsafe fn lean_float_array_uset(a: Obj, i: usize, d: f64) -> Obj {
685    let r = lean_sarray_ensure_exclusive(a);
686    ptr::write_unaligned(float_cptr(r).add(i), d);
687    r
688}
689
690#[inline]
691pub unsafe fn lean_float_array_fset(a: Obj, i: Obj, d: f64) -> Obj {
692    lean_float_array_uset(a, lean_unbox(i), d)
693}
694
695#[inline]
696pub unsafe fn lean_float_array_set(a: Obj, i: Obj, d: f64) -> Obj {
697    if !lean_is_scalar(i) {
698        return a;
699    }
700    let idx = lean_unbox(i);
701    if idx >= lean_sarray_size(a) {
702        a
703    } else {
704        lean_float_array_uset(a, idx, d)
705    }
706}