← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
last_valid_row_checked.c
Go to the documentation of this file.
1#include <math.h>
2#include <stdint.h>
3#include <stddef.h>
4
5static int region_ok(const void *ptr, size_t count, size_t width) {
6 return ptr && (uintptr_t)ptr % width == 0 && count <= SIZE_MAX / width &&
7 (uintptr_t)ptr <= UINTPTR_MAX - count * width;
8}
9
10static int regions_overlap(const void *left, size_t left_bytes,
11 const void *right, size_t right_bytes) {
12 uintptr_t a = (uintptr_t)left, b = (uintptr_t)right;
13 return a < b + right_bytes && b < a + left_bytes;
14}
15
16/* Select the final valid token. The span is checked as a semantic assertion,
17 * so an externally supplied, in-bounds but incorrect span cannot redirect it. */
19 const float *input, size_t input_elements, size_t input_stride,
20 size_t token_capacity, size_t valid_tokens, size_t channels,
21 const int32_t *span, size_t span_elements,
22 float *output, size_t output_elements) {
23 if (!token_capacity || !valid_tokens || valid_tokens > token_capacity ||
24 valid_tokens > INT32_MAX || !channels || input_stride < channels ||
25 (token_capacity - 1) > (SIZE_MAX - channels) / input_stride ||
26 input_elements < (token_capacity - 1) * input_stride + channels ||
27 span_elements < 2 || output_elements < channels)
28 return -2;
29 if (!region_ok(input, input_elements, sizeof(float)) ||
30 !region_ok(span, span_elements, sizeof(int32_t)) ||
31 !region_ok(output, output_elements, sizeof(float)))
32 return -1;
33 if (regions_overlap(input, input_elements * sizeof(float),
34 output, output_elements * sizeof(float)) ||
35 regions_overlap(span, span_elements * sizeof(int32_t),
36 output, output_elements * sizeof(float)))
37 return -1;
38 if (span[0] != (int32_t)(valid_tokens - 1) ||
39 span[1] != (int32_t)valid_tokens)
40 return -2;
41 const float *last = input + (valid_tokens - 1) * input_stride;
42 for (size_t c = 0; c < channels; ++c)
43 if (!isfinite(last[c])) return -1;
44 for (size_t c = 0; c < channels; ++c)
45 output[c] = last[c];
46 return 0;
47}
static int region_ok(const void *ptr, size_t count, size_t width)
static int regions_overlap(const void *left, size_t left_bytes, const void *right, size_t right_bytes)
int ck_last_valid_row_checked_f32(const float *input, size_t input_elements, size_t input_stride, size_t token_capacity, size_t valid_tokens, size_t channels, const int32_t *span, size_t span_elements, float *output, size_t output_elements)
const char * left
Definition true_bpe.h:140
const uint8_t size_t count
Definition true_bpe.h:211
const char const char * right
Definition true_bpe.h:141