← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
last_valid_row_checked.c File Reference
#include <math.h>
#include <stdint.h>
#include <stddef.h>

Go to the source code of this file.

Functions

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)
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)

Function Documentation

◆ ck_last_valid_row_checked_f32()

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 )

Definition at line 18 of file last_valid_row_checked.c.

22 {
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)

References region_ok(), and regions_overlap().

◆ region_ok()

int region_ok ( const void * ptr,
size_t count,
size_t width )
static

Definition at line 5 of file last_valid_row_checked.c.

5 {
6 return ptr && (uintptr_t)ptr % width == 0 && count <= SIZE_MAX / width &&
7 (uintptr_t)ptr <= UINTPTR_MAX - count * width;
8}
const uint8_t size_t count
Definition true_bpe.h:211

References count.

Referenced by ck_last_valid_row_checked_f32().

◆ regions_overlap()

int regions_overlap ( const void * left,
size_t left_bytes,
const void * right,
size_t right_bytes )
static

Definition at line 10 of file last_valid_row_checked.c.

11 {
12 uintptr_t a = (uintptr_t)left, b = (uintptr_t)right;
13 return a < b + right_bytes && b < a + left_bytes;
14}
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

References left, and right.

Referenced by ck_last_valid_row_checked_f32().