Skip to content

Instantly share code, notes, and snippets.

@miura1729
Last active December 15, 2020 08:34
Show Gist options
  • Save miura1729/4ef3a2310a2095d34c4ae7d183914f25 to your computer and use it in GitHub Desktop.
Save miura1729/4ef3a2310a2095d34c4ae7d183914f25 to your computer and use it in GitHub Desktop.
def add(x, y)
hc = ((x & 0x0f0f0f0f) + (y & 0x0f0f0f0f)) & 0xf0f0f0f0
hc |= (((x & 0xf0f0f0f0) + (y & 0xf0f0f0f0)) & 0xf0f0f0f0f)
c = x + y
if c & 0xf > 0x9 or hc & 0x10 != 0 then
c = c - 0xa + 0x10
end
if c & 0xf0 > 0x90 or hc & 0x100 != 0 then
c = c - 0xa0 + 0x100
end
if c & 0xf00 > 0x900 or hc & 0x1000 != 0 then
c = c - 0xa00 + 0x1000
end
if c & 0xf000 > 0x9000 or hc & 0x10000 != 0 then
c = c - 0xa000 + 0x10000
end
if c & 0xf0000 > 0x90000 or hc & 0x100000 != 0 then
c = c - 0xa0000 + 0x100000
end
if c & 0xf0000 > 0x900000 or hc & 0x1000000 != 0 then
c = c - 0xa00000 + 0x1000000
end
if c & 0xf000000 > 0x9000000 or hc & 0x10000000 != 0 then
c = c - 0xa000000 + 0x10000000
end
if c & 0xf000000 > 0x90000000 or hc & 0x100000000 != 0 then
c = c - 0xa0000000 + 0x100000000
end
c
end
def add8(x)
x = add(x & 0x0f0f0f0f, (x >> 4) & 0x0f0f0f0f)
x = add(x & 0x00ff00ff, (x >> 8) & 0x00ff00ff)
x = add(x & 0x0000ffff, (x >> 16) & 0x0000ffff)
x
end
p add8(0x01020304).to_s(16)
p add8(0x02040816).to_s(16)
p add8(0x12345678).to_s(16)
p add8(0).to_s(16)
p add8(0x99999999).to_s(16)
#p add(0x11, 0x22).to_s(16)
#p add(0x12, 0x29).to_s(16)
#p add(0x13, 0x92).to_s(16)
#p add(0x14, 0x26).to_s(16)
#p add(0x84, 0x16).to_s(16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment