-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprawl_c.odin
113 lines (88 loc) · 3.44 KB
/
sprawl_c.odin
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
package sprawl
// This encompasses everything in the file.
// Hence, from here on, though it goes against
// common style, everything will start with 0 indent.
//
// If you want c-compatibility within Sprawl, make sure to
// define `CCOMPAT` to `true`.
//
// TODO(F0x): Once Odin allows for compiling to big-endian,
// add an endianness `when` check.
when #config(CCOMPAT, false) {
import "core:runtime"
import "core:c"
sprawlc_Slice :: struct {
size : c.size_t,
data : rawptr, // [] byte
n_data : u32,
lengths : rawptr, // [] u32
n_lengths: u32,
offsets : rawptr, // [] u32
n_offsets: u32,
}
// Converts a c-compatible array to an Odin u32 array.
ccompat_arr_to_u32arr :: inline proc "c" (arr: rawptr, len: u32) -> [] u32 {
return transmute([]u32) runtime.Raw_Slice{
arr,
cast(int) len,
};
}
@export
@(link_name="_sprawlc_index")
sprawlc_index :: proc "c" (ndim: ^sprawlc_Slice, n_indexes: u32, indexes: rawptr) -> u32 {
indexes := ccompat_arr_to_u32arr(indexes, n_indexes);
lengths := ccompat_arr_to_u32arr(ndim.lengths, ndim.n_lengths);
if (ndim.offsets == nil) {
return _index(indexes, lengths);
}
else {
offsets := ccompat_arr_to_u32arr(ndim.offsets, ndim.n_offsets);
return _index_sliced(indexes, lengths, offsets);
}
}
@export
@(link_name="_sprawlc_get")
sprawlc_get :: proc "c" (ndim: ^sprawlc_Slice, dest: rawptr, n_indexes: u32, indexes: rawptr) {
indexes := ccompat_arr_to_u32arr(indexes, n_indexes);
lengths := ccompat_arr_to_u32arr(ndim.lengths, ndim.n_lengths);
src: uintptr = transmute(uintptr) ndim.data;
if (ndim.offsets == nil) {
src += cast(uintptr) (cast(u64) _index(indexes, lengths) * cast(u64) ndim.size);
}
else {
offsets := ccompat_arr_to_u32arr(ndim.offsets, ndim.n_offsets);
src += cast(uintptr) (cast(u64) _index_sliced(indexes, lengths, offsets) * cast(u64) ndim.size);
}
runtime.mem_copy(dest, cast(rawptr) src, cast(int) ndim.size);
}
@export
@(link_name="_sprawlc_set")
sprawlc_set :: proc "c" (ndim: ^sprawlc_Slice, src: rawptr, n_indexes: u32, indexes: rawptr) {
indexes := ccompat_arr_to_u32arr(indexes, n_indexes);
lengths := ccompat_arr_to_u32arr(ndim.lengths, ndim.n_lengths);
dest: uintptr = transmute(uintptr) ndim.data;
if (ndim.offsets == nil) {
dest += cast(uintptr) (cast(u64) _index(indexes, lengths) * cast(u64) ndim.size);
}
else {
offsets := ccompat_arr_to_u32arr(ndim.offsets, ndim.n_offsets);
dest += cast(uintptr) (cast(u64) _index_sliced(indexes, lengths, offsets) * cast(u64) ndim.size);
}
runtime.mem_copy(cast(rawptr) dest, src, cast(int) ndim.size);
}
@export
@(link_name="_sprawlc_ref")
sprawlc_ref :: proc "c" (ndim: ^sprawlc_Slice, n_indexes: u32, indexes: rawptr) -> rawptr {
indexes := ccompat_arr_to_u32arr(indexes, n_indexes);
lengths := ccompat_arr_to_u32arr(ndim.lengths, ndim.n_lengths);
if (ndim.offsets == nil) {
return cast(rawptr) (cast(uintptr) ndim.data +
cast(uintptr) (_index(indexes, lengths) * cast(u32) ndim.size));
}
else {
offsets := ccompat_arr_to_u32arr(ndim.offsets, ndim.n_offsets);
return cast(rawptr) (cast(uintptr) ndim.data +
cast(uintptr) (_index_sliced(indexes, lengths, offsets) * cast(u32) ndim.size));
}
}
}