At 08:43 PM 3/8/2001 +0000, Nicholas Clark wrote:
>On Thu, Mar 08, 2001 at 11:31:06AM -0800, Hong Zhang wrote:
> > > Looks like they do operations with 16-bit integers. I'd as soon go with
> > > 32-bit ones--wastes a little space, but should be faster. (Except 
> where we
> > > should shift to 64-bit words)
> >
> > Using 32/31-bit requires general support of 64-bit arithmetics, for shift
> > and multiply. Without it, we have to use some extremely complicated
> > code to deal with multiply/division/shift, and we will lose the speed.
>
>unsigned 64 multiply of two unsigned 32 bit quantities is not hard if you
>have a 32 bit multiply instruction. (you use 4, to evaluate the partial
>products of splitting the operands into low and high 16 bits)
>I don't know how to do shift or divide. I don't see shift as hard.

I think most processors that do 32x32 multiply provide a way to get the 
64-bit result. Whether *we* can is another matter, of course, but if 
platform folks want to drop to assembly I'm fine with that.

> > Detecting overflow for 2's complement signed integer addtion/substract will
> > be easy:
> >   c = a + b;
> >   if (((c ^ a) & (c ^ b)) < 0) {
> >     /* overflow */
> >   }
> > The code is super-scalar and need only one branch. It is also fast to
> > generate boolean flag without using branch:
> >   int overflow = ((unsigned) ((c ^ a) & (c ^ b))) >> 31;
>
>Does the above work on 64 bit Irix?
>The overflow in c = a + b is undefined behaviour in C. Irix gave the
>bit value that was expected, but didn't execute subsequence > conditions
>in the way that would be expected by an assembly programmer.
>[However, I like the elegant sign bit testing logic you give.]

The potential vagaries of platform math is why I was thinking of 31-bit 
things, because then all you need do is:

   int overflow = 0x80000000 & (c = a + b)

which isn't quite so clever, but still not too bad. :)

                                        Dan

--------------------------------------"it's like this"-------------------
Dan Sugalski                          even samurai
[EMAIL PROTECTED]                         have teddy bears and even
                                      teddy bears get drunk

Reply via email to