Hi,
I am looking for some fast imlementations for basic integer math in XC. Like abs(), min() or max(). Is there any library shipping with XC/XDE that I have not noticed?
math.h and fastmath.h are disabled in XC - and they deal with floating point. But there should be somewhere some basic routines that are better than my if/else non sense.
Thanks
Marcus
Integer math lib in XC??
-
- XCore Addict
- Posts: 216
- Joined: Wed Feb 10, 2010 10:26 am
-
- Member++
- Posts: 28
- Joined: Thu Dec 10, 2009 7:25 pm
`labs' in the C library will do abs for integers, and you can just include stdlib.h in your XC program to get this to work.Interactive_Matter wrote:I am looking for some fast imlementations for basic integer math in XC. Like abs(), min() or max(). Is there any library shipping with XC/XDE that I have not noticed?
min and max are usually done as macros (with the irritating side-effect of evaluating the arguments twice) or as an inline function. I don't believe that there are standard library functions for these (but I could well be wrong..)
-
- XCore Addict
- Posts: 216
- Joined: Wed Feb 10, 2010 10:26 am
Thanks,
understood the abs thing. The min/max macros puzzle me a bit but I can work with it ;)
Marcus
understood the abs thing. The min/max macros puzzle me a bit but I can work with it ;)
Marcus
-
- Respected Member
- Posts: 279
- Joined: Fri Dec 11, 2009 1:34 pm
Here's a fairly small abs function I've used. Don't know how it compares to the library version.
Code: Select all
// Absolute of signed integer
static __inline int FASTABS(int x)
{
int sign;
sign = x >> (sizeof(int) * 8 - 1);
x ^= sign;
x -= sign;
return x;
}
-
- XCore Addict
- Posts: 216
- Joined: Wed Feb 10, 2010 10:26 am
Any recomendation for a good resource for those kinds of algorithms (must admint I am more familiar with Java - there you do not think about this very often ;) )
-
- Respected Member
- Posts: 318
- Joined: Tue Dec 15, 2009 12:46 am
The following page is a good source for various tricks:Interactive_Matter wrote:Any recomendation for a good resource for those kinds of algorithms (must admint I am more familiar with Java - there you do not think about this very often ;) )
http://graphics.stanford.edu/~seander/bithacks.html
I'd also highly recommend Hacker's Delight.