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

Go to the source code of this file.

Functions

int affine_qdq_u8_f32 (const float *input, size_t input_elements, float *output, size_t output_elements, size_t elements, float scale, size_t zero_point)
int ctc_top1_confidence_f32 (const float *input, size_t input_elements, int32_t *ids, size_t ids_elements, float *confidence, size_t confidence_elements, size_t classes, size_t steps)
int image_nearest2x_chw_f32 (const float *input, size_t input_elements, float *output, size_t output_elements, size_t rows, size_t columns)
int image_sigmoid_f32 (const float *input, size_t input_elements, float *output, size_t output_elements, size_t elements)

Function Documentation

◆ affine_qdq_u8_f32()

int affine_qdq_u8_f32 ( const float * input,
size_t input_elements,
float * output,
size_t output_elements,
size_t elements,
float scale,
size_t zero_point )

Definition at line 38 of file image_plane_ops_checked.c.

40 {
41 if (!elements || input_elements != elements || output_elements != elements ||
42 !isfinite(scale) || scale <= 0 || scale > FLT_MAX / 255.0f || zero_point > 255 ||
43 !ck_checked_region(input, elements) || !ck_checked_region(output, elements) ||
44 ck_checked_overlap(input, elements, output, elements))
45 return -1;
46 const int zp = (int)zero_point;
47 size_t start = 0;
48#if defined(__AVX2__)
49 const __m256i exponent = _mm256_set1_epi32(0x7f800000);
50 const size_t vector_count = elements & ~(size_t)7;
51 for (size_t i = 0; i < vector_count; i += 8) {
52 const __m256i bits = _mm256_castps_si256(_mm256_loadu_ps(input + i));
53 if (_mm256_movemask_epi8(_mm256_cmpeq_epi32(_mm256_and_si256(bits, exponent), exponent)))
54 return -1;
55 }
56 if (!ck_checked_finite(input + vector_count, elements - vector_count))
57 return -1;
58 const __m256 qscale = _mm256_set1_ps(scale);
59 const __m256 lo = _mm256_set1_ps((float)-zp), hi = _mm256_set1_ps((float)(255-zp));
60 const __m256 positive_zero = _mm256_setzero_ps();
61 for (; start < vector_count; start += 8) {
62 const __m256 value = _mm256_div_ps(_mm256_loadu_ps(input + start), qscale);
63 const __m256 bounded = _mm256_max_ps(lo, _mm256_min_ps(hi, value));
64 const __m256 rounded = _mm256_round_ps(bounded, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
65 // Scalar conversion through int produces positive zero, including at negative ties.
66 const __m256 zero = _mm256_cmp_ps(rounded, positive_zero, _CMP_EQ_OQ);
67 _mm256_storeu_ps(output + start, _mm256_mul_ps(_mm256_blendv_ps(rounded, positive_zero, zero), qscale));
68 }
69#else
70 if (!ck_checked_finite(input, elements))
71 return -1;
72#endif
73 for (size_t i = start; i < elements; ++i) {
74 const float x = fmaxf(-zp, fminf(255-zp, input[i] / scale));
75 const float base = floorf(x);
76 int q = (int)base;
77 const float fraction = x - base;
78 q += fraction > 0.5f || (fraction == 0.5f && q % 2 != 0);
79 output[i] = q * scale;
80 }
81 return 0;
82}
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_region(const float *ptr, size_t n)
static int ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)
static float rounded(float x)
uint32_t start
Definition utf8.c:214

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), rounded(), and start.

◆ ctc_top1_confidence_f32()

int ctc_top1_confidence_f32 ( const float * input,
size_t input_elements,
int32_t * ids,
size_t ids_elements,
float * confidence,
size_t confidence_elements,
size_t classes,
size_t steps )

Definition at line 84 of file image_plane_ops_checked.c.

87 {
88 size_t n;
89 const float *id_span = (const float *)(const void *)ids;
90 if (classes > INT32_MAX || !ck_checked_span(classes, steps, steps, &n) ||
91 input_elements != n || ids_elements != steps || confidence_elements != steps ||
92 !ck_checked_region(input, n) || !ck_checked_region(id_span, steps) ||
93 !ck_checked_region(confidence, steps) ||
94 ck_checked_overlap(input, n, id_span, steps) ||
95 ck_checked_overlap(input, n, confidence, steps) ||
96 ck_checked_overlap(id_span, steps, confidence, steps) ||
97 !ck_checked_finite(input, n))
98 return -1;
99 for (size_t t = 0; t < steps; ++t) {
100 float best = input[t];
101 int32_t id = 0;
102 for (size_t c = 1; c < classes; ++c) {
103 const float x = input[c * steps + t];
104 if (x > best) {
105 best = x;
106 id = (int32_t)c;
107 }
108 }
109 float sum = 0;
110 for (size_t c = 0; c < classes; ++c)
111 sum += expf(input[c * steps + t] - best);
112 ids[t] = id;
113 confidence[t] = expf(best - (best + logf(sum)));
114 }
115 return 0;
116}
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
const int32_t * ids
Definition tokenizer.h:447
int32_t id
Definition tokenizer.h:317

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), id, and ids.

◆ image_nearest2x_chw_f32()

int image_nearest2x_chw_f32 ( const float * input,
size_t input_elements,
float * output,
size_t output_elements,
size_t rows,
size_t columns )

Definition at line 7 of file image_plane_ops_checked.c.

9 {
10 size_t n;
11 if (!ck_checked_span(rows, columns, columns, &n) || n > SIZE_MAX / 4 ||
12 input_elements != n || output_elements != 4 * n ||
13 !ck_checked_region(input, n) || !ck_checked_region(output, 4 * n) ||
14 ck_checked_overlap(input, n, output, 4 * n))
15 return -1;
16 for (size_t r = 0; r < rows; ++r) {
17 const float *src = input + r * columns;
18 float *dst = output + r * columns * 4;
19 for (size_t x = 0; x < columns; ++x) {
20 dst[2*x] = dst[2*x+1] = src[x];
21 dst[2*columns+2*x] = dst[2*columns+2*x+1] = src[x];
22 }
23 }
24 return 0;
25}

References ck_checked_overlap(), ck_checked_region(), and ck_checked_span().

◆ image_sigmoid_f32()

int image_sigmoid_f32 ( const float * input,
size_t input_elements,
float * output,
size_t output_elements,
size_t elements )

Definition at line 27 of file image_plane_ops_checked.c.

28 {
29 if (!elements || input_elements != elements || output_elements != elements ||
30 !ck_checked_region(input, elements) || !ck_checked_region(output, elements) ||
31 ck_checked_overlap(input, elements, output, elements))
32 return -1;
33 for (size_t i = 0; i < elements; ++i)
34 output[i] = 1.0f / (1.0f + expf(-input[i]));
35 return 0;
36}

References ck_checked_overlap(), and ck_checked_region().