Integer math lib in XC??

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Integer math lib in XC??

Post by Interactive_Matter »

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


User avatar
daveg
Member++
Posts: 28
Joined: Thu Dec 10, 2009 7:25 pm

Post by daveg »

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?
`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.

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..)
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Post by Interactive_Matter »

Thanks,

understood the abs thing. The min/max macros puzzle me a bit but I can work with it ;)

Marcus
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

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;
}
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Post by Interactive_Matter »

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 ;) )
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

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 ;) )
The following page is a good source for various tricks:

http://graphics.stanford.edu/~seander/bithacks.html

I'd also highly recommend Hacker's Delight.