Skip to content

Instantly share code, notes, and snippets.

@djg
Created August 14, 2017 01:19
Show Gist options
  • Save djg/d820789288f2bd2a60ba5aaa3b901808 to your computer and use it in GitHub Desktop.
Save djg/d820789288f2bd2a60ba5aaa3b901808 to your computer and use it in GitHub Desktop.
Adding two numbers in one register
Square by Pulse does this trick of adding two 8.8 fixed numbers in
"one 40-bit register" in the inner loop of it's pin-mapping routine. I
wonder if this is a known bit-twiddling trick documented some where?
(It feels like Lamport's 1976 paper where u is a 23 bit number)
Say we have u and v in 8.8 fixed point format and we want to increment
those by some signed deltas du and dv also in 8.8 fixed point.
I'll only consider a negative du because that seems to be an
interesting case in the code. Eg. let u = v = 64 = 64.00 = 0x4000 and
du -2 = -2.0 = 0xFE00, dv = 2 = 2.0 = 0x0200
Square sets up the 40-bit delta increment in CL:EBP registers as:
0xFFFFFFFE00 - 40-bit signed-extend du
+ 0x0200000000 - 40-bit dv << 24
--------------
= 0x01FFFFFE00
and the 40-bit value in BL:EDX registers as:
0x0000004000 - 40-bit u & 0xFFFF
+ 0x4000000000 - 40-bit v << 24
+ 0x0000800000
--------------
= 0x4000804000
Addition proceeds as:
0x4000804000 64.00 64.00
+ 0x01FFFFFE00
--------------
0x4200803E00 66.00 62.00
+ 0x01FFFFFE00
--------------
0x4400803C00 68.00 60.00
+ 0x01FFFFFE00
--------------
0x4600803A00 70.00 58.00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment