Repository navigation
ltpf: add Arm Helium (MVE) SIMD backend #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /****************************************************************************** | ||
| * | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at: | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| ******************************************************************************/ | ||
|
|
||
| /* | ||
| * Arm Helium (M-Profile Vector Extension, MVE) backend for the LTPF | ||
| * pitch-detection hotspot, targeting Armv8.1-M cores (e.g. Cortex-M55). | ||
| * | ||
| * `dot()` accumulates int16 x int16 products into a 64-bit integer and | ||
| * applies the rounding `(v + 32) >> 6`. MVE `vmlaldavaq_s16` | ||
| * performs an 8-lane int16 x int16 -> int64 multiply-accumulate across the | ||
| * vector; the integer sum and the subsequent rounding are therefore | ||
| * identical to the scalar reference, i.e. the result is strictly bit-exact. | ||
| * | ||
| * This mirrors the NEON (`ltpf_neon.h`) and Arm DSP (`ltpf_arm.h`) backends | ||
| * and hooks the same `dot` override point. Include this file before the | ||
| * NEON/DSP backends in `ltpf.c` so its definitions take precedence on MVE | ||
| * targets. | ||
| */ | ||
|
|
||
| #if (__ARM_FEATURE_MVE & 1) && !defined(TEST_ARM) && !defined(TEST_NEON) \ | ||
| || defined(TEST_MVE) | ||
|
|
||
| #ifndef TEST_MVE | ||
| #include <arm_mve.h> | ||
| #endif /* TEST_MVE */ | ||
|
|
||
|
|
||
| /** | ||
| * Return dot product of 2 vectors | ||
| * a, b, n The 2 vectors of size `n` (> 0 and <= 128) | ||
| * return round( sum( a[i] * b[i] ) / 64 ), as float | ||
| * | ||
| * The size `n` of vectors is a multiple of 16 (so also of 8). | ||
| * | ||
| * The intrinsic is used instead of relying on the compiler's | ||
| * auto-vectorizer: at normal optimization levels for an MVE target, | ||
| * e.g. -O3 -march=armv8.1-m.main+dsp+mve.fp+fp.dp, gcc does not | ||
| * vectorize the scalar loop, while clang vectorizes it only at | ||
| * width 4. The intrinsic guarantees the 8-lane vmlaldavaq_s16 form, | ||
| * same as the NEON and Arm backends. | ||
| */ | ||
| #ifndef dot | ||
|
|
||
| LC3_HOT static inline float mve_dot(const int16_t *a, const int16_t *b, int n) | ||
| { | ||
| int64_t v = 0; | ||
|
|
||
| for (int i = 0; i < (n >> 3); i++) { | ||
| int16x8_t va = vldrhq_s16(a); a += 8; | ||
| int16x8_t vb = vldrhq_s16(b); b += 8; | ||
| v = vmlaldavaq_s16(v, va, vb); | ||
| } | ||
|
|
||
| int32_t v32 = (v + (1 << 5)) >> 6; | ||
| return (float)v32; | ||
| } | ||
|
|
||
| #ifndef TEST_MVE | ||
| #define dot mve_dot | ||
| #endif | ||
|
|
||
| #endif /* dot */ | ||
|
|
||
| #endif /* __ARM_FEATURE_MVE & 1 */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /****************************************************************************** | ||
| * | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at: | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| ******************************************************************************/ | ||
|
|
||
| #include "mve.h" | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdint.h> | ||
| #include <stdlib.h> | ||
|
|
||
| /* -------------------------------------------------------------------------- */ | ||
|
|
||
| #define TEST_MVE | ||
| #include <ltpf.c> | ||
|
|
||
| void lc3_put_bits_generic(lc3_bits_t *a, unsigned b, int c) | ||
| { (void)a, (void)b, (void)c; } | ||
|
|
||
| unsigned lc3_get_bits_generic(struct lc3_bits *a, int b) | ||
| { return (void)a, (void)b, 0; } | ||
|
|
||
| /* -------------------------------------------------------------------------- */ | ||
|
|
||
| static int check_dot() | ||
| { | ||
| int16_t x[200]; | ||
| for (int i = 0; i < 200; i++) | ||
| x[i] = rand() & 0xffff; | ||
|
|
||
| float y = dot(x, x+3, 128); | ||
| float y_mve = mve_dot(x, x+3, 128); | ||
| if (y != y_mve) | ||
| return -1; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static void mve_correlate( | ||
| const int16_t *a, const int16_t *b, int n, float *y, int nc) | ||
| { | ||
| for (const float *ye = y + nc; y < ye; ) | ||
| *(y++) = mve_dot(a, b--, n); | ||
| } | ||
|
|
||
| static int check_correlate() | ||
| { | ||
| int16_t alignas(4) a[500], b[500]; | ||
| float y[100], y_mve[100]; | ||
|
|
||
| for (int i = 0; i < 500; i++) { | ||
| a[i] = rand() & 0xffff; | ||
| b[i] = rand() & 0xffff; | ||
| } | ||
|
|
||
| correlate(a, b+200, 128, y, 100); | ||
| mve_correlate(a, b+200, 128, y_mve, 100); | ||
| if (memcmp(y, y_mve, 100 * sizeof(*y)) != 0) | ||
| return -1; | ||
|
|
||
| correlate(a, b+199, 128, y, 99); | ||
| mve_correlate(a, b+199, 128, y_mve, 99); | ||
| if (memcmp(y, y_mve, 99 * sizeof(*y)) != 0) | ||
| return -1; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int check_ltpf(void) | ||
| { | ||
| int ret; | ||
|
|
||
| if ((ret = check_dot()) < 0) | ||
| return ret; | ||
|
|
||
| if ((ret = check_correlate()) < 0) | ||
| return ret; | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at: | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| test_mve_src += \ | ||
| $(TEST_DIR)/mve/test_mve.c \ | ||
| $(TEST_DIR)/mve/ltpf_mve.c \ | ||
| $(SRC_DIR)/tables.c | ||
|
|
||
| test_mve_include += $(SRC_DIR) | ||
| test_mve_ldlibs += m | ||
|
|
||
| $(eval $(call add-bin,test_mve)) | ||
|
|
||
| test_mve: $(test_mve_bin) | ||
| @echo " RUN $(notdir $<)" | ||
| $(V)$< | ||
|
|
||
| test: test_mve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /****************************************************************************** | ||
| * | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at: | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| ******************************************************************************/ | ||
|
|
||
| #if (__ARM_FEATURE_MVE & 1) | ||
|
|
||
| #include <arm_mve.h> | ||
|
|
||
| #else | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
|
|
||
| /* ---------------------------------------------------------------------------- | ||
| * Integer | ||
| * -------------------------------------------------------------------------- */ | ||
|
|
||
| typedef struct { int16_t e[8]; } int16x8_t; | ||
|
|
||
|
|
||
| /** | ||
| * Load | ||
| */ | ||
|
|
||
| __attribute__((unused)) | ||
| static int16x8_t vldrhq_s16(const int16_t *p) | ||
| { | ||
| return (int16x8_t){ { p[0], p[1], p[2], p[3], | ||
| p[4], p[5], p[6], p[7] } }; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Multiply-accumulate across vector, into a 64-bit accumulator | ||
| */ | ||
|
|
||
| __attribute__((unused)) | ||
| static int64_t vmlaldavaq_s16(int64_t a, int16x8_t b, int16x8_t c) | ||
| { | ||
| for (int i = 0; i < 8; i++) | ||
| a += (int32_t)b.e[i] * c.e[i]; | ||
|
|
||
| return a; | ||
| } | ||
|
|
||
|
|
||
| #endif /* __ARM_FEATURE_MVE */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /****************************************************************************** | ||
| * | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at: | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| ******************************************************************************/ | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| int check_ltpf(void); | ||
|
|
||
| int main() | ||
| { | ||
| int r, ret = 0; | ||
|
|
||
| printf("Checking LTPF MVE... "); fflush(stdout); | ||
| printf("%s\n", (r = check_ltpf()) == 0 ? "OK" : "Failed"); | ||
| ret = ret || r; | ||
|
|
||
| return ret; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.