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

Go to the source code of this file.

Functions

int ck_token_span_mean_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 *spans, size_t span_elements, size_t span_count, float *output, size_t output_elements, size_t output_stride)
static int overlaps (const void *a, size_t a_bytes, const void *b, size_t b_bytes)
static int valid_region (const void *ptr, size_t elements, size_t bytes, size_t alignment)

Function Documentation

◆ ck_token_span_mean_checked_f32()

int ck_token_span_mean_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 * spans,
size_t span_elements,
size_t span_count,
float * output,
size_t output_elements,
size_t output_stride )

Definition at line 18 of file token_span_mean_checked.c.

22 {
23 if (!token_capacity || !valid_tokens || valid_tokens > token_capacity ||
24 !channels || !span_count || input_stride < channels ||
25 output_stride < channels || span_count > SIZE_MAX / 2)
26 return -2;
27 if (token_capacity - 1 > (SIZE_MAX - channels) / input_stride ||
28 input_elements < (token_capacity - 1) * input_stride + channels ||
29 span_elements < span_count * 2 ||
30 span_count - 1 > (SIZE_MAX - channels) / output_stride ||
31 output_elements < (span_count - 1) * output_stride + channels)
32 return -2;
33 if (!valid_region(input, input_elements, sizeof(float), _Alignof(float)) ||
34 !valid_region(spans, span_elements, sizeof(int32_t), _Alignof(int32_t)) ||
35 !valid_region(output, output_elements, sizeof(float), _Alignof(float)))
36 return -1;
37 if (overlaps(input, input_elements * sizeof(float), output,
38 output_elements * sizeof(float)) ||
39 overlaps(spans, span_elements * sizeof(int32_t), output,
40 output_elements * sizeof(float)))
41 return -1;
42
43 /* Validate every result before publishing any output. The second pass uses
44 the same order and leaves padding untouched. */
45 for (int pass = 0; pass < 2; ++pass) {
46 for (size_t s = 0; s < span_count; ++s) {
47 int32_t start = spans[2 * s], end = spans[2 * s + 1];
48 if (start < 0 || end <= start || (size_t)end > valid_tokens)
49 return -2;
50 for (size_t c = 0; c < channels; ++c) {
51 double sum = 0.0;
52 for (size_t t = (size_t)start; t < (size_t)end; ++t) {
53 float value = input[t * input_stride + c];
54 if (!isfinite(value)) return -1;
55 sum += (double)value;
56 }
57 float mean = (float)(sum / (double)(end - start));
58 if (!isfinite(mean)) return -1;
59 if (pass) output[s * output_stride + c] = mean;
60 }
61 }
62 }
63 return 0;
64}
static int valid_region(const void *ptr, size_t elements, size_t bytes, size_t alignment)
static int overlaps(const void *a, size_t a_bytes, const void *b, size_t b_bytes)
uint32_t end
Definition utf8.c:215
uint32_t start
Definition utf8.c:214

References end, overlaps(), start, and valid_region().

◆ overlaps()

int overlaps ( const void * a,
size_t a_bytes,
const void * b,
size_t b_bytes )
static

Definition at line 13 of file token_span_mean_checked.c.

13 {
14 uintptr_t x = (uintptr_t)a, y = (uintptr_t)b;
15 return x < y + b_bytes && y < x + a_bytes;
16}

Referenced by ck_token_span_mean_checked_f32().

◆ valid_region()

int valid_region ( const void * ptr,
size_t elements,
size_t bytes,
size_t alignment )
static

Definition at line 6 of file token_span_mean_checked.c.

7 {
8 if (!ptr || (uintptr_t)ptr % alignment || elements > SIZE_MAX / bytes)
9 return 0;
10 return (uintptr_t)ptr <= UINTPTR_MAX - elements * bytes;
11}

Referenced by ck_token_span_mean_checked_f32().