Skip to content

Instantly share code, notes, and snippets.

@hgomersall
Last active February 11, 2016 08:40
Show Gist options
  • Save hgomersall/22a19f245559d55f1ff6 to your computer and use it in GitHub Desktop.
Save hgomersall/22a19f245559d55f1ff6 to your computer and use it in GitHub Desktop.
Problem code with Valgrind on ARM
#include <stdio.h>
#include <stdint.h>
#define N_SUBVALS 4
#define BOUNDARY 4
int main()
{
// The code finds the next boundary alignment for each set of of 4
// values in the following list, and sums them all together.
uint32_t vals[] = {10,10,13,13,13,12,13,14,15,16,17,18,19,20,21,22};
uint32_t counter = 0;
uint32_t index = 0;
for (uint32_t i=0; i < 4; ++i){
uint32_t max_subval = 0;
for (uint32_t j=0; j < N_SUBVALS; ++j){
uint32_t val = vals[index];
if (val > max_subval){
max_subval = val;
}
index += 1;
}
// should get the next aligned value from the largest of the set
// of N_SUBVALS subvalues
// The max sub vals are: 13, 14, 18 and 22
// These align to: 16, 16, 20 and 24
uint32_t aligned_subval = (max_subval +
(BOUNDARY - (max_subval % BOUNDARY)) % BOUNDARY);
// 16 + 16 + 20 + 24 = 76
counter += aligned_subval;
}
// Should print 76
//
if (counter != 76){
printf("wrong final counter: %d\n", counter);
} else {
printf("correct final counter: %d\n", counter);
}
return 0;
}
@hgomersall
Copy link
Author

Run and tested with the following shell script:

set -o verbose

# System:
# ARM cortex A9
# valgrind 3.10.1
# gcc 5.2.0
# glibc 2.22
# kernel 3.19.0

gcc -o debug_works.os -c -std=c11 -mfpu=neon -g -Og test.c
gcc -o debug_works debug_works.os
gcc -o release_fail.os -c -std=c11 -mfpu=neon -g -O3 test.c
gcc -o release_fail release_fail.os

# Prints "wrong final counter: 71"
valgrind ./release_fail


# Prints "correct final counter: 76"
valgrind ./debug_works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment