int128.nim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. ## This module is for compiler internal use only. For reliable error
  2. ## messages and range checks, the compiler needs a data type that can
  3. ## hold all from `low(BiggestInt)` to `high(BiggestUInt)`, This
  4. ## type is for that purpose.
  5. from std/math import trunc
  6. when defined(nimPreviewSlimSystem):
  7. import std/assertions
  8. type
  9. Int128* = object
  10. udata: array[4, uint32]
  11. template sdata(arg: Int128, idx: int): int32 =
  12. # udata and sdata was supposed to be in a union, but unions are
  13. # handled incorrectly in the VM.
  14. cast[ptr int32](arg.udata[idx].unsafeAddr)[]
  15. # encoding least significant int first (like LittleEndian)
  16. const
  17. Zero* = Int128(udata: [0'u32, 0, 0, 0])
  18. One* = Int128(udata: [1'u32, 0, 0, 0])
  19. Ten* = Int128(udata: [10'u32, 0, 0, 0])
  20. Min = Int128(udata: [0'u32, 0, 0, 0x80000000'u32])
  21. Max = Int128(udata: [high(uint32), high(uint32), high(uint32), uint32(high(int32))])
  22. NegOne* = Int128(udata: [0xffffffff'u32, 0xffffffff'u32, 0xffffffff'u32, 0xffffffff'u32])
  23. template low*(t: typedesc[Int128]): Int128 = Min
  24. template high*(t: typedesc[Int128]): Int128 = Max
  25. proc `$`*(a: Int128): string
  26. proc toInt128*[T: SomeInteger | bool](arg: T): Int128 =
  27. {.noSideEffect.}:
  28. result = Zero
  29. when T is bool: result.sdata(0) = int32(arg)
  30. elif T is SomeUnsignedInt:
  31. when sizeof(arg) <= 4:
  32. result.udata[0] = uint32(arg)
  33. else:
  34. result.udata[0] = uint32(arg and T(0xffffffff))
  35. result.udata[1] = uint32(arg shr 32)
  36. elif sizeof(arg) <= 4:
  37. result.sdata(0) = int32(arg)
  38. if arg < 0: # sign extend
  39. result.sdata(1) = -1
  40. result.sdata(2) = -1
  41. result.sdata(3) = -1
  42. else:
  43. let tmp = int64(arg)
  44. result.udata[0] = uint32(tmp and 0xffffffff)
  45. result.sdata(1) = int32(tmp shr 32)
  46. if arg < 0: # sign extend
  47. result.sdata(2) = -1
  48. result.sdata(3) = -1
  49. template isNegative(arg: Int128): bool =
  50. arg.sdata(3) < 0
  51. proc bitconcat(a, b: uint32): uint64 =
  52. (uint64(a) shl 32) or uint64(b)
  53. proc toInt64*(arg: Int128): int64 =
  54. if isNegative(arg):
  55. assert(arg.sdata(3) == -1, "out of range")
  56. assert(arg.sdata(2) == -1, "out of range")
  57. else:
  58. assert(arg.sdata(3) == 0, "out of range")
  59. assert(arg.sdata(2) == 0, "out of range")
  60. cast[int64](bitconcat(arg.udata[1], arg.udata[0]))
  61. proc toInt64Checked*(arg: Int128; onError: int64): int64 =
  62. if isNegative(arg):
  63. if arg.sdata(3) != -1 or arg.sdata(2) != -1:
  64. return onError
  65. else:
  66. if arg.sdata(3) != 0 or arg.sdata(2) != 0:
  67. return onError
  68. return cast[int64](bitconcat(arg.udata[1], arg.udata[0]))
  69. proc toInt32*(arg: Int128): int32 =
  70. if isNegative(arg):
  71. assert(arg.sdata(3) == -1, "out of range")
  72. assert(arg.sdata(2) == -1, "out of range")
  73. assert(arg.sdata(1) == -1, "out of range")
  74. else:
  75. assert(arg.sdata(3) == 0, "out of range")
  76. assert(arg.sdata(2) == 0, "out of range")
  77. assert(arg.sdata(1) == 0, "out of range")
  78. arg.sdata(0)
  79. proc toInt16*(arg: Int128): int16 =
  80. if isNegative(arg):
  81. assert(arg.sdata(3) == -1, "out of range")
  82. assert(arg.sdata(2) == -1, "out of range")
  83. assert(arg.sdata(1) == -1, "out of range")
  84. else:
  85. assert(arg.sdata(3) == 0, "out of range")
  86. assert(arg.sdata(2) == 0, "out of range")
  87. assert(arg.sdata(1) == 0, "out of range")
  88. int16(arg.sdata(0))
  89. proc toInt8*(arg: Int128): int8 =
  90. if isNegative(arg):
  91. assert(arg.sdata(3) == -1, "out of range")
  92. assert(arg.sdata(2) == -1, "out of range")
  93. assert(arg.sdata(1) == -1, "out of range")
  94. else:
  95. assert(arg.sdata(3) == 0, "out of range")
  96. assert(arg.sdata(2) == 0, "out of range")
  97. assert(arg.sdata(1) == 0, "out of range")
  98. int8(arg.sdata(0))
  99. proc toInt*(arg: Int128): int =
  100. when sizeof(int) == 4:
  101. cast[int](toInt32(arg))
  102. else:
  103. cast[int](toInt64(arg))
  104. proc toUInt64*(arg: Int128): uint64 =
  105. assert(arg.udata[3] == 0)
  106. assert(arg.udata[2] == 0)
  107. bitconcat(arg.udata[1], arg.udata[0])
  108. proc toUInt32*(arg: Int128): uint32 =
  109. assert(arg.udata[3] == 0)
  110. assert(arg.udata[2] == 0)
  111. assert(arg.udata[1] == 0)
  112. arg.udata[0]
  113. proc toUInt16*(arg: Int128): uint16 =
  114. assert(arg.udata[3] == 0)
  115. assert(arg.udata[2] == 0)
  116. assert(arg.udata[1] == 0)
  117. uint16(arg.udata[0])
  118. proc toUInt8*(arg: Int128): uint8 =
  119. assert(arg.udata[3] == 0)
  120. assert(arg.udata[2] == 0)
  121. assert(arg.udata[1] == 0)
  122. uint8(arg.udata[0])
  123. proc toUInt*(arg: Int128): uint =
  124. when sizeof(int) == 4:
  125. cast[uint](toInt32(arg))
  126. else:
  127. cast[uint](toInt64(arg))
  128. proc castToInt64*(arg: Int128): int64 =
  129. ## Conversion to int64 without range check.
  130. cast[int64](bitconcat(arg.udata[1], arg.udata[0]))
  131. proc castToUInt64*(arg: Int128): uint64 =
  132. ## Conversion to uint64 without range check.
  133. cast[uint64](bitconcat(arg.udata[1], arg.udata[0]))
  134. proc addToHex(result: var string; arg: uint32) =
  135. for i in 0..<8:
  136. let idx = (arg shr ((7-i) * 4)) and 0xf
  137. result.add "0123456789abcdef"[idx]
  138. proc addToHex*(result: var string; arg: Int128) =
  139. var i = 3
  140. while i >= 0:
  141. result.addToHex(arg.udata[i])
  142. i -= 1
  143. proc toHex*(arg: Int128): string =
  144. result = ""
  145. result.addToHex(arg)
  146. proc inc*(a: var Int128, y: uint32 = 1) =
  147. a.udata[0] += y
  148. if unlikely(a.udata[0] < y):
  149. a.udata[1].inc
  150. if unlikely(a.udata[1] == 0):
  151. a.udata[2].inc
  152. if unlikely(a.udata[2] == 0):
  153. a.udata[3].inc
  154. doAssert(a.sdata(3) != low(int32), "overflow")
  155. proc cmp*(a, b: Int128): int =
  156. let tmp1 = cmp(a.sdata(3), b.sdata(3))
  157. if tmp1 != 0: return tmp1
  158. let tmp2 = cmp(a.udata[2], b.udata[2])
  159. if tmp2 != 0: return tmp2
  160. let tmp3 = cmp(a.udata[1], b.udata[1])
  161. if tmp3 != 0: return tmp3
  162. let tmp4 = cmp(a.udata[0], b.udata[0])
  163. return tmp4
  164. proc `<`*(a, b: Int128): bool =
  165. cmp(a, b) < 0
  166. proc `<=`*(a, b: Int128): bool =
  167. cmp(a, b) <= 0
  168. proc `==`*(a, b: Int128): bool =
  169. if a.udata[0] != b.udata[0]: return false
  170. if a.udata[1] != b.udata[1]: return false
  171. if a.udata[2] != b.udata[2]: return false
  172. if a.udata[3] != b.udata[3]: return false
  173. return true
  174. proc bitnot*(a: Int128): Int128 =
  175. result = Zero
  176. result.udata[0] = not a.udata[0]
  177. result.udata[1] = not a.udata[1]
  178. result.udata[2] = not a.udata[2]
  179. result.udata[3] = not a.udata[3]
  180. proc bitand*(a, b: Int128): Int128 =
  181. result = Zero
  182. result.udata[0] = a.udata[0] and b.udata[0]
  183. result.udata[1] = a.udata[1] and b.udata[1]
  184. result.udata[2] = a.udata[2] and b.udata[2]
  185. result.udata[3] = a.udata[3] and b.udata[3]
  186. proc bitor*(a, b: Int128): Int128 =
  187. result = Zero
  188. result.udata[0] = a.udata[0] or b.udata[0]
  189. result.udata[1] = a.udata[1] or b.udata[1]
  190. result.udata[2] = a.udata[2] or b.udata[2]
  191. result.udata[3] = a.udata[3] or b.udata[3]
  192. proc bitxor*(a, b: Int128): Int128 =
  193. result = Zero
  194. result.udata[0] = a.udata[0] xor b.udata[0]
  195. result.udata[1] = a.udata[1] xor b.udata[1]
  196. result.udata[2] = a.udata[2] xor b.udata[2]
  197. result.udata[3] = a.udata[3] xor b.udata[3]
  198. proc `shr`*(a: Int128, b: int): Int128 =
  199. result = Zero
  200. let b = b and 127
  201. if b < 32:
  202. result.sdata(3) = a.sdata(3) shr b
  203. result.udata[2] = cast[uint32](bitconcat(a.udata[3], a.udata[2]) shr b)
  204. result.udata[1] = cast[uint32](bitconcat(a.udata[2], a.udata[1]) shr b)
  205. result.udata[0] = cast[uint32](bitconcat(a.udata[1], a.udata[0]) shr b)
  206. elif b < 64:
  207. if isNegative(a):
  208. result.sdata(3) = -1
  209. result.sdata(2) = a.sdata(3) shr (b and 31)
  210. result.udata[1] = cast[uint32](bitconcat(a.udata[3], a.udata[2]) shr (b and 31))
  211. result.udata[0] = cast[uint32](bitconcat(a.udata[2], a.udata[1]) shr (b and 31))
  212. elif b < 96:
  213. if isNegative(a):
  214. result.sdata(3) = -1
  215. result.sdata(2) = -1
  216. result.sdata(1) = a.sdata(3) shr (b and 31)
  217. result.udata[0] = cast[uint32](bitconcat(a.udata[3], a.udata[2]) shr (b and 31))
  218. else: # b < 128
  219. if isNegative(a):
  220. result.sdata(3) = -1
  221. result.sdata(2) = -1
  222. result.sdata(1) = -1
  223. result.sdata(0) = a.sdata(3) shr (b and 31)
  224. proc `shl`*(a: Int128, b: int): Int128 =
  225. result = Zero
  226. let b = b and 127
  227. if b < 32:
  228. result.udata[0] = a.udata[0] shl b
  229. result.udata[1] = cast[uint32]((bitconcat(a.udata[1], a.udata[0]) shl b) shr 32)
  230. result.udata[2] = cast[uint32]((bitconcat(a.udata[2], a.udata[1]) shl b) shr 32)
  231. result.udata[3] = cast[uint32]((bitconcat(a.udata[3], a.udata[2]) shl b) shr 32)
  232. elif b < 64:
  233. result.udata[0] = 0
  234. result.udata[1] = a.udata[0] shl (b and 31)
  235. result.udata[2] = cast[uint32]((bitconcat(a.udata[1], a.udata[0]) shl (b and 31)) shr 32)
  236. result.udata[3] = cast[uint32]((bitconcat(a.udata[2], a.udata[1]) shl (b and 31)) shr 32)
  237. elif b < 96:
  238. result.udata[0] = 0
  239. result.udata[1] = 0
  240. result.udata[2] = a.udata[0] shl (b and 31)
  241. result.udata[3] = cast[uint32]((bitconcat(a.udata[1], a.udata[0]) shl (b and 31)) shr 32)
  242. else:
  243. result.udata[0] = 0
  244. result.udata[1] = 0
  245. result.udata[2] = 0
  246. result.udata[3] = a.udata[0] shl (b and 31)
  247. proc `+`*(a, b: Int128): Int128 =
  248. result = Zero
  249. let tmp0 = uint64(a.udata[0]) + uint64(b.udata[0])
  250. result.udata[0] = cast[uint32](tmp0)
  251. let tmp1 = uint64(a.udata[1]) + uint64(b.udata[1]) + (tmp0 shr 32)
  252. result.udata[1] = cast[uint32](tmp1)
  253. let tmp2 = uint64(a.udata[2]) + uint64(b.udata[2]) + (tmp1 shr 32)
  254. result.udata[2] = cast[uint32](tmp2)
  255. let tmp3 = uint64(a.udata[3]) + uint64(b.udata[3]) + (tmp2 shr 32)
  256. result.udata[3] = cast[uint32](tmp3)
  257. proc `+=`*(a: var Int128, b: Int128) =
  258. a = a + b
  259. proc `-`*(a: Int128): Int128 =
  260. result = bitnot(a)
  261. result.inc
  262. proc `-`*(a, b: Int128): Int128 =
  263. a + (-b)
  264. proc `-=`*(a: var Int128, b: Int128) =
  265. a = a - b
  266. proc abs*(a: Int128): Int128 =
  267. if isNegative(a):
  268. -a
  269. else:
  270. a
  271. proc abs(a: int32): int =
  272. if a < 0: -a else: a
  273. proc `*`(a: Int128, b: uint32): Int128 =
  274. result = Zero
  275. let tmp0 = uint64(a.udata[0]) * uint64(b)
  276. let tmp1 = uint64(a.udata[1]) * uint64(b)
  277. let tmp2 = uint64(a.udata[2]) * uint64(b)
  278. let tmp3 = uint64(a.udata[3]) * uint64(b)
  279. if unlikely(tmp3 > uint64(high(int32))):
  280. assert(false, "overflow")
  281. result.udata[0] = cast[uint32](tmp0)
  282. result.udata[1] = cast[uint32](tmp1) + cast[uint32](tmp0 shr 32)
  283. result.udata[2] = cast[uint32](tmp2) + cast[uint32](tmp1 shr 32)
  284. result.udata[3] = cast[uint32](tmp3) + cast[uint32](tmp2 shr 32)
  285. proc `*`*(a: Int128, b: int32): Int128 =
  286. result = a * cast[uint32](abs(b))
  287. if b < 0:
  288. result = -result
  289. proc `*=`(a: var Int128, b: int32) =
  290. a = a * b
  291. proc makeInt128(high, low: uint64): Int128 =
  292. result = Zero
  293. result.udata[0] = cast[uint32](low)
  294. result.udata[1] = cast[uint32](low shr 32)
  295. result.udata[2] = cast[uint32](high)
  296. result.udata[3] = cast[uint32](high shr 32)
  297. proc high64(a: Int128): uint64 =
  298. bitconcat(a.udata[3], a.udata[2])
  299. proc low64(a: Int128): uint64 =
  300. bitconcat(a.udata[1], a.udata[0])
  301. proc `*`*(lhs, rhs: Int128): Int128 =
  302. let a32 = uint64(lhs.udata[1])
  303. let a00 = uint64(lhs.udata[0])
  304. let b32 = uint64(rhs.udata[1])
  305. let b00 = uint64(rhs.udata[0])
  306. result = makeInt128(high64(lhs) * low64(rhs) + low64(lhs) * high64(rhs) + a32 * b32, a00 * b00)
  307. result += toInt128(a32 * b00) shl 32
  308. result += toInt128(a00 * b32) shl 32
  309. proc `*=`*(a: var Int128, b: Int128) =
  310. a = a * b
  311. import std/bitops
  312. proc fastLog2*(a: Int128): int =
  313. result = 0
  314. if a.udata[3] != 0:
  315. return 96 + fastLog2(a.udata[3])
  316. if a.udata[2] != 0:
  317. return 64 + fastLog2(a.udata[2])
  318. if a.udata[1] != 0:
  319. return 32 + fastLog2(a.udata[1])
  320. if a.udata[0] != 0:
  321. return fastLog2(a.udata[0])
  322. proc divMod*(dividend, divisor: Int128): tuple[quotient, remainder: Int128] =
  323. assert(divisor != Zero)
  324. result = (Zero, Zero)
  325. let isNegativeA = isNegative(dividend)
  326. let isNegativeB = isNegative(divisor)
  327. var dividend = abs(dividend)
  328. let divisor = abs(divisor)
  329. if divisor > dividend:
  330. result.quotient = Zero
  331. if isNegativeA:
  332. result.remainder = -dividend
  333. else:
  334. result.remainder = dividend
  335. return
  336. if divisor == dividend:
  337. if isNegativeA xor isNegativeB:
  338. result.quotient = NegOne
  339. else:
  340. result.quotient = One
  341. result.remainder = Zero
  342. return
  343. var denominator = divisor
  344. var quotient = Zero
  345. # Left aligns the MSB of the denominator and the dividend.
  346. let shift = fastLog2(dividend) - fastLog2(denominator)
  347. denominator = denominator shl shift
  348. # Uses shift-subtract algorithm to divide dividend by denominator. The
  349. # remainder will be left in dividend.
  350. for i in 0..shift:
  351. quotient = quotient shl 1
  352. if dividend >= denominator:
  353. dividend -= denominator
  354. quotient = bitor(quotient, One)
  355. denominator = denominator shr 1
  356. if isNegativeA xor isNegativeB:
  357. result.quotient = -quotient
  358. else:
  359. result.quotient = quotient
  360. if isNegativeA:
  361. result.remainder = -dividend
  362. else:
  363. result.remainder = dividend
  364. proc `div`*(a, b: Int128): Int128 =
  365. let (a, _) = divMod(a, b)
  366. return a
  367. proc `mod`*(a, b: Int128): Int128 =
  368. let (_, b) = divMod(a, b)
  369. return b
  370. proc addInt128*(result: var string; value: Int128) =
  371. let initialSize = result.len
  372. if value == Zero:
  373. result.add '0'
  374. elif value == low(Int128):
  375. result.add "-170141183460469231731687303715884105728"
  376. else:
  377. let isNegative = isNegative(value)
  378. var value = abs(value)
  379. while value > Zero:
  380. let (quot, rem) = divMod(value, Ten)
  381. result.add "0123456789"[rem.toInt64]
  382. value = quot
  383. if isNegative:
  384. result.add '-'
  385. var i = initialSize
  386. var j = high(result)
  387. while i < j:
  388. swap(result[i], result[j])
  389. i += 1
  390. j -= 1
  391. proc `$`*(a: Int128): string =
  392. # "-170141183460469231731687303715884105728".len == 41
  393. result = newStringOfCap(41)
  394. result.addInt128(a)
  395. proc parseDecimalInt128*(arg: string, pos: int = 0): Int128 =
  396. assert(pos < arg.len)
  397. assert(arg[pos] in {'-', '0'..'9'})
  398. var isNegative = false
  399. var pos = pos
  400. if arg[pos] == '-':
  401. isNegative = true
  402. pos += 1
  403. result = Zero
  404. while pos < arg.len and arg[pos] in '0'..'9':
  405. result = result * Ten
  406. result.inc(uint32(arg[pos]) - uint32('0'))
  407. pos += 1
  408. if isNegative:
  409. result = -result
  410. # fluff
  411. proc `<`*(a: Int128, b: BiggestInt): bool =
  412. cmp(a, toInt128(b)) < 0
  413. proc `<`*(a: BiggestInt, b: Int128): bool =
  414. cmp(toInt128(a), b) < 0
  415. proc `<=`*(a: Int128, b: BiggestInt): bool =
  416. cmp(a, toInt128(b)) <= 0
  417. proc `<=`*(a: BiggestInt, b: Int128): bool =
  418. cmp(toInt128(a), b) <= 0
  419. proc `==`*(a: Int128, b: BiggestInt): bool =
  420. a == toInt128(b)
  421. proc `==`*(a: BiggestInt, b: Int128): bool =
  422. toInt128(a) == b
  423. proc `-`*(a: BiggestInt, b: Int128): Int128 =
  424. toInt128(a) - b
  425. proc `-`*(a: Int128, b: BiggestInt): Int128 =
  426. a - toInt128(b)
  427. proc `+`*(a: BiggestInt, b: Int128): Int128 =
  428. toInt128(a) + b
  429. proc `+`*(a: Int128, b: BiggestInt): Int128 =
  430. a + toInt128(b)
  431. proc toFloat64*(arg: Int128): float64 =
  432. let isNegative = isNegative(arg)
  433. let arg = abs(arg)
  434. let a = float64(bitconcat(arg.udata[1], arg.udata[0]))
  435. let b = float64(bitconcat(arg.udata[3], arg.udata[2]))
  436. result = a + 18446744073709551616'f64 * b # a + 2^64 * b
  437. if isNegative:
  438. result = -result
  439. proc ldexp(x: float64, exp: cint): float64 {.importc: "ldexp", header: "<math.h>".}
  440. template bitor(a, b, c: Int128): Int128 = bitor(bitor(a, b), c)
  441. proc toInt128*(arg: float64): Int128 =
  442. let isNegative = arg < 0
  443. let v0 = ldexp(abs(arg), -100)
  444. let w0 = uint64(trunc(v0))
  445. let v1 = ldexp(v0 - float64(w0), 50)
  446. let w1 = uint64(trunc(v1))
  447. let v2 = ldexp(v1 - float64(w1), 50)
  448. let w2 = uint64(trunc(v2))
  449. let res = bitor(toInt128(w0) shl 100, toInt128(w1) shl 50, toInt128(w2))
  450. if isNegative:
  451. return -res
  452. else:
  453. return res
  454. proc maskUInt64*(arg: Int128): Int128 {.noinit, inline.} =
  455. result = Zero
  456. result.udata[0] = arg.udata[0]
  457. result.udata[1] = arg.udata[1]
  458. result.udata[2] = 0
  459. result.udata[3] = 0
  460. proc maskUInt32*(arg: Int128): Int128 {.noinit, inline.} =
  461. result = Zero
  462. result.udata[0] = arg.udata[0]
  463. result.udata[1] = 0
  464. result.udata[2] = 0
  465. result.udata[3] = 0
  466. proc maskUInt16*(arg: Int128): Int128 {.noinit, inline.} =
  467. result = Zero
  468. result.udata[0] = arg.udata[0] and 0xffff
  469. result.udata[1] = 0
  470. result.udata[2] = 0
  471. result.udata[3] = 0
  472. proc maskUInt8*(arg: Int128): Int128 {.noinit, inline.} =
  473. result = Zero
  474. result.udata[0] = arg.udata[0] and 0xff
  475. result.udata[1] = 0
  476. result.udata[2] = 0
  477. result.udata[3] = 0
  478. proc maskBytes*(arg: Int128, numbytes: int): Int128 {.noinit.} =
  479. case numbytes
  480. of 1:
  481. return maskUInt8(arg)
  482. of 2:
  483. return maskUInt16(arg)
  484. of 4:
  485. return maskUInt32(arg)
  486. of 8:
  487. return maskUInt64(arg)
  488. else:
  489. raiseAssert "masking only implemented for 1, 2, 4 and 8 bytes"