Fix #8349: Add const to Max Array C/C++ examples where applicable. (#8350)

As noted by Matt during his [Advent of Compiler Optimizations - Episode
20: Vectorization
[0:55]](https://www.youtube.com/watch?v=d68x8TF7XJs&t=55s), `const` can
and should be introduced here.

We don't need the elements of `y` to be modifiable.

Closes #8349 

<!-- THIS COMMENT IS INVISIBLE IN THE FINAL PR, BUT FEEL FREE TO REMOVE
IT
Thanks for taking the time to improve CE. We really appreciate it.
Before opening the PR, please make sure that the tests & linter pass
their checks,
  by running `make check`.
In the best case scenario, you are also adding tests to back up your
changes,
  but don't sweat it if you don't. We can discuss them at a later date.
Feel free to append your name to the CONTRIBUTORS.md file
Thanks again, we really appreciate this!
-->
This commit is contained in:
Ethan Hardy
2025-12-22 20:20:18 -05:00
committed by GitHub
parent 8dbc7ea62b
commit 19721bb1c4
3 changed files with 3 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
void maxArray(double* x, double* y) { void maxArray(double* x, const double* y) {
for (int i = 0; i < 65536; i++) { for (int i = 0; i < 65536; i++) {
if (y[i] > x[i]) x[i] = y[i]; if (y[i] > x[i]) x[i] = y[i];
} }

View File

@@ -1,7 +1,7 @@
// Compile with -O3 -march=native to see autovectorization // Compile with -O3 -march=native to see autovectorization
typedef double *__attribute__((aligned(64))) aligned_double; typedef double *__attribute__((aligned(64))) aligned_double;
void maxArray(aligned_double __restrict x, aligned_double __restrict y) { void maxArray(aligned_double __restrict x, const aligned_double __restrict y) {
for (int i = 0; i < 65536; i++) { for (int i = 0; i < 65536; i++) {
x[i] = ((y[i] > x[i]) ? y[i] : x[i]); x[i] = ((y[i] > x[i]) ? y[i] : x[i]);
} }

View File

@@ -1,4 +1,4 @@
void maxArray(double* x, double* y) { void maxArray(double* x, const double* y) {
int i; int i;
for (i = 0; i < 65536; i++) { for (i = 0; i < 65536; i++) {