Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: [Caml-list] Integer arithmetic: mod
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-08-06 (01:07)
From: John Gerard Malecki <johnm@a...>
Subject: Re: [Caml-list] Integer arithmetic: mod
Does anyone know if the hardware implementation of integer division
and/or remainder is faster because the returned value from remainder
is sometimes negative?  Maybe its slower but everyone does it the same
way for backwards compatibility?

One reason for the hardware remainder to be positive is that it allows
for easier compiler optimizations.  If the remainder is always
positive then the following transformations are always available:

	 M /   (2**N) == M asr  N
	 M mod (2**N) == M land (2**N-1)

I guess it doesn't much matter as this optimization is normally
applied to array indices which usually are non-negative anyway.

PS - i'm not sure if this trick also works with 1s complement
     representation but when was the last time anyone built one of
     those machines?
Bug reports:  FAQ:
To unsubscribe, mail  Archives: