D
David Mathog
Do any of the current or upcoming C language standards provide support
to help a compiler recognize vectors and then generate SIMD
operations? For instance, consider this code snippet:
#define ALEN 128
unsigned char a[ALEN];
unsigned char b[ALEN];
int i;
/* arrays are initialized (not shown) */
for (i=0;i<ALEN;i++){
b += a;
}
One would hope that a compiler could recognize that as a vector
operation and generate code to take advantage of it on a given target
that supports SIMD operations. This gets trickier when converted to a
function:
addABvector(unsigned char *a, unsigned char *b, int len){
int i;
for (i=0;i<len;i++){
b += a;
}
}
Here while a and b may be "vectors" there is nothing to show that they
are aligned optimally, or that len will be large, so the compiler
cannot easily know which way it should optimize this (for long aligned
vectors, or short unaligned data). If we try to tell the compiler to
make a distinction by length, for instance, I am guessing that most
compilers would just optimize the test out of existence, for instance,
in this case:
addABvector(unsigned char *a, unsigned char *b, int len){
int i;
if(len<ISMINLENGTHVECTOR){
for (i=0;i<len;i++){ b += a; }
}
else {
for (i=0;i<len;i++){ b += a; }
}
}
Finally, what if saturating math is required? Then the loop becomes
something like this:
for (i=0;i<len;i++){
b += a;
if(b < a)b=UCHAR_MAX; /* or any other test for
saturation, all of which have a conditional */
}
I believe some processors have saturating math operations in their
"normal" instruction set, and there are definitely saturating add
operations in the SIMD instruction sets. But in order to use these
the compiler has to deduce from the logic of the loop that a
saturating add is the desired result.
There are certainly other facets, but clues the compiler would need to
tell it when it should use vector instructions would seem to be
minimally:
1. a method to indicate when math operations are saturating.
2. a method to indicate when memory structures must be aligned on
some 2^N byte boundary.
3. a method to indicate the allowed size of an array.
Is any of this present in a standard C language variant, or is the
only way to achieve this to use compiler specific pragmas and
intrinsics?
Thanks,
David Mathog
to help a compiler recognize vectors and then generate SIMD
operations? For instance, consider this code snippet:
#define ALEN 128
unsigned char a[ALEN];
unsigned char b[ALEN];
int i;
/* arrays are initialized (not shown) */
for (i=0;i<ALEN;i++){
b += a;
}
One would hope that a compiler could recognize that as a vector
operation and generate code to take advantage of it on a given target
that supports SIMD operations. This gets trickier when converted to a
function:
addABvector(unsigned char *a, unsigned char *b, int len){
int i;
for (i=0;i<len;i++){
b += a;
}
}
Here while a and b may be "vectors" there is nothing to show that they
are aligned optimally, or that len will be large, so the compiler
cannot easily know which way it should optimize this (for long aligned
vectors, or short unaligned data). If we try to tell the compiler to
make a distinction by length, for instance, I am guessing that most
compilers would just optimize the test out of existence, for instance,
in this case:
addABvector(unsigned char *a, unsigned char *b, int len){
int i;
if(len<ISMINLENGTHVECTOR){
for (i=0;i<len;i++){ b += a; }
}
else {
for (i=0;i<len;i++){ b += a; }
}
}
Finally, what if saturating math is required? Then the loop becomes
something like this:
for (i=0;i<len;i++){
b += a;
if(b < a)b=UCHAR_MAX; /* or any other test for
saturation, all of which have a conditional */
}
I believe some processors have saturating math operations in their
"normal" instruction set, and there are definitely saturating add
operations in the SIMD instruction sets. But in order to use these
the compiler has to deduce from the logic of the loop that a
saturating add is the desired result.
There are certainly other facets, but clues the compiler would need to
tell it when it should use vector instructions would seem to be
minimally:
1. a method to indicate when math operations are saturating.
2. a method to indicate when memory structures must be aligned on
some 2^N byte boundary.
3. a method to indicate the allowed size of an array.
Is any of this present in a standard C language variant, or is the
only way to achieve this to use compiler specific pragmas and
intrinsics?
Thanks,
David Mathog