divide by 48


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

#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!
You could try to cast the dividend to unsigned and see if the compiler is able to make some optimization.
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.
Your compiler may already have an optimisation for it.

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