u32 LP PHY Qdiv Roundup (u32 dividend, u32 divisor, u8 precision)
- If divisor == 0
- Return 0
- Otherwise
- Set quotient to dividend / divisor
- Set remainder to dividend % divisor
- Set (u32) rbit to the bitwise AND of divisor with 1
- Set (u32) roundup to divisor right shifted by 1 plus rbit
- While precision != 0
- Left shift quotient by 1
If remainder >= roundup
- increase quotient by 1
- Set remainder to (remainder - roundup) left shifted by 1 plus rbit
- Otherwise
- Left shift remainder by 1
- Decrease precision by 1
If remainder >= roundup
- Increase quotient by 1
- return quotient
(this is really an integer version of round(((double)dividend / (double)divisor) * (double)(1 << precision)))