← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
audio_prosody_upsample_checked.c
Go to the documentation of this file.
3
4#include <stdint.h>
5#include <string.h>
6
8 const float *input, size_t input_elements, size_t input_stride,
9 float *output, size_t output_elements, size_t output_stride,
10 size_t channels, size_t input_frames, size_t factor,
11 size_t output_frames) {
12 size_t input_span, output_span;
13 if (!channels || !input_frames || !factor)
15 if (input_frames > SIZE_MAX / factor)
17 if (input_frames * factor != output_frames ||
18 !ck_checked_span(channels, input_frames, input_stride, &input_span) ||
19 !ck_checked_span(channels, output_frames, output_stride, &output_span))
21 if (input_elements < input_span || output_elements < output_span)
23 if (!ck_checked_region(input, input_span) ||
24 !ck_checked_region(output, output_span) ||
25 ck_checked_overlap(input, input_span, output, output_span))
27 for (size_t channel = 0; channel < channels; ++channel)
28 if (!ck_checked_finite(input + channel * input_stride, input_frames))
30 for (size_t channel = 0; channel < channels; ++channel)
31 for (size_t frame = 0; frame < output_frames; ++frame)
32 output[channel * output_stride + frame] =
33 input[channel * input_stride + frame / factor];
35}
36
38 const float *input, size_t input_elements, size_t input_stride,
39 const float *weight, size_t weight_elements,
40 const float *bias, size_t bias_elements,
41 float *output, size_t output_elements, size_t output_stride,
42 float *scratch, size_t scratch_elements,
43 size_t channels, size_t input_frames, size_t kernel_size,
44 size_t stride, size_t padding, size_t output_padding,
45 size_t output_frames) {
46 size_t input_span, output_span, weight_need, scratch_need, geometry;
47 if (!channels || !input_frames || !kernel_size || !stride ||
48 output_padding >= stride || !output_frames)
50 if (input_frames - 1 > SIZE_MAX / stride)
52 geometry = (input_frames - 1) * stride;
53 if (geometry > SIZE_MAX - kernel_size ||
54 geometry + kernel_size > SIZE_MAX - output_padding ||
55 padding > SIZE_MAX / 2)
57 geometry += kernel_size + output_padding;
58 if (2 * padding >= geometry || geometry - 2 * padding != output_frames)
60 if (!ck_checked_span(channels, input_frames, input_stride, &input_span) ||
61 !ck_checked_span(channels, output_frames, output_stride, &output_span) ||
62 channels > SIZE_MAX / kernel_size ||
63 channels > SIZE_MAX / output_frames)
65 weight_need = channels * kernel_size;
66 scratch_need = channels * output_frames;
67 if (weight_need > SIZE_MAX / sizeof(float) ||
68 scratch_need > SIZE_MAX / sizeof(float))
70 if (input_elements < input_span || weight_elements < weight_need ||
71 bias_elements < channels || output_elements < output_span ||
72 scratch_elements < scratch_need)
74 if (!ck_checked_region(input, input_span) ||
75 !ck_checked_region(weight, weight_need) ||
76 !ck_checked_region(bias, channels) ||
77 !ck_checked_region(output, output_span) ||
78 !ck_checked_region(scratch, scratch_need) ||
79 ck_checked_overlap(output, output_span, input, input_span) ||
80 ck_checked_overlap(output, output_span, weight, weight_need) ||
81 ck_checked_overlap(output, output_span, bias, channels) ||
82 ck_checked_overlap(output, output_span, scratch, scratch_need) ||
83 ck_checked_overlap(scratch, scratch_need, input, input_span) ||
84 ck_checked_overlap(scratch, scratch_need, weight, weight_need) ||
85 ck_checked_overlap(scratch, scratch_need, bias, channels))
87 if (!ck_checked_finite(weight, weight_need) ||
88 !ck_checked_finite(bias, channels))
90 for (size_t channel = 0; channel < channels; ++channel)
91 if (!ck_checked_finite(input + channel * input_stride, input_frames))
93 for (size_t channel = 0; channel < channels; ++channel) {
94 float *row = scratch + channel * output_frames;
95 for (size_t frame = 0; frame < output_frames; ++frame)
96 row[frame] = bias[channel];
97 for (size_t frame = 0; frame < input_frames; ++frame)
98 for (size_t tap = 0; tap < kernel_size; ++tap) {
99 size_t target = frame * stride + tap;
100 if (target >= padding && target - padding < output_frames)
101 row[target - padding] = fmaf(
102 input[channel * input_stride + frame],
103 weight[channel * kernel_size + tap],
104 row[target - padding]);
105 }
106 if (!ck_checked_finite(row, output_frames))
108 }
109 for (size_t channel = 0; channel < channels; ++channel)
110 memcpy(output + channel * output_stride,
111 scratch + channel * output_frames,
112 output_frames * sizeof(float));
114}
int audio_conv_transpose1d_depthwise_channel_major_f32_checked(const float *input, size_t input_elements, size_t input_stride, const float *weight, size_t weight_elements, const float *bias, size_t bias_elements, float *output, size_t output_elements, size_t output_stride, float *scratch, size_t scratch_elements, size_t channels, size_t input_frames, size_t kernel_size, size_t stride, size_t padding, size_t output_padding, size_t output_frames)
int audio_upsample_nearest_channel_major_f32_checked(const float *input, size_t input_elements, size_t input_stride, float *output, size_t output_elements, size_t output_stride, size_t channels, size_t input_frames, size_t factor, size_t output_frames)
static int ck_checked_finite(const float *ptr, size_t n)
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)