3DSoftware.com > Programming > Integers > Page 4
Integers  Page 4
 
Wide Integers
 
Simd Addition
 
To add two WideInteger numbers, A and B, each number having 8 digit packets, begin by loading the digits into two sets of simd registers:
 
A7   A6   A5   A4
B7   B6   B5   B4
A3   A2   A1   A0
B3   B2   B1   B0
 
The digit packets are indexed with n = 0 for the least significant digit. In this example they increase in significance from right to left.
 
Invoke the PADDD instruction on both sets of registers to add B into A:   A = A + B
 
 
+
  A7 A6 A5 A4
B7 B6 B5 B4
  A3 A2 A1 A0
B3 B2 B1 B0

 =   A7 A6 A5 A4   A3 A2 A1 A0
 
After invoking PADDD, the new digit packets of A have carries that need to be propagated. Now put a copy of this new A into the other simd registers with MOVDQU invoked for each set of registers:
 
A7 A6 A5 A4
A7 A6 A5 A4
  A3 A2 A1 A0
A3 A2 A1 A0
 
Right shift the bits of each digit of the copy of A, 30 bits, with PSRLD to create the carries C:
 
A7 A6 A5 A4
C7 C6 C5 C4
  A3 A2 A1 A0
C3 C2 C1 C0
 
Clear bit 30 of each A digit by ANDing each digit packet with 3FFFFFFFh (using PAND), then store (save) A0 to the first 16 bytes of an output stream memory buffer with MOVNTDQ.
 
Then shift the digit packets, overwriting A0 which did not have any carries added to it and has already been stored.  To do that, right shift the digit packets 4 bytes with PSRLDQ after copying A7…A4 to another register with MOVDQU.  Left shift that other register 12 bytes with PSLLDQ and then POR it to the right-shifted low order digit packets:
 
 0
C7
 A7 A6 A5
 C6 C5 C4
  A4 A3 A2 A1
C3 C2 C1 C0
 
A temporary digit packet A8 will recieve possible overflow:
 
A8 A7 A6 A5
C7 C6 C5 C4
  A4 A3 A2 A1
C3 C2 C1 C0
 
Add the carries into these digits of A with PADDD.
 
Repeat these operations to perform all possible carries. When finished, invoke MFENCE and then extract the new A digits from the output stream memory buffer.
 
Note: In assembly language, AMD uses Microsoft (backwards) packet ordering to be compatible with Microsoft Windows.
 
—  Page 4  —
 « Page 3 Contents Page 5 » 
 
Copyright © 2008 by 3D Software. All rights reserved.
3D Software, P.O. Box 221190, Sacramento CA 95822 USA
www.3DSoftware.com     Contact us
Thursday, 20-Nov-2008 12:11:29 GMT