From 19721bb1c4de4622acea4bab4481c6f95f0f10ff Mon Sep 17 00:00:00 2001 From: Ethan Hardy <102838865+hardy-ethan@users.noreply.github.com> Date: Mon, 22 Dec 2025 20:20:18 -0500 Subject: [PATCH] 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 --- examples/c++/Max_array.cpp | 2 +- examples/c++/Max_array_(Optimized).cpp | 2 +- examples/c/Max_array.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/c++/Max_array.cpp b/examples/c++/Max_array.cpp index bc2ef8799..45ea1c6a6 100644 --- a/examples/c++/Max_array.cpp +++ b/examples/c++/Max_array.cpp @@ -1,4 +1,4 @@ -void maxArray(double* x, double* y) { +void maxArray(double* x, const double* y) { for (int i = 0; i < 65536; i++) { if (y[i] > x[i]) x[i] = y[i]; } diff --git a/examples/c++/Max_array_(Optimized).cpp b/examples/c++/Max_array_(Optimized).cpp index a6256e93f..aa7dfde1b 100644 --- a/examples/c++/Max_array_(Optimized).cpp +++ b/examples/c++/Max_array_(Optimized).cpp @@ -1,7 +1,7 @@ // Compile with -O3 -march=native to see autovectorization 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++) { x[i] = ((y[i] > x[i]) ? y[i] : x[i]); } diff --git a/examples/c/Max_array.c b/examples/c/Max_array.c index c1ab7c15b..e485502c3 100644 --- a/examples/c/Max_array.c +++ b/examples/c/Max_array.c @@ -1,4 +1,4 @@ -void maxArray(double* x, double* y) { +void maxArray(double* x, const double* y) { int i; for (i = 0; i < 65536; i++) {