Graphics Reference
In-Depth Information
typedef uint32 uint64[2];
void Uadd64(uint64 x, uint64 y, uint64 res)
{
uint32 a = x[1] + y[1];
// Compute sum of higher 32 bits
uint32 b = x[0] + y[0];
// Compute sum of lower 32 bits
if (b < x[0]) a++;
// Carry if low sum overflowed
res[0] = b;
res[1] = a;
}
Extended-precision multiplication of two 32-bit values resulting in a 64-bit value
can be performed as follows. Let x and y be the two 32-bit values. Write them as
x
x h 2 16
y h 2 16
=
+
x l and y
=
+
y l . Then the product xy is equivalent to
( x h 2 16
x l )( y h 2 16
x h y h 2 32
x l y h )2 16
xy
=
+
+
y l )
=
+
( x h y l
+
+
x l y l .
×
32-bit multiplications that
in this scenario are assumed hardware supported. The code for 32
The products x h y h , x h y l , x l y h , and x l y l are all 16
16
×
32
64-bit
multiplication then becomes:
void Umult32to64(uint32 x, uint32 y, uint64 res)
{
uint16 xh=x>>16,xl=x&0xffff;
uint16 yh=y>>16,yl=y&0xffff;
uint32a=xh*yh;
uint32b=xh*yl;
uint32c=xl*yh;
uint32d=xl*yl;
d=d+(b<<16);
if (d < (b << 16)) a++;
d=d+(c<<16);
if (d < (c << 16)) a++;
a=a+(b>>16)+(c>>16);
res[0] = d;
res[1] = a;
}
Signed operations can be implemented analogously. Algorithms for implementing
extended-precision arithmetic are well described in several sources; for example, in
[Knuth97].
 
Search WWH ::




Custom Search