Re: MATH_MOD: Include in GLib?
- From: Jan Kratochvil <rcpt-gtk-devel-list AT gnome org jankratochvil net>
- To: Owen Taylor <otaylor redhat com>
- Cc: drlion-dated-1091219837 02c327 teepee ath cx, gtk-devel-list gnome org
- Subject: Re: MATH_MOD: Include in GLib?
- Date: Mon, 26 Jul 2004 16:01:14 +0200
Hi Owen,
On Mon, 26 Jul 2004 14:23:42 +0200, Owen Taylor wrote:
...
> You also usually want the equivalent division at the same time:
/* Provide C modulo (x % m) with defined result for m<0
* always in the range of 0..(m-1).
* `x' and `m' must be integers. `m' must != 0.
*/
#define G_MOD(x, m) ((x) >= 0 ? (x) % (m) : (m) - 1 - ((-(x) - 1) % (m)))
/* Provide C division (x / m) compatible with G_MOD() even for m<0:
* x == G_DIV(x, m) * m + G_MOD(x, m)
* `x' and `m' must be integers. `m' must != 0.
*/
#define G_DIV(x, m) ((x) / (m) - ((x) >= 0 || 0 == (-(x)) % (m) ? 0 : 1))
> It's worth noting that powers of two can be optimized for this:
>
> MOD(x, 1024) = x >> 10
> DIV(x, 1024) = x & 1023
x & ((1<<10)-1) would be IMO more clear
Regards,
Lace
--
Jan Kratochvil; Captive: free r/w NTFS Filesystem; http://www.jankratochvil.net/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]