aboutsummaryrefslogtreecommitdiffstats
path: root/source/audio/audio_simd.cpp
blob: 18a512eb4a1ae203bf436611ed7dc36f7852cbec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
internal void simd_gain_ramp_stereo(f32 *dst, const f32 *src, u32 frames, f32 g0, f32 g1)
{
	f32 step = (frames > 0) ? (g1 - g0) / (f32)frames : 0.0f;
	u32 frame = 0;

#if SIMD_NEON || SIMD_SSE
	// NOTE: one vector holds two stereo frames: {L0 R0 L1 R1}, so the gain lanes are {g g g+s g+s}.
	// we do 4 frames per iteration (two vectors) to keep both fma pipes busy.
	if(g0 == g1) {
		f32x4 gain = f32x4_set1(g0);
		for(; frame + 4 <= frames; frame += 4) {
			f32x4 a = f32x4_load(src + frame * 2 + 0);
			f32x4 b = f32x4_load(src + frame * 2 + 4);
			f32x4_store(dst + frame * 2 + 0, f32x4_mul(a, gain));
			f32x4_store(dst + frame * 2 + 4, f32x4_mul(b, gain));
		}
	} else {
		f32x4 gain_a = f32x4_make(g0, g0, g0 + step, g0 + step);
		f32x4 gain_b = f32x4_make(g0 + 2 * step, g0 + 2 * step, g0 + 3 * step, g0 + 3 * step);
		f32x4 delta  = f32x4_set1(4 * step);
		for(; frame + 4 <= frames; frame += 4) {
			f32x4 a = f32x4_load(src + frame * 2 + 0);
			f32x4 b = f32x4_load(src + frame * 2 + 4);
			f32x4_store(dst + frame * 2 + 0, f32x4_mul(a, gain_a));
			f32x4_store(dst + frame * 2 + 4, f32x4_mul(b, gain_b));
			gain_a = f32x4_add(gain_a, delta);
			gain_b = f32x4_add(gain_b, delta);
		}
	}
#endif

	for(; frame < frames; ++frame) {
		f32 gain           = g0 + step * (f32)frame;
		dst[frame * 2 + 0] = src[frame * 2 + 0] * gain;
		dst[frame * 2 + 1] = src[frame * 2 + 1] * gain;
	}
}

internal void simd_gain_ramp_copy_stereo(f32 *dst, const f32 *src, u32 frames, f32 g, f32 step)
{
	u32 frame = 0;
#if SIMD_NEON || SIMD_SSE
	f32x4 gain_a = f32x4_make(g, g, g + step, g + step);
	f32x4 gain_b = f32x4_make(g + 2 * step, g + 2 * step, g + 3 * step, g + 3 * step);
	f32x4 delta  = f32x4_set1(4 * step);
	for(; frame + 4 <= frames; frame += 4) {
		f32x4 a = f32x4_load(src + frame * 2 + 0);
		f32x4 b = f32x4_load(src + frame * 2 + 4);
		f32x4_store(dst + frame * 2 + 0, f32x4_mul(a, gain_a));
		f32x4_store(dst + frame * 2 + 4, f32x4_mul(b, gain_b));
		gain_a = f32x4_add(gain_a, delta);
		gain_b = f32x4_add(gain_b, delta);
	}
#endif
	for(; frame < frames; ++frame) {
		f32 gain           = g + step * (f32)frame;
		dst[frame * 2 + 0] = src[frame * 2 + 0] * gain;
		dst[frame * 2 + 1] = src[frame * 2 + 1] * gain;
	}
}

internal f32 simd_peak(const f32 *samples, u32 count)
{
	u32 index = 0;
	f32 peak  = 0.0f;

#if SIMD_NEON || SIMD_SSE
	f32x4 acc_a = f32x4_set1(0.0f);
	f32x4 acc_b = f32x4_set1(0.0f);
	for(; index + 8 <= count; index += 8) {
		acc_a = f32x4_max(acc_a, f32x4_abs(f32x4_load(samples + index + 0)));
		acc_b = f32x4_max(acc_b, f32x4_abs(f32x4_load(samples + index + 4)));
	}
	peak = f32x4_hmax(f32x4_max(acc_a, acc_b));
#endif

	for(; index < count; ++index) {
		f32 value = fabsf(samples[index]);
		peak      = Max(peak, value);
	}
	return peak;
}

internal void simd_mono_window(f32 *out, const f32 *stereo, const f32 *window, u32 frames)
{
	u32 frame = 0;

#if SIMD_NEON
	// NOTE: vld2q deinterleaves for free: val[0] = 4 lefts, val[1] = 4 rights
	f32x4 half = f32x4_set1(0.5f);
	for(; frame + 4 <= frames; frame += 4) {
		float32x4x2_t lr   = vld2q_f32(stereo + frame * 2);
		f32x4         mono = f32x4_mul(f32x4_add(lr.val[0], lr.val[1]), half);
		f32x4_store(out + frame, f32x4_mul(mono, f32x4_load(window + frame)));
	}
#elif SIMD_SSE
	f32x4 half = f32x4_set1(0.5f);
	for(; frame + 4 <= frames; frame += 4) {
		f32x4 a     = f32x4_load(stereo + frame * 2 + 0); // L0 R0 L1 R1
		f32x4 b     = f32x4_load(stereo + frame * 2 + 4); // L2 R2 L3 R3
		f32x4 left  = _mm_shuffle_ps(a, b, _MM_SHUFFLE(2, 0, 2, 0));
		f32x4 right = _mm_shuffle_ps(a, b, _MM_SHUFFLE(3, 1, 3, 1));
		f32x4 mono  = f32x4_mul(f32x4_add(left, right), half);
		f32x4_store(out + frame, f32x4_mul(mono, f32x4_load(window + frame)));
	}
#endif

	for(; frame < frames; ++frame) {
		out[frame] = 0.5f * (stereo[frame * 2] + stereo[frame * 2 + 1]) * window[frame];
	}
}

internal void simd_power(f32 *out, const f32 *re, const f32 *im, u32 count)
{
	u32 index = 0;
#if SIMD_NEON || SIMD_SSE
	for(; index + 4 <= count; index += 4) {
		f32x4 r = f32x4_load(re + index);
		f32x4 i = f32x4_load(im + index);
		f32x4_store(out + index, f32x4_madd(f32x4_mul(r, r), i, i));
	}
#endif
	for(; index < count; ++index) {
		out[index] = re[index] * re[index] + im[index] * im[index];
	}
}