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

Go to the source code of this file.

Functions

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)
 

Function Documentation

◆ audio_snake_strided_f32_checked()

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 
)

Definition at line 21 of file audio_snake_checked.c.

25 {
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}
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)

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

◆ snake_value()

static int snake_value ( float  x,
float  alpha,
float *  value 
)
static

Definition at line 6 of file audio_snake_checked.c.

6 {
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}

Referenced by audio_snake_strided_f32_checked().