arithmetics.nim 22 KB

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