← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
audio_snake_checked.c
Go to the documentation of this file.
3
4#include <math.h>
5
6static int snake_value(float x, float alpha, float *value) {
7 const float argument = alpha * x;
8 if (!isfinite(argument)) return 0;
9 const float sine = sinf(argument);
10 const float inverse = 1.0f / alpha;
11 const float square = sine * sine;
12 /* Preserve the FP32 multiplication before addition even when the model
13 * library is built with FMA enabled. */
14 volatile float scaled = inverse * square;
15 const float result = x + scaled;
16 if (!isfinite(sine) || !isfinite(inverse) || !isfinite(result)) return 0;
17 *value = result;
18 return 1;
19}
20
22 const float *input, size_t input_elements, size_t input_stride,
23 const float *alpha, size_t alpha_elements,
24 float *output, size_t output_elements, size_t output_stride,
25 size_t channels, size_t frames) {
26 size_t input_span, output_span;
27 if (!channels || !frames || input_stride < frames || output_stride < frames)
28 return -1;
29 if (!ck_checked_span(channels, frames, input_stride, &input_span) ||
30 !ck_checked_span(channels, frames, output_stride, &output_span))
31 return -2;
32 if (input_elements < input_span || output_elements < output_span ||
33 alpha_elements < channels) return -2;
34 if (!ck_checked_region(input, input_span) ||
35 !ck_checked_region(alpha, channels) ||
36 !ck_checked_region(output, output_span) ||
37 ck_checked_overlap(input, input_span, output, output_span) ||
38 ck_checked_overlap(alpha, channels, input, input_span) ||
39 ck_checked_overlap(alpha, channels, output, output_span))
40 return -1;
41
42 /* A preflight pass preserves all output, including valid cells, when a
43 * late element overflows. No heap or variable-length stack storage. */
44 for (size_t channel = 0; channel < channels; ++channel) {
45 const float a = alpha[channel];
46 if (!isfinite(a) || a == 0.0f) return -3;
47 for (size_t frame = 0; frame < frames; ++frame) {
48 const float x = input[channel * input_stride + frame];
49 float result;
50 if (!isfinite(x) || !snake_value(x, a, &result)) return -3;
51 }
52 }
53 for (size_t channel = 0; channel < channels; ++channel)
54 for (size_t frame = 0; frame < frames; ++frame) {
55 float result = 0.0f;
56 /* Preflight established that this evaluation is finite. */
57 (void)snake_value(input[channel * input_stride + frame],
58 alpha[channel], &result);
59 output[channel * output_stride + frame] = result;
60 }
61 return 0;
62}
int audio_snake_strided_f32_checked(const float *input, size_t input_elements, size_t input_stride, const float *alpha, size_t alpha_elements, float *output, size_t output_elements, size_t output_stride, size_t channels, size_t frames)
static int snake_value(float x, float alpha, float *value)
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 ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)