divide by 48

Nov 6, 2012 at 8:14am
Hello

I got the following C (not c++) code:

1
2
#define PTR( m_idx )		( &( arr[ ( m_idx ) ].byte[ 0 ] ) )
#define IDX( m_ptr )		(uint32_t)( ( ( m_ptr ) - ( PTR( 0 ) ) ) / 48 ) 


arr is an array of structs wich are 48 bytes in size

It works great, but the only thing we have a problem with is the division by 48.
It takes to long. In the past we used 32 so we could shift the bits to accomplish the division. But we need more space in our structs, but 64 is way to much. This is an embedded application with 8kB of RAM.

My question is: Is there any fast algorithm to divide a number by 48? If it helps... 48 = 32 + 16.

Thank you!
Nov 6, 2012 at 10:54am
You could try to cast the dividend to unsigned and see if the compiler is able to make some optimization.
Nov 6, 2012 at 12:46pm
48 = 3*16 so you could divide by 16 (a shift) and then divide by 3
There are discussions on dividion by 3 e.g.
http://stackoverflow.com/questions/171301/whats-the-fastest-way-to-divide-an-integer-by-3
Your compiler may already have an optimisation for it.
Nov 6, 2012 at 2:00pm
thnx!

I will take a good look at that.
Topic archived. No new replies allowed.