Skip to content

Instantly share code, notes, and snippets.

@mansr
Created August 5, 2012 15:14
Show Gist options
  • Save mansr/3265340 to your computer and use it in GitHub Desktop.
Save mansr/3265340 to your computer and use it in GitHub Desktop.
/* gives wrong result at -O3 */
#include <stdio.h>
static void ac3_excite(int *excitep, int *band_psd,
int lowcomp, int is_lfe, int end)
{
int excite[1] = { 0 };
int band;
for (band = 0; band < end; band++) {
if (!(is_lfe && band == 6))
lowcomp = 2;
excite[band] = lowcomp;
if (!(is_lfe && band == 6)) {
if (band_psd[band] <= band_psd[band+1]) {
break;
}
}
}
excitep[0] = excite[0];
}
static int psd[2];
int main(void)
{
int excite;
ac3_excite(&excite, psd, 1, 0, 1);
printf("%d\n", excite);
return 0;
}
/* crashes at -O2 or higher */
int foo(char *a, unsigned b, int c)
{
return a[b + 1 - c];
}
int main(void)
{
char a = 0;
foo(&a, 0, 1);
return 0;
}
/* wrong result at -O3 */
#include <stdio.h>
float dprod(int *a, float *b, int n)
{
float sum = 0.0;
int i;
for (i = 0; i < n; i++)
sum += a[i] * b[i];
return sum;
}
int main(void)
{
int a[32];
float b[32];
float s;
int i;
for (i = 0; i < 32; i++) {
a[i] = i;
b[i] = i;
}
s = dprod(a, b, 32);
printf("%f\n", s);
return 0;
}
/*
wrong output at any optimisation level
correct output:
1 4
1 1
*/
#include <stdio.h>
#include <stdlib.h>
int foo(char *a, unsigned b, int c)
{
return a[b - c + 1];
}
int bar(char *a, int b, int c)
{
return a[b - c + 1];
}
int main(void)
{
char *a;
int b, c;
a = malloc(0x100000001);
a[ 0] = 1;
a[ 1] = 2;
a[ 0xfffffffe] = 3;
a[ 0xffffffff] = 4;
a[0x100000000] = 5;
b = foo(a, 0, 1);
c = foo(a, 0, 2);
printf("%d %d\n", b, c);
b = bar(a, 0, 1);
c = bar(a + 1, 0, 2);
printf("%d %d\n", b, c);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment