set_ptr.c (10038B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "stdlib/ndarray/ctor/set_ptr.h" 20 #include "stdlib/ndarray/ctor/ndarray.h" 21 #include "stdlib/ndarray/dtypes.h" 22 #include <stdint.h> 23 #include <stdbool.h> 24 #include <complex.h> 25 26 /** 27 * Sets an ndarray data element specified by a byte array pointer. 28 * 29 * ## Notes 30 * 31 * - The function does **not** perform bounds checking, and, thus, the function does **not** prevent you from overwriting **unowned** memory. Accordingly, the function **assumes** you know what you are doing. 32 * - The function returns `-1` if unable to set an element and `0` otherwise. 33 * - The function requires a pointer to a data value `v` in order to provide a generic API supporting ndarrays having different data types. 34 * 35 * @param arr input ndarray 36 * @param idx byte array pointer to an ndarray data element 37 * @param v value to set 38 * @return status code 39 */ 40 int8_t stdlib_ndarray_set_ptr_value( const struct ndarray *arr, uint8_t *idx, const void *v ) { 41 switch ( arr->dtype ) { 42 case STDLIB_NDARRAY_FLOAT64: 43 *(double *)idx = *(double *)v; 44 return 0; 45 case STDLIB_NDARRAY_FLOAT32: 46 *(float *)idx = *(float *)v; 47 return 0; 48 case STDLIB_NDARRAY_UINT64: 49 *(uint64_t *)idx = *(uint64_t *)v; 50 return 0; 51 case STDLIB_NDARRAY_INT64: 52 *(int64_t *)idx = *(int64_t *)v; 53 return 0; 54 case STDLIB_NDARRAY_UINT32: 55 *(uint32_t *)idx = *(uint32_t *)v; 56 return 0; 57 case STDLIB_NDARRAY_INT32: 58 *(int32_t *)idx = *(int32_t *)v; 59 return 0; 60 case STDLIB_NDARRAY_UINT16: 61 *(uint16_t *)idx = *(uint16_t *)v; 62 return 0; 63 case STDLIB_NDARRAY_INT16: 64 *(int16_t *)idx = *(int16_t *)v; 65 return 0; 66 case STDLIB_NDARRAY_UINT8: 67 *(uint8_t *)idx = *(uint8_t *)v; 68 return 0; 69 case STDLIB_NDARRAY_UINT8C: 70 *(uint8_t *)idx = *(uint8_t *)v; 71 return 0; 72 case STDLIB_NDARRAY_INT8: 73 *(int8_t *)idx = *(int8_t *)v; 74 return 0; 75 case STDLIB_NDARRAY_COMPLEX128: 76 *(double complex *)idx = *(double complex *)v; 77 return 0; 78 case STDLIB_NDARRAY_COMPLEX64: 79 *(float complex *)idx = *(float complex *)v; 80 return 0; 81 case STDLIB_NDARRAY_BOOL: 82 *(bool *)idx = *(bool *)v; 83 return 0; 84 default: 85 return -1; 86 } 87 } 88 89 /** 90 * Sets a double-precision floating-point ndarray data element specified by a byte array pointer. 91 * 92 * ## Notes 93 * 94 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 95 * - The function always returns `0`. 96 * 97 * @param idx byte array pointer to an ndarray data element 98 * @param v value to set 99 * @return status code 100 */ 101 int8_t stdlib_ndarray_set_ptr_float64( uint8_t *idx, const double v ) { 102 *(double *)idx = v; 103 return 0; 104 } 105 106 /** 107 * Sets a single-precision floating-point ndarray data element specified by a byte array pointer. 108 * 109 * ## Notes 110 * 111 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 112 * - The function always returns `0`. 113 * 114 * @param idx byte array pointer to an ndarray data element 115 * @param v value to set 116 * @return status code 117 */ 118 int8_t stdlib_ndarray_set_ptr_float32( uint8_t *idx, const float v ) { 119 *(float *)idx = v; 120 return 0; 121 } 122 123 /** 124 * Sets an unsigned 64-bit integer ndarray data element specified by a byte array pointer. 125 * 126 * ## Notes 127 * 128 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 129 * - The function always returns `0`. 130 * 131 * @param idx byte array pointer to an ndarray data element 132 * @param v value to set 133 * @return status code 134 */ 135 int8_t stdlib_ndarray_set_ptr_uint64( uint8_t *idx, const uint64_t v ) { 136 *(uint64_t *)idx = v; 137 return 0; 138 } 139 140 /** 141 * Sets a signed 64-bit integer ndarray data element specified by a byte array pointer. 142 * 143 * ## Notes 144 * 145 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 146 * - The function always returns `0`. 147 * 148 * @param idx byte array pointer to an ndarray data element 149 * @param v value to set 150 * @return status code 151 */ 152 int8_t stdlib_ndarray_set_ptr_int64( uint8_t *idx, const int64_t v ) { 153 *(int64_t *)idx = v; 154 return 0; 155 } 156 157 /** 158 * Sets an unsigned 32-bit integer ndarray data element specified by a byte array pointer. 159 * 160 * ## Notes 161 * 162 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 163 * - The function always returns `0`. 164 * 165 * @param idx byte array pointer to an ndarray data element 166 * @param v value to set 167 * @return status code 168 */ 169 int8_t stdlib_ndarray_set_ptr_uint32( uint8_t *idx, const uint32_t v ) { 170 *(uint32_t *)idx = v; 171 return 0; 172 } 173 174 /** 175 * Sets a signed 32-bit integer ndarray data element specified by a byte array pointer. 176 * 177 * ## Notes 178 * 179 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 180 * - The function always returns `0`. 181 * 182 * @param idx byte array pointer to an ndarray data element 183 * @param v value to set 184 * @return status code 185 */ 186 int8_t stdlib_ndarray_set_ptr_int32( uint8_t *idx, const int32_t v ) { 187 *(int32_t *)idx = v; 188 return 0; 189 } 190 191 /** 192 * Sets an unsigned 16-bit integer ndarray data element specified by a byte array pointer. 193 * 194 * ## Notes 195 * 196 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 197 * - The function always returns `0`. 198 * 199 * @param idx byte array pointer to an ndarray data element 200 * @param v value to set 201 * @return status code 202 */ 203 int8_t stdlib_ndarray_set_ptr_uint16( uint8_t *idx, const uint16_t v ) { 204 *(uint16_t *)idx = v; 205 return 0; 206 } 207 208 /** 209 * Sets a signed 16-bit integer ndarray data element specified by a byte array pointer. 210 * 211 * ## Notes 212 * 213 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 214 * - The function always returns `0`. 215 * 216 * @param idx byte array pointer to an ndarray data element 217 * @param v value to set 218 * @return status code 219 */ 220 int8_t stdlib_ndarray_set_ptr_int16( uint8_t *idx, const int16_t v ) { 221 *(int16_t *)idx = v; 222 return 0; 223 } 224 225 /** 226 * Sets an unsigned 8-bit integer ndarray data element specified by a byte array pointer. 227 * 228 * ## Notes 229 * 230 * - The function always returns `0`. 231 * 232 * @param idx byte array pointer to an ndarray data element 233 * @param v value to set 234 * @return status code 235 */ 236 int8_t stdlib_ndarray_set_ptr_uint8( uint8_t *idx, const uint8_t v ) { 237 *(uint8_t *)idx = v; 238 return 0; 239 } 240 241 /** 242 * Sets a signed 8-bit integer ndarray data element specified by a byte array pointer. 243 * 244 * ## Notes 245 * 246 * - The function always returns `0`. 247 * 248 * @param idx byte array pointer to an ndarray data element 249 * @param v value to set 250 * @return status code 251 */ 252 int8_t stdlib_ndarray_set_ptr_int8( uint8_t *idx, const int8_t v ) { 253 *(int8_t *)idx = v; 254 return 0; 255 } 256 257 /** 258 * Sets a double-precision complex floating-point ndarray data element specified by a byte array pointer. 259 * 260 * ## Notes 261 * 262 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 263 * - The function always returns `0`. 264 * 265 * @param idx byte array pointer to an ndarray data element 266 * @param v value to set 267 * @return status code 268 */ 269 int8_t stdlib_ndarray_set_ptr_complex128( uint8_t *idx, const double complex v ) { 270 *(double complex *)idx = v; 271 return 0; 272 } 273 274 /** 275 * Sets a single-precision complex floating-point ndarray data element specified by a byte array pointer. 276 * 277 * ## Notes 278 * 279 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 280 * - The function always returns `0`. 281 * 282 * @param idx byte array pointer to an ndarray data element 283 * @param v value to set 284 * @return status code 285 */ 286 int8_t stdlib_ndarray_set_ptr_complex64( uint8_t *idx, const float complex v ) { 287 *(float complex *)idx = v; 288 return 0; 289 } 290 291 /** 292 * Sets a boolean ndarray data element specified by a byte array pointer. 293 * 294 * ## Notes 295 * 296 * - The function has no way of determining whether `idx` actually points to a compatible memory address. Accordingly, overwriting **unowned** memory is possible, and this function **assumes** you know what you are doing. 297 * - The function always returns `0`. 298 * 299 * @param idx byte array pointer to an ndarray data element 300 * @param v value to set 301 * @return status code 302 */ 303 int8_t stdlib_ndarray_set_ptr_bool( uint8_t *idx, const bool v ) { 304 *(bool *)idx = v; 305 return 0; 306 }