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

Go to the source code of this file.

Functions

static int checked_i32_region (const int32_t *ptr, size_t count)
int indexed_sum_rows_checked_f32 (const float *left, size_t left_elements, size_t left_stride, size_t left_rows, const float *right, size_t right_elements, size_t right_stride, size_t right_rows, const int32_t *left_indices, size_t left_index_elements, const int32_t *right_indices, size_t right_index_elements, float *output, size_t output_elements, size_t output_stride, size_t rows, size_t channels)
static int overlaps_bytes (const void *a, size_t a_bytes, const void *b, size_t b_bytes)

Function Documentation

◆ checked_i32_region()

int checked_i32_region ( const int32_t * ptr,
size_t count )
static

Definition at line 7 of file indexed_sum_rows_checked.c.

8{
9 return ptr && (uintptr_t)ptr % _Alignof(int32_t) == 0 &&
10 count <= SIZE_MAX / sizeof(int32_t) &&
11 (uintptr_t)ptr <= UINTPTR_MAX - count * sizeof(int32_t);
12}
const uint8_t size_t count
Definition true_bpe.h:211

References count.

Referenced by indexed_sum_rows_checked_f32().

◆ indexed_sum_rows_checked_f32()

int indexed_sum_rows_checked_f32 ( const float * left,
size_t left_elements,
size_t left_stride,
size_t left_rows,
const float * right,
size_t right_elements,
size_t right_stride,
size_t right_rows,
const int32_t * left_indices,
size_t left_index_elements,
const int32_t * right_indices,
size_t right_index_elements,
float * output,
size_t output_elements,
size_t output_stride,
size_t rows,
size_t channels )

Definition at line 21 of file indexed_sum_rows_checked.c.

28{
29 size_t left_span, right_span, output_span;
30 if (!ck_checked_span(left_rows, channels, left_stride, &left_span) ||
31 !ck_checked_span(right_rows, channels, right_stride, &right_span) ||
32 !ck_checked_span(rows, channels, output_stride, &output_span) ||
33 left_elements < left_span || right_elements < right_span ||
34 output_elements < output_span || left_index_elements < rows ||
35 right_index_elements < rows || left_rows > INT32_MAX ||
36 right_rows > INT32_MAX) return -2;
37 if (!ck_checked_region(left, left_span) ||
38 !ck_checked_region(right, right_span) ||
39 !ck_checked_region(output, output_span) ||
40 !checked_i32_region(left_indices, rows) ||
41 !checked_i32_region(right_indices, rows) ||
42 overlaps_bytes(left, left_span * sizeof(float), output,
43 output_span * sizeof(float)) ||
44 overlaps_bytes(right, right_span * sizeof(float), output,
45 output_span * sizeof(float)) ||
46 overlaps_bytes(left_indices, rows * sizeof(int32_t), output,
47 output_span * sizeof(float)) ||
48 overlaps_bytes(right_indices, rows * sizeof(int32_t), output,
49 output_span * sizeof(float))) return -1;
50
51 /* Preflight every row so rejection cannot expose a partial output. */
52 for (size_t row = 0; row < rows; ++row) {
53 const int32_t li = left_indices[row], ri = right_indices[row];
54 if (li < -1 || ri < -1 || (li == -1 && ri == -1) ||
55 (li >= 0 && (size_t)li >= left_rows) ||
56 (ri >= 0 && (size_t)ri >= right_rows)) return -2;
57 for (size_t channel = 0; channel < channels; ++channel) {
58 const float l = li < 0 ? 0.0f : left[(size_t)li * left_stride + channel];
59 const float r = ri < 0 ? 0.0f : right[(size_t)ri * right_stride + channel];
60 if (!isfinite(l) || !isfinite(r) ||
61 (li >= 0 && ri >= 0 && !isfinite(l + r))) return -3;
62 }
63 }
64 for (size_t row = 0; row < rows; ++row) {
65 const int32_t li = left_indices[row], ri = right_indices[row];
66 for (size_t channel = 0; channel < channels; ++channel) {
67 const float l = li < 0 ? 0.0f : left[(size_t)li * left_stride + channel];
68 const float r = ri < 0 ? 0.0f : right[(size_t)ri * right_stride + channel];
69 output[row * output_stride + channel] =
70 li < 0 ? r : (ri < 0 ? l : l + r);
71 }
72 }
73 return 0;
74}
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
static int ck_checked_region(const float *ptr, size_t n)
static int checked_i32_region(const int32_t *ptr, size_t count)
static int overlaps_bytes(const void *a, size_t a_bytes, const void *b, size_t b_bytes)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

References checked_i32_region(), ck_checked_region(), ck_checked_span(), left, overlaps_bytes(), and right.

◆ overlaps_bytes()

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

Definition at line 14 of file indexed_sum_rows_checked.c.

16{
17 const uintptr_t x = (uintptr_t)a, y = (uintptr_t)b;
18 return x < y + b_bytes && y < x + a_bytes;
19}

Referenced by indexed_sum_rows_checked_f32().