← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
concat_features_bf16_checked.c
Go to the documentation of this file.
1#include <stddef.h>
2#include <stdint.h>
3#include <string.h>
4
5static int valid_span(const uint16_t *data, size_t elements)
6{
7 return data && (uintptr_t)data % _Alignof(uint16_t) == 0 &&
8 elements <= (UINTPTR_MAX - (uintptr_t)data) / sizeof(*data);
9}
10
11static int overlaps(const uint16_t *out, size_t out_elements,
12 const uint16_t *in, size_t in_elements)
13{
14 return (uintptr_t)out < (uintptr_t)in + in_elements * sizeof(*in) &&
15 (uintptr_t)in < (uintptr_t)out + out_elements * sizeof(*out);
16}
17
18int ck_concat_features_bf16_checked(const uint16_t *left, const uint16_t *right,
19 uint16_t *out, size_t rows, size_t left_width,
20 size_t right_width, size_t left_elements,
21 size_t right_elements, size_t out_elements,
22 size_t out_width)
23{
24 if (!rows || !left_width || !right_width || left_width > SIZE_MAX - right_width)
25 return -2;
26 const size_t width = left_width + right_width;
27 if (out_width != width || rows > SIZE_MAX / width || left_elements != rows * left_width ||
28 right_elements != rows * right_width || out_elements != rows * width)
29 return -2;
30 if (!valid_span(left, left_elements) || !valid_span(right, right_elements) ||
31 !valid_span(out, out_elements) || overlaps(out, out_elements, left, left_elements) ||
32 overlaps(out, out_elements, right, right_elements))
33 return -1;
34 for (size_t row = 0; row < rows; ++row) {
35 memcpy(out + row * width, left + row * left_width, left_width * sizeof(*out));
36 memcpy(out + row * width + left_width, right + row * right_width, right_width * sizeof(*out));
37 }
38 return 0;
39}
static int valid_span(const uint16_t *data, size_t elements)
int ck_concat_features_bf16_checked(const uint16_t *left, const uint16_t *right, uint16_t *out, size_t rows, size_t left_width, size_t right_width, size_t left_elements, size_t right_elements, size_t out_elements, size_t out_width)
static int overlaps(const uint16_t *out, size_t out_elements, const uint16_t *in, size_t in_elements)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141