endians.nim 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module contains helpers that deal with different byte orders
  10. ## (`endian`:idx:).
  11. ##
  12. ## Endianness is the order of bytes of a value in memory. Big-endian means that
  13. ## the most significant byte is stored at the smallest memory address,
  14. ## while little endian means that the least-significant byte is stored
  15. ## at the smallest address. See also https://en.wikipedia.org/wiki/Endianness.
  16. ##
  17. ## Unstable API.
  18. when defined(gcc) or defined(llvm_gcc) or defined(clang):
  19. const useBuiltinSwap = true
  20. proc builtin_bswap16(a: uint16): uint16 {.
  21. importc: "__builtin_bswap16", nodecl, noSideEffect.}
  22. proc builtin_bswap32(a: uint32): uint32 {.
  23. importc: "__builtin_bswap32", nodecl, noSideEffect.}
  24. proc builtin_bswap64(a: uint64): uint64 {.
  25. importc: "__builtin_bswap64", nodecl, noSideEffect.}
  26. elif defined(icc):
  27. const useBuiltinSwap = true
  28. proc builtin_bswap16(a: uint16): uint16 {.
  29. importc: "_bswap16", nodecl, noSideEffect.}
  30. proc builtin_bswap32(a: uint32): uint32 {.
  31. importc: "_bswap", nodecl, noSideEffect.}
  32. proc builtin_bswap64(a: uint64): uint64 {.
  33. importc: "_bswap64", nodecl, noSideEffect.}
  34. elif defined(vcc):
  35. const useBuiltinSwap = true
  36. proc builtin_bswap16(a: uint16): uint16 {.
  37. importc: "_byteswap_ushort", nodecl, header: "<intrin.h>", noSideEffect.}
  38. proc builtin_bswap32(a: uint32): uint32 {.
  39. importc: "_byteswap_ulong", nodecl, header: "<intrin.h>", noSideEffect.}
  40. proc builtin_bswap64(a: uint64): uint64 {.
  41. importc: "_byteswap_uint64", nodecl, header: "<intrin.h>", noSideEffect.}
  42. else:
  43. const useBuiltinSwap = false
  44. when useBuiltinSwap:
  45. template swapOpImpl(T: typedesc, op: untyped) =
  46. ## We have to use `copyMem` here instead of a simple dereference because they
  47. ## may point to a unaligned address. A sufficiently smart compiler _should_
  48. ## be able to elide them when they're not necessary.
  49. var tmp: T
  50. copyMem(addr tmp, inp, sizeof(T))
  51. tmp = op(tmp)
  52. copyMem(outp, addr tmp, sizeof(T))
  53. proc swapEndian64*(outp, inp: pointer) {.inline, noSideEffect.} =
  54. ## Copies `inp` to `outp`, reversing the byte order.
  55. ## Both buffers are supposed to contain at least 8 bytes.
  56. runnableExamples:
  57. var a = [1'u8, 2, 3, 4, 5, 6, 7, 8]
  58. var b: array[8, uint8]
  59. swapEndian64(addr b, addr a)
  60. assert b == [8'u8, 7, 6, 5, 4, 3, 2, 1]
  61. swapOpImpl(uint64, builtin_bswap64)
  62. proc swapEndian32*(outp, inp: pointer) {.inline, noSideEffect.} =
  63. ## Copies `inp` to `outp`, reversing the byte order.
  64. ## Both buffers are supposed to contain at least 4 bytes.
  65. runnableExamples:
  66. var a = [1'u8, 2, 3, 4]
  67. var b: array[4, uint8]
  68. swapEndian32(addr b, addr a)
  69. assert b == [4'u8, 3, 2, 1]
  70. swapOpImpl(uint32, builtin_bswap32)
  71. proc swapEndian16*(outp, inp: pointer) {.inline, noSideEffect.} =
  72. ## Copies `inp` to `outp`, reversing the byte order.
  73. ## Both buffers are supposed to contain at least 2 bytes.
  74. runnableExamples:
  75. var a = [1'u8, 2]
  76. var b: array[2, uint8]
  77. swapEndian16(addr b, addr a)
  78. assert b == [2'u8, 1]
  79. swapOpImpl(uint16, builtin_bswap16)
  80. else:
  81. proc swapEndian64*(outp, inp: pointer) =
  82. var i = cast[cstring](inp)
  83. var o = cast[cstring](outp)
  84. o[0] = i[7]
  85. o[1] = i[6]
  86. o[2] = i[5]
  87. o[3] = i[4]
  88. o[4] = i[3]
  89. o[5] = i[2]
  90. o[6] = i[1]
  91. o[7] = i[0]
  92. proc swapEndian32*(outp, inp: pointer) =
  93. var i = cast[cstring](inp)
  94. var o = cast[cstring](outp)
  95. o[0] = i[3]
  96. o[1] = i[2]
  97. o[2] = i[1]
  98. o[3] = i[0]
  99. proc swapEndian16*(outp, inp: pointer) =
  100. var i = cast[cstring](inp)
  101. var o = cast[cstring](outp)
  102. o[0] = i[1]
  103. o[1] = i[0]
  104. when system.cpuEndian == bigEndian:
  105. proc littleEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
  106. proc littleEndian32*(outp, inp: pointer) {.inline.} = swapEndian32(outp, inp)
  107. proc littleEndian16*(outp, inp: pointer) {.inline.} = swapEndian16(outp, inp)
  108. proc bigEndian64*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 8)
  109. proc bigEndian32*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 4)
  110. proc bigEndian16*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 2)
  111. else:
  112. proc littleEndian64*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 8)
  113. ## Copies `inp` to `outp`, storing it in 64-bit little-endian order.
  114. ## Both buffers are supposed to contain at least 8 bytes.
  115. proc littleEndian32*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 4)
  116. ## Copies `inp` to `outp`, storing it in 32-bit little-endian order.
  117. ## Both buffers are supposed to contain at least 4 bytes.
  118. proc littleEndian16*(outp, inp: pointer){.inline.} = copyMem(outp, inp, 2)
  119. ## Copies `inp` to `outp`, storing it in 16-bit little-endian order.
  120. ## Both buffers are supposed to contain at least 2 bytes.
  121. proc bigEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
  122. ## Copies `inp` to `outp`, storing it in 64-bit big-endian order.
  123. ## Both buffers are supposed to contain at least 8 bytes.
  124. proc bigEndian32*(outp, inp: pointer) {.inline.} = swapEndian32(outp, inp)
  125. ## Copies `inp` to `outp`, storing it in 32-bit big-endian order.
  126. ## Both buffers are supposed to contain at least 4 bytes.
  127. proc bigEndian16*(outp, inp: pointer) {.inline.} = swapEndian16(outp, inp)
  128. ## Copies `inp` to `outp`, storing it in 16-bit big-endian order.
  129. ## Both buffers are supposed to contain at least 2 bytes.