arithmetics.nim 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. proc succ*[T: Ordinal](x: T, y = 1): T {.magic: "Succ", noSideEffect.} =
  2. ## Returns the `y`-th successor (default: 1) of the value `x`.
  3. ##
  4. ## If such a value does not exist, `OverflowDefect` is raised
  5. ## or a compile time error occurs.
  6. runnableExamples:
  7. assert succ(5) == 6
  8. assert succ(5, 3) == 8
  9. proc pred*[T: Ordinal](x: T, y = 1): T {.magic: "Pred", noSideEffect.} =
  10. ## Returns the `y`-th predecessor (default: 1) of the value `x`.
  11. ##
  12. ## If such a value does not exist, `OverflowDefect` is raised
  13. ## or a compile time error occurs.
  14. runnableExamples:
  15. assert pred(5) == 4
  16. assert pred(5, 3) == 2
  17. proc inc*[T: Ordinal](x: var T, y = 1) {.magic: "Inc", noSideEffect.} =
  18. ## Increments the ordinal `x` by `y`.
  19. ##
  20. ## If such a value does not exist, `OverflowDefect` is raised or a compile
  21. ## time error occurs. This is a short notation for: `x = succ(x, y)`.
  22. runnableExamples:
  23. var i = 2
  24. inc(i)
  25. assert i == 3
  26. inc(i, 3)
  27. assert i == 6
  28. proc dec*[T: Ordinal](x: var T, y = 1) {.magic: "Dec", noSideEffect.} =
  29. ## Decrements the ordinal `x` by `y`.
  30. ##
  31. ## If such a value does not exist, `OverflowDefect` is raised or a compile
  32. ## time error occurs. This is a short notation for: `x = pred(x, y)`.
  33. runnableExamples:
  34. var i = 2
  35. dec(i)
  36. assert i == 1
  37. dec(i, 3)
  38. assert i == -2
  39. # --------------------------------------------------------------------------
  40. # built-in operators
  41. when defined(nimNoZeroExtendMagic):
  42. proc ze*(x: int8): int {.deprecated.} =
  43. ## zero extends a smaller integer type to `int`. This treats `x` as
  44. ## unsigned.
  45. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  46. cast[int](uint(cast[uint8](x)))
  47. proc ze*(x: int16): int {.deprecated.} =
  48. ## zero extends a smaller integer type to `int`. This treats `x` as
  49. ## unsigned.
  50. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  51. cast[int](uint(cast[uint16](x)))
  52. proc ze64*(x: int8): int64 {.deprecated.} =
  53. ## zero extends a smaller integer type to `int64`. This treats `x` as
  54. ## unsigned.
  55. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  56. cast[int64](uint64(cast[uint8](x)))
  57. proc ze64*(x: int16): int64 {.deprecated.} =
  58. ## zero extends a smaller integer type to `int64`. This treats `x` as
  59. ## unsigned.
  60. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  61. cast[int64](uint64(cast[uint16](x)))
  62. proc ze64*(x: int32): int64 {.deprecated.} =
  63. ## zero extends a smaller integer type to `int64`. This treats `x` as
  64. ## unsigned.
  65. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  66. cast[int64](uint64(cast[uint32](x)))
  67. proc ze64*(x: int): int64 {.deprecated.} =
  68. ## zero extends a smaller integer type to `int64`. This treats `x` as
  69. ## unsigned. Does nothing if the size of an `int` is the same as `int64`.
  70. ## (This is the case on 64 bit processors.)
  71. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  72. cast[int64](uint64(cast[uint](x)))
  73. proc toU8*(x: int): int8 {.deprecated.} =
  74. ## treats `x` as unsigned and converts it to a byte by taking the last 8 bits
  75. ## from `x`.
  76. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  77. cast[int8](x)
  78. proc toU16*(x: int): int16 {.deprecated.} =
  79. ## treats `x` as unsigned and converts it to an `int16` by taking the last
  80. ## 16 bits from `x`.
  81. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  82. cast[int16](x)
  83. proc toU32*(x: int64): int32 {.deprecated.} =
  84. ## treats `x` as unsigned and converts it to an `int32` by taking the
  85. ## last 32 bits from `x`.
  86. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  87. cast[int32](x)
  88. elif not defined(js):
  89. proc ze*(x: int8): int {.magic: "Ze8ToI", noSideEffect, deprecated.}
  90. ## zero extends a smaller integer type to `int`. This treats `x` as
  91. ## unsigned.
  92. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  93. proc ze*(x: int16): int {.magic: "Ze16ToI", noSideEffect, deprecated.}
  94. ## zero extends a smaller integer type to `int`. This treats `x` as
  95. ## unsigned.
  96. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  97. proc ze64*(x: int8): int64 {.magic: "Ze8ToI64", noSideEffect, deprecated.}
  98. ## zero extends a smaller integer type to `int64`. This treats `x` as
  99. ## unsigned.
  100. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  101. proc ze64*(x: int16): int64 {.magic: "Ze16ToI64", noSideEffect, deprecated.}
  102. ## zero extends a smaller integer type to `int64`. This treats `x` as
  103. ## unsigned.
  104. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  105. proc ze64*(x: int32): int64 {.magic: "Ze32ToI64", noSideEffect, deprecated.}
  106. ## zero extends a smaller integer type to `int64`. This treats `x` as
  107. ## unsigned.
  108. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  109. proc ze64*(x: int): int64 {.magic: "ZeIToI64", noSideEffect, deprecated.}
  110. ## zero extends a smaller integer type to `int64`. This treats `x` as
  111. ## unsigned. Does nothing if the size of an `int` is the same as `int64`.
  112. ## (This is the case on 64 bit processors.)
  113. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  114. proc toU8*(x: int): int8 {.magic: "ToU8", noSideEffect, deprecated.}
  115. ## treats `x` as unsigned and converts it to a byte by taking the last 8 bits
  116. ## from `x`.
  117. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  118. proc toU16*(x: int): int16 {.magic: "ToU16", noSideEffect, deprecated.}
  119. ## treats `x` as unsigned and converts it to an `int16` by taking the last
  120. ## 16 bits from `x`.
  121. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  122. proc toU32*(x: int64): int32 {.magic: "ToU32", noSideEffect, deprecated.}
  123. ## treats `x` as unsigned and converts it to an `int32` by taking the
  124. ## last 32 bits from `x`.
  125. ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
  126. # integer calculations:
  127. proc `+`*(x: int): int {.magic: "UnaryPlusI", noSideEffect.}
  128. ## Unary `+` operator for an integer. Has no effect.
  129. proc `+`*(x: int8): int8 {.magic: "UnaryPlusI", noSideEffect.}
  130. proc `+`*(x: int16): int16 {.magic: "UnaryPlusI", noSideEffect.}
  131. proc `+`*(x: int32): int32 {.magic: "UnaryPlusI", noSideEffect.}
  132. proc `+`*(x: int64): int64 {.magic: "UnaryPlusI", noSideEffect.}
  133. proc `-`*(x: int): int {.magic: "UnaryMinusI", noSideEffect.}
  134. ## Unary `-` operator for an integer. Negates `x`.
  135. proc `-`*(x: int8): int8 {.magic: "UnaryMinusI", noSideEffect.}
  136. proc `-`*(x: int16): int16 {.magic: "UnaryMinusI", noSideEffect.}
  137. proc `-`*(x: int32): int32 {.magic: "UnaryMinusI", noSideEffect.}
  138. proc `-`*(x: int64): int64 {.magic: "UnaryMinusI64", noSideEffect.}
  139. proc `not`*(x: int): int {.magic: "BitnotI", noSideEffect.} =
  140. ## Computes the `bitwise complement` of the integer `x`.
  141. runnableExamples:
  142. assert not 0'u8 == 255
  143. assert not 0'i8 == -1
  144. assert not 1000'u16 == 64535
  145. assert not 1000'i16 == -1001
  146. proc `not`*(x: int8): int8 {.magic: "BitnotI", noSideEffect.}
  147. proc `not`*(x: int16): int16 {.magic: "BitnotI", noSideEffect.}
  148. proc `not`*(x: int32): int32 {.magic: "BitnotI", noSideEffect.}
  149. proc `not`*(x: int64): int64 {.magic: "BitnotI", noSideEffect.}
  150. proc `+`*(x, y: int): int {.magic: "AddI", noSideEffect.}
  151. ## Binary `+` operator for an integer.
  152. proc `+`*(x, y: int8): int8 {.magic: "AddI", noSideEffect.}
  153. proc `+`*(x, y: int16): int16 {.magic: "AddI", noSideEffect.}
  154. proc `+`*(x, y: int32): int32 {.magic: "AddI", noSideEffect.}
  155. proc `+`*(x, y: int64): int64 {.magic: "AddI", noSideEffect.}
  156. proc `-`*(x, y: int): int {.magic: "SubI", noSideEffect.}
  157. ## Binary `-` operator for an integer.
  158. proc `-`*(x, y: int8): int8 {.magic: "SubI", noSideEffect.}
  159. proc `-`*(x, y: int16): int16 {.magic: "SubI", noSideEffect.}
  160. proc `-`*(x, y: int32): int32 {.magic: "SubI", noSideEffect.}
  161. proc `-`*(x, y: int64): int64 {.magic: "SubI", noSideEffect.}
  162. proc `*`*(x, y: int): int {.magic: "MulI", noSideEffect.}
  163. ## Binary `*` operator for an integer.
  164. proc `*`*(x, y: int8): int8 {.magic: "MulI", noSideEffect.}
  165. proc `*`*(x, y: int16): int16 {.magic: "MulI", noSideEffect.}
  166. proc `*`*(x, y: int32): int32 {.magic: "MulI", noSideEffect.}
  167. proc `*`*(x, y: int64): int64 {.magic: "MulI", noSideEffect.}
  168. proc `div`*(x, y: int): int {.magic: "DivI", noSideEffect.} =
  169. ## Computes the integer division.
  170. ##
  171. ## This is roughly the same as `math.trunc(x/y).int`.
  172. runnableExamples:
  173. assert (1 div 2) == 0
  174. assert (2 div 2) == 1
  175. assert (3 div 2) == 1
  176. assert (7 div 3) == 2
  177. assert (-7 div 3) == -2
  178. assert (7 div -3) == -2
  179. assert (-7 div -3) == 2
  180. proc `div`*(x, y: int8): int8 {.magic: "DivI", noSideEffect.}
  181. proc `div`*(x, y: int16): int16 {.magic: "DivI", noSideEffect.}
  182. proc `div`*(x, y: int32): int32 {.magic: "DivI", noSideEffect.}
  183. proc `div`*(x, y: int64): int64 {.magic: "DivI", noSideEffect.}
  184. proc `mod`*(x, y: int): int {.magic: "ModI", noSideEffect.} =
  185. ## Computes the integer modulo operation (remainder).
  186. ##
  187. ## This is the same as `x - (x div y) * y`.
  188. runnableExamples:
  189. assert (7 mod 5) == 2
  190. assert (-7 mod 5) == -2
  191. assert (7 mod -5) == 2
  192. assert (-7 mod -5) == -2
  193. proc `mod`*(x, y: int8): int8 {.magic: "ModI", noSideEffect.}
  194. proc `mod`*(x, y: int16): int16 {.magic: "ModI", noSideEffect.}
  195. proc `mod`*(x, y: int32): int32 {.magic: "ModI", noSideEffect.}
  196. proc `mod`*(x, y: int64): int64 {.magic: "ModI", noSideEffect.}
  197. when defined(nimOldShiftRight):
  198. const shrDepMessage = "`shr` will become sign preserving."
  199. proc `shr`*(x: int, y: SomeInteger): int {.magic: "ShrI", noSideEffect, deprecated: shrDepMessage.}
  200. proc `shr`*(x: int8, y: SomeInteger): int8 {.magic: "ShrI", noSideEffect, deprecated: shrDepMessage.}
  201. proc `shr`*(x: int16, y: SomeInteger): int16 {.magic: "ShrI", noSideEffect, deprecated: shrDepMessage.}
  202. proc `shr`*(x: int32, y: SomeInteger): int32 {.magic: "ShrI", noSideEffect, deprecated: shrDepMessage.}
  203. proc `shr`*(x: int64, y: SomeInteger): int64 {.magic: "ShrI", noSideEffect, deprecated: shrDepMessage.}
  204. else:
  205. proc `shr`*(x: int, y: SomeInteger): int {.magic: "AshrI", noSideEffect.} =
  206. ## Computes the `shift right` operation of `x` and `y`, filling
  207. ## vacant bit positions with the sign bit.
  208. ##
  209. ## **Note**: `Operator precedence <manual.html#syntax-precedence>`_
  210. ## is different than in *C*.
  211. ##
  212. ## See also:
  213. ## * `ashr func<#ashr,int,SomeInteger>`_ for arithmetic shift right
  214. runnableExamples:
  215. assert 0b0001_0000'i8 shr 2 == 0b0000_0100'i8
  216. assert 0b0000_0001'i8 shr 1 == 0b0000_0000'i8
  217. assert 0b1000_0000'i8 shr 4 == 0b1111_1000'i8
  218. assert -1 shr 5 == -1
  219. assert 1 shr 5 == 0
  220. assert 16 shr 2 == 4
  221. assert -16 shr 2 == -4
  222. proc `shr`*(x: int8, y: SomeInteger): int8 {.magic: "AshrI", noSideEffect.}
  223. proc `shr`*(x: int16, y: SomeInteger): int16 {.magic: "AshrI", noSideEffect.}
  224. proc `shr`*(x: int32, y: SomeInteger): int32 {.magic: "AshrI", noSideEffect.}
  225. proc `shr`*(x: int64, y: SomeInteger): int64 {.magic: "AshrI", noSideEffect.}
  226. proc `shl`*(x: int, y: SomeInteger): int {.magic: "ShlI", noSideEffect.} =
  227. ## Computes the `shift left` operation of `x` and `y`.
  228. ##
  229. ## **Note**: `Operator precedence <manual.html#syntax-precedence>`_
  230. ## is different than in *C*.
  231. runnableExamples:
  232. assert 1'i32 shl 4 == 0x0000_0010
  233. assert 1'i64 shl 4 == 0x0000_0000_0000_0010
  234. proc `shl`*(x: int8, y: SomeInteger): int8 {.magic: "ShlI", noSideEffect.}
  235. proc `shl`*(x: int16, y: SomeInteger): int16 {.magic: "ShlI", noSideEffect.}
  236. proc `shl`*(x: int32, y: SomeInteger): int32 {.magic: "ShlI", noSideEffect.}
  237. proc `shl`*(x: int64, y: SomeInteger): int64 {.magic: "ShlI", noSideEffect.}
  238. proc ashr*(x: int, y: SomeInteger): int {.magic: "AshrI", noSideEffect.} =
  239. ## Shifts right by pushing copies of the leftmost bit in from the left,
  240. ## and let the rightmost bits fall off.
  241. ##
  242. ## Note that `ashr` is not an operator so use the normal function
  243. ## call syntax for it.
  244. ##
  245. ## See also:
  246. ## * `shr func<#shr,int,SomeInteger>`_
  247. runnableExamples:
  248. assert ashr(0b0001_0000'i8, 2) == 0b0000_0100'i8
  249. assert ashr(0b1000_0000'i8, 8) == 0b1111_1111'i8
  250. assert ashr(0b1000_0000'i8, 1) == 0b1100_0000'i8
  251. proc ashr*(x: int8, y: SomeInteger): int8 {.magic: "AshrI", noSideEffect.}
  252. proc ashr*(x: int16, y: SomeInteger): int16 {.magic: "AshrI", noSideEffect.}
  253. proc ashr*(x: int32, y: SomeInteger): int32 {.magic: "AshrI", noSideEffect.}
  254. proc ashr*(x: int64, y: SomeInteger): int64 {.magic: "AshrI", noSideEffect.}
  255. proc `and`*(x, y: int): int {.magic: "BitandI", noSideEffect.} =
  256. ## Computes the `bitwise and` of numbers `x` and `y`.
  257. runnableExamples:
  258. assert (0b0011 and 0b0101) == 0b0001
  259. assert (0b0111 and 0b1100) == 0b0100
  260. proc `and`*(x, y: int8): int8 {.magic: "BitandI", noSideEffect.}
  261. proc `and`*(x, y: int16): int16 {.magic: "BitandI", noSideEffect.}
  262. proc `and`*(x, y: int32): int32 {.magic: "BitandI", noSideEffect.}
  263. proc `and`*(x, y: int64): int64 {.magic: "BitandI", noSideEffect.}
  264. proc `or`*(x, y: int): int {.magic: "BitorI", noSideEffect.} =
  265. ## Computes the `bitwise or` of numbers `x` and `y`.
  266. runnableExamples:
  267. assert (0b0011 or 0b0101) == 0b0111
  268. assert (0b0111 or 0b1100) == 0b1111
  269. proc `or`*(x, y: int8): int8 {.magic: "BitorI", noSideEffect.}
  270. proc `or`*(x, y: int16): int16 {.magic: "BitorI", noSideEffect.}
  271. proc `or`*(x, y: int32): int32 {.magic: "BitorI", noSideEffect.}
  272. proc `or`*(x, y: int64): int64 {.magic: "BitorI", noSideEffect.}
  273. proc `xor`*(x, y: int): int {.magic: "BitxorI", noSideEffect.} =
  274. ## Computes the `bitwise xor` of numbers `x` and `y`.
  275. runnableExamples:
  276. assert (0b0011 xor 0b0101) == 0b0110
  277. assert (0b0111 xor 0b1100) == 0b1011
  278. proc `xor`*(x, y: int8): int8 {.magic: "BitxorI", noSideEffect.}
  279. proc `xor`*(x, y: int16): int16 {.magic: "BitxorI", noSideEffect.}
  280. proc `xor`*(x, y: int32): int32 {.magic: "BitxorI", noSideEffect.}
  281. proc `xor`*(x, y: int64): int64 {.magic: "BitxorI", noSideEffect.}
  282. # unsigned integer operations:
  283. proc `not`*(x: uint): uint {.magic: "BitnotI", noSideEffect.}
  284. ## Computes the `bitwise complement` of the integer `x`.
  285. proc `not`*(x: uint8): uint8 {.magic: "BitnotI", noSideEffect.}
  286. proc `not`*(x: uint16): uint16 {.magic: "BitnotI", noSideEffect.}
  287. proc `not`*(x: uint32): uint32 {.magic: "BitnotI", noSideEffect.}
  288. proc `not`*(x: uint64): uint64 {.magic: "BitnotI", noSideEffect.}
  289. proc `shr`*(x: uint, y: SomeInteger): uint {.magic: "ShrI", noSideEffect.}
  290. ## Computes the `shift right` operation of `x` and `y`.
  291. proc `shr`*(x: uint8, y: SomeInteger): uint8 {.magic: "ShrI", noSideEffect.}
  292. proc `shr`*(x: uint16, y: SomeInteger): uint16 {.magic: "ShrI", noSideEffect.}
  293. proc `shr`*(x: uint32, y: SomeInteger): uint32 {.magic: "ShrI", noSideEffect.}
  294. proc `shr`*(x: uint64, y: SomeInteger): uint64 {.magic: "ShrI", noSideEffect.}
  295. proc `shl`*(x: uint, y: SomeInteger): uint {.magic: "ShlI", noSideEffect.}
  296. ## Computes the `shift left` operation of `x` and `y`.
  297. proc `shl`*(x: uint8, y: SomeInteger): uint8 {.magic: "ShlI", noSideEffect.}
  298. proc `shl`*(x: uint16, y: SomeInteger): uint16 {.magic: "ShlI", noSideEffect.}
  299. proc `shl`*(x: uint32, y: SomeInteger): uint32 {.magic: "ShlI", noSideEffect.}
  300. proc `shl`*(x: uint64, y: SomeInteger): uint64 {.magic: "ShlI", noSideEffect.}
  301. proc `and`*(x, y: uint): uint {.magic: "BitandI", noSideEffect.}
  302. ## Computes the `bitwise and` of numbers `x` and `y`.
  303. proc `and`*(x, y: uint8): uint8 {.magic: "BitandI", noSideEffect.}
  304. proc `and`*(x, y: uint16): uint16 {.magic: "BitandI", noSideEffect.}
  305. proc `and`*(x, y: uint32): uint32 {.magic: "BitandI", noSideEffect.}
  306. proc `and`*(x, y: uint64): uint64 {.magic: "BitandI", noSideEffect.}
  307. proc `or`*(x, y: uint): uint {.magic: "BitorI", noSideEffect.}
  308. ## Computes the `bitwise or` of numbers `x` and `y`.
  309. proc `or`*(x, y: uint8): uint8 {.magic: "BitorI", noSideEffect.}
  310. proc `or`*(x, y: uint16): uint16 {.magic: "BitorI", noSideEffect.}
  311. proc `or`*(x, y: uint32): uint32 {.magic: "BitorI", noSideEffect.}
  312. proc `or`*(x, y: uint64): uint64 {.magic: "BitorI", noSideEffect.}
  313. proc `xor`*(x, y: uint): uint {.magic: "BitxorI", noSideEffect.}
  314. ## Computes the `bitwise xor` of numbers `x` and `y`.
  315. proc `xor`*(x, y: uint8): uint8 {.magic: "BitxorI", noSideEffect.}
  316. proc `xor`*(x, y: uint16): uint16 {.magic: "BitxorI", noSideEffect.}
  317. proc `xor`*(x, y: uint32): uint32 {.magic: "BitxorI", noSideEffect.}
  318. proc `xor`*(x, y: uint64): uint64 {.magic: "BitxorI", noSideEffect.}
  319. proc `+`*(x, y: uint): uint {.magic: "AddU", noSideEffect.}
  320. ## Binary `+` operator for unsigned integers.
  321. proc `+`*(x, y: uint8): uint8 {.magic: "AddU", noSideEffect.}
  322. proc `+`*(x, y: uint16): uint16 {.magic: "AddU", noSideEffect.}
  323. proc `+`*(x, y: uint32): uint32 {.magic: "AddU", noSideEffect.}
  324. proc `+`*(x, y: uint64): uint64 {.magic: "AddU", noSideEffect.}
  325. proc `-`*(x, y: uint): uint {.magic: "SubU", noSideEffect.}
  326. ## Binary `-` operator for unsigned integers.
  327. proc `-`*(x, y: uint8): uint8 {.magic: "SubU", noSideEffect.}
  328. proc `-`*(x, y: uint16): uint16 {.magic: "SubU", noSideEffect.}
  329. proc `-`*(x, y: uint32): uint32 {.magic: "SubU", noSideEffect.}
  330. proc `-`*(x, y: uint64): uint64 {.magic: "SubU", noSideEffect.}
  331. proc `*`*(x, y: uint): uint {.magic: "MulU", noSideEffect.}
  332. ## Binary `*` operator for unsigned integers.
  333. proc `*`*(x, y: uint8): uint8 {.magic: "MulU", noSideEffect.}
  334. proc `*`*(x, y: uint16): uint16 {.magic: "MulU", noSideEffect.}
  335. proc `*`*(x, y: uint32): uint32 {.magic: "MulU", noSideEffect.}
  336. proc `*`*(x, y: uint64): uint64 {.magic: "MulU", noSideEffect.}
  337. proc `div`*(x, y: uint): uint {.magic: "DivU", noSideEffect.}
  338. ## Computes the integer division for unsigned integers.
  339. ## This is roughly the same as `trunc(x/y)`.
  340. proc `div`*(x, y: uint8): uint8 {.magic: "DivU", noSideEffect.}
  341. proc `div`*(x, y: uint16): uint16 {.magic: "DivU", noSideEffect.}
  342. proc `div`*(x, y: uint32): uint32 {.magic: "DivU", noSideEffect.}
  343. proc `div`*(x, y: uint64): uint64 {.magic: "DivU", noSideEffect.}
  344. proc `mod`*(x, y: uint): uint {.magic: "ModU", noSideEffect.}
  345. ## Computes the integer modulo operation (remainder) for unsigned integers.
  346. ## This is the same as `x - (x div y) * y`.
  347. proc `mod`*(x, y: uint8): uint8 {.magic: "ModU", noSideEffect.}
  348. proc `mod`*(x, y: uint16): uint16 {.magic: "ModU", noSideEffect.}
  349. proc `mod`*(x, y: uint32): uint32 {.magic: "ModU", noSideEffect.}
  350. proc `mod`*(x, y: uint64): uint64 {.magic: "ModU", noSideEffect.}
  351. proc `+%`*(x, y: int): int {.inline.} =
  352. ## Treats `x` and `y` as unsigned and adds them.
  353. ##
  354. ## The result is truncated to fit into the result.
  355. ## This implements modulo arithmetic. No overflow errors are possible.
  356. cast[int](cast[uint](x) + cast[uint](y))
  357. proc `+%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) + cast[uint8](y))
  358. proc `+%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) + cast[uint16](y))
  359. proc `+%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) + cast[uint32](y))
  360. proc `+%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) + cast[uint64](y))
  361. proc `-%`*(x, y: int): int {.inline.} =
  362. ## Treats `x` and `y` as unsigned and subtracts them.
  363. ##
  364. ## The result is truncated to fit into the result.
  365. ## This implements modulo arithmetic. No overflow errors are possible.
  366. cast[int](cast[uint](x) - cast[uint](y))
  367. proc `-%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) - cast[uint8](y))
  368. proc `-%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) - cast[uint16](y))
  369. proc `-%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) - cast[uint32](y))
  370. proc `-%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) - cast[uint64](y))
  371. proc `*%`*(x, y: int): int {.inline.} =
  372. ## Treats `x` and `y` as unsigned and multiplies them.
  373. ##
  374. ## The result is truncated to fit into the result.
  375. ## This implements modulo arithmetic. No overflow errors are possible.
  376. cast[int](cast[uint](x) * cast[uint](y))
  377. proc `*%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) * cast[uint8](y))
  378. proc `*%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) * cast[uint16](y))
  379. proc `*%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) * cast[uint32](y))
  380. proc `*%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) * cast[uint64](y))
  381. proc `/%`*(x, y: int): int {.inline.} =
  382. ## Treats `x` and `y` as unsigned and divides them.
  383. ##
  384. ## The result is truncated to fit into the result.
  385. ## This implements modulo arithmetic. No overflow errors are possible.
  386. cast[int](cast[uint](x) div cast[uint](y))
  387. proc `/%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) div cast[uint8](y))
  388. proc `/%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) div cast[uint16](y))
  389. proc `/%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) div cast[uint32](y))
  390. proc `/%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) div cast[uint64](y))
  391. proc `%%`*(x, y: int): int {.inline.} =
  392. ## Treats `x` and `y` as unsigned and compute the modulo of `x` and `y`.
  393. ##
  394. ## The result is truncated to fit into the result.
  395. ## This implements modulo arithmetic. No overflow errors are possible.
  396. cast[int](cast[uint](x) mod cast[uint](y))
  397. proc `%%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) mod cast[uint8](y))
  398. proc `%%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) mod cast[uint16](y))
  399. proc `%%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) mod cast[uint32](y))
  400. proc `%%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) mod cast[uint64](y))
  401. proc `+=`*[T: SomeInteger](x: var T, y: T) {.
  402. magic: "Inc", noSideEffect.}
  403. ## Increments an integer.
  404. proc `-=`*[T: SomeInteger](x: var T, y: T) {.
  405. magic: "Dec", noSideEffect.}
  406. ## Decrements an integer.
  407. proc `*=`*[T: SomeInteger](x: var T, y: T) {.
  408. inline, noSideEffect.} =
  409. ## Binary `*=` operator for integers.
  410. x = x * y