|
18 | 18 |
|
19 | 19 | #include <stdlib.h>
|
20 | 20 | #include <stdint.h>
|
21 |
| -#include "stdlib/ndarray/base/ind.h" |
22 |
| -#include "stdlib/ndarray/dtypes.h" |
23 |
| -#include "stdlib/ndarray/index_modes.h" |
24 | 21 | #include "stdlib/ndarray/ctor.h"
|
25 | 22 |
|
26 |
| -/** |
27 |
| -* Returns a byte array pointer to an ndarray data element. |
28 |
| -* |
29 |
| -* @param arr input ndarray |
30 |
| -* @param sub ndarray subscripts |
31 |
| -* @return underlying byte array pointer |
32 |
| -*/ |
33 |
| -uint8_t * stdlib_ndarray_get_ptr( const struct ndarray *arr, const int64_t *sub ) { |
34 |
| - enum STDLIB_NDARRAY_INDEX_MODE *submodes; |
35 |
| - enum STDLIB_NDARRAY_INDEX_MODE mode; |
36 |
| - int64_t *strides; |
37 |
| - int64_t *shape; |
38 |
| - int64_t ndims; |
39 |
| - uint8_t *idx; |
40 |
| - int64_t ind; |
41 |
| - int64_t M; |
42 |
| - int64_t i; |
43 |
| - |
44 |
| - ndims = arr->ndims; |
45 |
| - shape = arr->shape; |
46 |
| - strides = arr->strides; |
47 |
| - submodes = arr->submodes; |
48 |
| - M = arr->nsubmodes; |
49 |
| - |
50 |
| - idx = (arr->data) + (arr->offset); // pointer arithmetic |
51 |
| - for ( i = 0; i < ndims; i++ ) { |
52 |
| - mode = submodes[ i%M ]; |
53 |
| - ind = stdlib_ndarray_ind( sub[ i ], shape[ i ]-1, mode ); |
54 |
| - if ( ind < 0 ) { |
55 |
| - return NULL; |
56 |
| - } |
57 |
| - idx += strides[ i ] * ind; // pointer arithmetic |
58 |
| - } |
59 |
| - return idx; |
60 |
| -} |
61 |
| - |
62 |
| -/** |
63 |
| -* Returns an ndarray data element specified by a byte array pointer. |
64 |
| -* |
65 |
| -* ## Notes |
66 |
| -* |
67 |
| -* - The function does **not** perform bounds checking and **assumes** you know what you are doing. |
68 |
| -* - The function returns `-1` if unable to get an element and `0` otherwise. |
69 |
| -* - The function requires a `void` pointer for the output address `out` in order to provide a generic API supporting ndarrays having different data types. |
70 |
| -* |
71 |
| -* @param arr input ndarray |
72 |
| -* @param idx byte array pointer to an ndarray data element |
73 |
| -* @param out output address |
74 |
| -* @return status code |
75 |
| -*/ |
76 |
| -int8_t stdlib_ndarray_get_ptr_value( const struct ndarray *arr, const uint8_t *idx, void *out ) { |
77 |
| - switch ( arr->dtype ) { |
78 |
| - case STDLIB_NDARRAY_FLOAT64: |
79 |
| - *(double *)out = *(double *)idx; |
80 |
| - return 0; |
81 |
| - case STDLIB_NDARRAY_FLOAT32: |
82 |
| - *(float *)out = *(float *)idx; |
83 |
| - return 0; |
84 |
| - case STDLIB_NDARRAY_UINT64: |
85 |
| - *(uint64_t *)out = *(uint64_t *)idx; |
86 |
| - return 0; |
87 |
| - case STDLIB_NDARRAY_INT64: |
88 |
| - *(int64_t *)out = *(int64_t *)idx; |
89 |
| - return 0; |
90 |
| - case STDLIB_NDARRAY_UINT32: |
91 |
| - *(uint32_t *)out = *(uint32_t *)idx; |
92 |
| - return 0; |
93 |
| - case STDLIB_NDARRAY_INT32: |
94 |
| - *(int32_t *)out = *(int32_t *)idx; |
95 |
| - return 0; |
96 |
| - case STDLIB_NDARRAY_UINT16: |
97 |
| - *(uint16_t *)out = *(uint16_t *)idx; |
98 |
| - return 0; |
99 |
| - case STDLIB_NDARRAY_INT16: |
100 |
| - *(int16_t *)out = *(int16_t *)idx; |
101 |
| - return 0; |
102 |
| - case STDLIB_NDARRAY_UINT8: |
103 |
| - *(uint8_t *)out = *(uint8_t *)idx; |
104 |
| - return 0; |
105 |
| - case STDLIB_NDARRAY_INT8: |
106 |
| - *(int8_t *)out = *(int8_t *)idx; |
107 |
| - return 0; |
108 |
| - } |
109 |
| - return -1; |
110 |
| -} |
111 |
| - |
112 |
| -/** |
113 |
| -* Returns a double-precision floating-point ndarray data element specified by a byte array pointer. |
114 |
| -* |
115 |
| -* ## Notes |
116 |
| -* |
117 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
118 |
| -* - The function always returns `0`. |
119 |
| -* |
120 |
| -* @param idx byte array pointer to an ndarray data element |
121 |
| -* @param out output address |
122 |
| -* @return status code |
123 |
| -*/ |
124 |
| -int8_t stdlib_ndarray_get_ptr_float64( const uint8_t *idx, double *out ) { |
125 |
| - *out = *(double *)idx; |
126 |
| - return 0; |
127 |
| -} |
128 |
| - |
129 |
| -/** |
130 |
| -* Returns a single-precision floating-point ndarray data element specified by a byte array pointer. |
131 |
| -* |
132 |
| -* ## Notes |
133 |
| -* |
134 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
135 |
| -* - The function always returns `0`. |
136 |
| -* |
137 |
| -* @param idx byte array pointer to an ndarray data element |
138 |
| -* @param out output address |
139 |
| -* @return status code |
140 |
| -*/ |
141 |
| -int8_t stdlib_ndarray_get_ptr_float32( const uint8_t *idx, float *out ) { |
142 |
| - *out = *(float *)idx; |
143 |
| - return 0; |
144 |
| -} |
145 |
| - |
146 |
| -/** |
147 |
| -* Returns an unsigned 64-bit integer ndarray data element specified by a byte array pointer. |
148 |
| -* |
149 |
| -* ## Notes |
150 |
| -* |
151 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
152 |
| -* - The function always returns `0`. |
153 |
| -* |
154 |
| -* @param idx byte array pointer to an ndarray data element |
155 |
| -* @param out output address |
156 |
| -* @return status code |
157 |
| -*/ |
158 |
| -int8_t stdlib_ndarray_get_ptr_uint64( const uint8_t *idx, uint64_t *out ) { |
159 |
| - *out = *(uint64_t *)idx; |
160 |
| - return 0; |
161 |
| -} |
162 |
| - |
163 |
| -/** |
164 |
| -* Returns a signed 64-bit integer ndarray data element specified by a byte array pointer. |
165 |
| -* |
166 |
| -* ## Notes |
167 |
| -* |
168 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
169 |
| -* - The function always returns `0`. |
170 |
| -* |
171 |
| -* @param idx byte array pointer to an ndarray data element |
172 |
| -* @param out output address |
173 |
| -* @return status code |
174 |
| -*/ |
175 |
| -int8_t stdlib_ndarray_get_ptr_int64( const uint8_t *idx, int64_t *out ) { |
176 |
| - *out = *(int64_t *)idx; |
177 |
| - return 0; |
178 |
| -} |
179 |
| - |
180 |
| -/** |
181 |
| -* Returns an unsigned 32-bit integer ndarray data element specified by a byte array pointer. |
182 |
| -* |
183 |
| -* ## Notes |
184 |
| -* |
185 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
186 |
| -* - The function always returns `0`. |
187 |
| -* |
188 |
| -* @param idx byte array pointer to an ndarray data element |
189 |
| -* @param out output address |
190 |
| -* @return status code |
191 |
| -*/ |
192 |
| -int8_t stdlib_ndarray_get_ptr_uint32( const uint8_t *idx, uint32_t *out ) { |
193 |
| - *out = *(uint32_t *)idx; |
194 |
| - return 0; |
195 |
| -} |
196 |
| - |
197 |
| -/** |
198 |
| -* Returns a signed 32-bit integer ndarray data element specified by a byte array pointer. |
199 |
| -* |
200 |
| -* ## Notes |
201 |
| -* |
202 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
203 |
| -* - The function always returns `0`. |
204 |
| -* |
205 |
| -* @param idx byte array pointer to an ndarray data element |
206 |
| -* @param out output address |
207 |
| -* @return status code |
208 |
| -*/ |
209 |
| -int8_t stdlib_ndarray_get_ptr_int32( const uint8_t *idx, int32_t *out ) { |
210 |
| - *out = *(int32_t *)idx; |
211 |
| - return 0; |
212 |
| -} |
213 |
| - |
214 |
| -/** |
215 |
| -* Returns an unsigned 16-bit integer ndarray data element specified by a byte array pointer. |
216 |
| -* |
217 |
| -* ## Notes |
218 |
| -* |
219 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
220 |
| -* - The function always returns `0`. |
221 |
| -* |
222 |
| -* @param idx byte array pointer to an ndarray data element |
223 |
| -* @param out output address |
224 |
| -* @return status code |
225 |
| -*/ |
226 |
| -int8_t stdlib_ndarray_get_ptr_uint16( const uint8_t *idx, uint16_t *out ) { |
227 |
| - *out = *(uint16_t *)idx; |
228 |
| - return 0; |
229 |
| -} |
230 |
| - |
231 |
| -/** |
232 |
| -* Returns a signed 16-bit integer ndarray data element specified by a byte array pointer. |
233 |
| -* |
234 |
| -* ## Notes |
235 |
| -* |
236 |
| -* - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. |
237 |
| -* - The function always returns `0`. |
238 |
| -* |
239 |
| -* @param idx byte array pointer to an ndarray data element |
240 |
| -* @param out output address |
241 |
| -* @return status code |
242 |
| -*/ |
243 |
| -int8_t stdlib_ndarray_get_ptr_int16( const uint8_t *idx, int16_t *out ) { |
244 |
| - *out = *(int16_t *)idx; |
245 |
| - return 0; |
246 |
| -} |
247 |
| - |
248 |
| -/** |
249 |
| -* Returns an unsigned 8-bit integer ndarray data element specified by a byte array pointer. |
250 |
| -* |
251 |
| -* ## Notes |
252 |
| -* |
253 |
| -* - The function always returns `0`. |
254 |
| -* |
255 |
| -* @param idx byte array pointer to an ndarray data element |
256 |
| -* @param out output address |
257 |
| -* @return status code |
258 |
| -*/ |
259 |
| -int8_t stdlib_ndarray_get_ptr_uint8( const uint8_t *idx, uint8_t *out ) { |
260 |
| - *out = *(uint8_t *)idx; |
261 |
| - return 0; |
262 |
| -} |
263 |
| - |
264 |
| -/** |
265 |
| -* Returns a signed 8-bit integer ndarray data element specified by a byte array pointer. |
266 |
| -* |
267 |
| -* ## Notes |
268 |
| -* |
269 |
| -* - The function always returns `0`. |
270 |
| -* |
271 |
| -* @param idx byte array pointer to an ndarray data element |
272 |
| -* @param out output address |
273 |
| -* @return status code |
274 |
| -*/ |
275 |
| -int8_t stdlib_ndarray_get_ptr_int8( const uint8_t *idx, int8_t *out ) { |
276 |
| - *out = *(int8_t *)idx; |
277 |
| - return 0; |
278 |
| -} |
279 |
| - |
280 | 23 | /**
|
281 | 24 | * Returns an ndarray data element.
|
282 | 25 | *
|
|
0 commit comments