Skip to content

Commit 3c6812f

Browse files
committed
Fix M0 math
1 parent 15d80a8 commit 3c6812f

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

shared-module/audioio/Mixer.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,28 @@ void audioio_mixer_reset_buffer(audioio_mixer_obj_t* self,
131131
}
132132
}
133133

134+
#pragma GCC push_options
135+
#pragma GCC optimize ("O0")
134136
uint32_t add8signed(uint32_t a, uint32_t b) {
135137
#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
136138
return __QADD8(a, b);
137139
#else
138140
uint32_t result = 0;
139141
for (int8_t i = 0; i < 4; i++) {
140-
int8_t ai = a >> (sizeof(int8_t) * i);
141-
int8_t bi = b >> (sizeof(int8_t) * i);
142+
int8_t ai = a >> (sizeof(int8_t) * 8 * i);
143+
int8_t bi = b >> (sizeof(int8_t) * 8 * i);
142144
int32_t intermediate = (int32_t) ai + bi;
143145
if (intermediate > CHAR_MAX) {
144146
intermediate = CHAR_MAX;
145147
} else if (intermediate < CHAR_MIN) {
146-
intermediate = CHAR_MIN;
148+
//intermediate = CHAR_MIN;
147149
}
148-
result |= ((int8_t) intermediate) >> (sizeof(int8_t) * i);
150+
result |= (((uint32_t) intermediate) & 0xff) << (sizeof(int8_t) * 8 * i);
149151
}
150152
return result;
151153
#endif
152154
}
155+
#pragma GCC pop_options
153156

154157
uint32_t add8unsigned(uint32_t a, uint32_t b) {
155158
#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
@@ -161,13 +164,13 @@ uint32_t add8unsigned(uint32_t a, uint32_t b) {
161164
#else
162165
uint32_t result = 0;
163166
for (int8_t i = 0; i < 4; i++) {
164-
uint8_t ai = a >> (sizeof(uint8_t) * i);
165-
uint8_t bi = b >> (sizeof(uint8_t) * i);
167+
int8_t ai = (a >> (sizeof(uint8_t) * 8 * i)) - 128;
168+
int8_t bi = (b >> (sizeof(uint8_t) * 8 * i)) - 128;
166169
int32_t intermediate = (int32_t) ai + bi;
167170
if (intermediate > UCHAR_MAX) {
168171
intermediate = UCHAR_MAX;
169172
}
170-
result |= ((uint8_t) intermediate) >> (sizeof(uint8_t) * i);
173+
result |= ((uint8_t) intermediate + 128) << (sizeof(uint8_t) * 8 * i);
171174
}
172175
return result;
173176
#endif
@@ -179,15 +182,15 @@ uint32_t add16signed(uint32_t a, uint32_t b) {
179182
#else
180183
uint32_t result = 0;
181184
for (int8_t i = 0; i < 2; i++) {
182-
int16_t ai = a >> (sizeof(int16_t) * i);
183-
int16_t bi = b >> (sizeof(int16_t) * i);
185+
int16_t ai = a >> (sizeof(int16_t) * 8 * i);
186+
int16_t bi = b >> (sizeof(int16_t) * 8 * i);
184187
int32_t intermediate = (int32_t) ai + bi;
185188
if (intermediate > SHRT_MAX) {
186189
intermediate = SHRT_MAX;
187190
} else if (intermediate < SHRT_MIN) {
188191
intermediate = SHRT_MIN;
189192
}
190-
result |= ((int16_t) intermediate) >> (sizeof(int16_t) * i);
193+
result |= (((uint32_t) intermediate) & 0xffff) << (sizeof(int16_t) * 8 * i);
191194
}
192195
return result;
193196
#endif
@@ -203,13 +206,13 @@ uint32_t add16unsigned(uint32_t a, uint32_t b) {
203206
#else
204207
uint32_t result = 0;
205208
for (int8_t i = 0; i < 2; i++) {
206-
uint16_t ai = a >> (sizeof(uint16_t) * i);
207-
uint16_t bi = b >> (sizeof(uint16_t) * i);
208-
uint32_t intermediate = (uint32_t) ai + bi;
209+
int16_t ai = (a >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
210+
int16_t bi = (b >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
211+
int32_t intermediate = (int32_t) ai + bi;
209212
if (intermediate > USHRT_MAX) {
210213
intermediate = USHRT_MAX;
211214
}
212-
result |= ((uint16_t) intermediate) >> (sizeof(int16_t) * i);
215+
result |= ((uint16_t) intermediate + 0x8000) << (sizeof(int16_t) * 8 * i);
213216
}
214217
return result;
215218
#endif

0 commit comments

Comments
 (0)