M
Mike Wahler
Christian Lande said:Hi all,
can anybody help me with the following code:
#include <stdio.h>
#include <emmintrin.h>
#define M 2
int main () {
int i;
__m128d *s, ts, td;
The header <emmintrin.h> and that type '__m128d'
are not part of standard C, and I don't know what
they are, but here are a few points:
double *p = malloc(M*sizeof(*p)); //A
You didn't #include <stdlib.h> for the prototype of
'malloc()', so a (C89) compiler will assume it returns
type 'int'. So the return value is type 'int' before
you assign it to the pointer 'p'. Very possible the
value has been corrupted.
// double p[2]; //B
This form will guarantee that the expression 'p' does
indeed evaluate to type 'double *' with a valid address
value. This (given that I've actually identified the
problem above) would be why this way works and above does
not.
if (p==NULL) return 0;
You might want to output some kind of message before
returning to give some visual clue of a problem.
p[0]=p[1]=1.0;
printf("%.3f %.3f\n", p[0], p[1]);
s = (__m128d *)p; //D
The language gives no guarantee that such a cast
will give a meaningful result. I must leave it
up to you to make sure it does (check your documentation).
if (s != p) return 0;
Same thing here: the language doesn't guarantee a
meaningful result for this comparison.
ts = *s;
This is perfectly valid as long as what's stored at
address 's' has a valid type '__m128d' value.
td = _mm_add_pd(ts, ts);
I can't comment on the nonstandard '_mm_add_pd()' function.
You might want to double check your documentation to ensure
you're using it correctly.
*s = td; //C
This is OK as long as 'td' contains a valid type '__m128d' value.
"D"printf("%.3f %.3f\n", p[0], p[1]);
}
Line "C" causes a Segmentation Fault and I don't know why. If I replace line
"A" with line "B" it works correctly. Something must be wrong with line
Possibly. But I can't know since what you're doing is not standard C.
It could easily be valid for your implemenation. Double check your
documentation.
maybe somebody knows what?
My best guess is what I stated above: lacking a prototype, your
compiler is converting the address returned from 'malloc()' to
type 'int', thus corrupting it.
-Mike