diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2021-06-18 15:25:36 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2021-06-18 15:25:36 +0200 |
| commit | 54a2b6f00e49008a1db9907213a99212e17ac5a3 (patch) | |
| tree | 78ea170cc0768cc43420478d0606d065eeae1cc8 /src/llvm_backend.cpp | |
| parent | abe728dbbb51dc5e9c905e2cfa72f2c687c36588 (diff) | |
Add `bytes.buffer_create_of_type` and `bytes.buffer_convert_to_type`.
Convenience functions to reinterpret or cast one buffer to another type, or create a buffer of a specific type.
Example:
```odin
fmt.println("Convert []f16le (x2) to []f32 (x2).");
b := []u8{0, 60, 0, 60}; // == []f16{1.0, 1.0}
res, backing, had_to_allocate, err := bytes.buffer_convert_to_type(2, f32, f16le, b);
fmt.printf("res : %v\n", res); // [1.000, 1.000]
fmt.printf("backing : %v\n", backing); // &Buffer{buf = [0, 0, 128, 63, 0, 0, 128, 63], off = 0, last_read = Invalid}
fmt.printf("allocated: %v\n", had_to_allocate); // true
fmt.printf("err : %v\n", err); // false
if had_to_allocate { defer bytes.buffer_destroy(backing); }
fmt.println("\nConvert []f16le (x2) to []u16 (x2).");
res2: []u16;
res2, backing, had_to_allocate, err = bytes.buffer_convert_to_type(2, u16, f16le, b);
fmt.printf("res : %v\n", res2); // [15360, 15360]
fmt.printf("backing : %v\n", backing); // Buffer.buf points to `b` because it could be converted in-place.
fmt.printf("allocated: %v\n", had_to_allocate); // false
fmt.printf("err : %v\n", err); // false
if had_to_allocate { defer bytes.buffer_destroy(backing); }
fmt.println("\nConvert []f16le (x2) to []u16 (x2), force_convert=true.");
res2, backing, had_to_allocate, err = bytes.buffer_convert_to_type(2, u16, f16le, b, true);
fmt.printf("res : %v\n", res2); // [1, 1]
fmt.printf("backing : %v\n", backing); // Buffer.buf points to `b` because it could be converted in-place.
fmt.printf("allocated: %v\n", had_to_allocate); // false
fmt.printf("err : %v\n", err); // false
if had_to_allocate { defer bytes.buffer_destroy(backing); }
```
Diffstat (limited to 'src/llvm_backend.cpp')
0 files changed, 0 insertions, 0 deletions