← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
frame_token_grid_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(const uint16_t *p, size_t elements)
6{
7 return p && (uintptr_t)p % sizeof(*p) == 0 &&
8 elements <= (UINTPTR_MAX - (uintptr_t)p)/sizeof(*p);
9}
10
11static int overlaps(const uint16_t *a, size_t n, const uint16_t *b, size_t m)
12{
13 return (uintptr_t)a < (uintptr_t)b+m*sizeof(*b) &&
14 (uintptr_t)b < (uintptr_t)a+n*sizeof(*a);
15}
16
17int ck_frame_token_grid_bf16_checked(const uint16_t *grid, const uint16_t *newline,
18 const uint16_t *separator, uint16_t *out, size_t input_elements, size_t output_elements,
19 size_t height, size_t width, size_t channels)
20{
21 if (!height || height > 64 || !width || width > 64 || !channels || channels > 4096 ||
22 input_elements != height*width*channels ||
23 output_elements != (height*(width+1)+1)*channels)
24 return -2;
25 if (!valid(grid,input_elements) || !valid(newline,channels) || !valid(separator,channels) ||
26 !valid(out,output_elements) || overlaps(out,output_elements,grid,input_elements) ||
27 overlaps(out,output_elements,newline,channels) || overlaps(out,output_elements,separator,channels))
28 return -1;
29 for (size_t y = 0; y < height; ++y) {
30 memcpy(out+y*(width+1)*channels,grid+y*width*channels,width*channels*sizeof(*grid));
31 memcpy(out+(y*(width+1)+width)*channels,newline,channels*sizeof(*newline));
32 }
33 memcpy(out+height*(width+1)*channels,separator,channels*sizeof(*separator));
34 return 0;
35}
int ck_frame_token_grid_bf16_checked(const uint16_t *grid, const uint16_t *newline, const uint16_t *separator, uint16_t *out, size_t input_elements, size_t output_elements, size_t height, size_t width, size_t channels)
static int overlaps(const uint16_t *a, size_t n, const uint16_t *b, size_t m)
static int valid(const uint16_t *p, size_t elements)