endians.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. when defined(gcc) or defined(llvm_gcc) or defined(clang):
  12. const useBuiltinSwap = true
  13. proc builtin_bswap16(a: uint16): uint16 {.
  14. importc: "__builtin_bswap16", nodecl, nosideeffect.}
  15. proc builtin_bswap32(a: uint32): uint32 {.
  16. importc: "__builtin_bswap32", nodecl, nosideeffect.}
  17. proc builtin_bswap64(a: uint64): uint64 {.
  18. importc: "__builtin_bswap64", nodecl, nosideeffect.}
  19. elif defined(icc):
  20. const useBuiltinSwap = true
  21. proc builtin_bswap16(a: uint16): uint16 {.
  22. importc: "_bswap16", nodecl, nosideeffect.}
  23. proc builtin_bswap32(a: uint32): uint32 {.
  24. importc: "_bswap", nodecl, nosideeffect.}
  25. proc builtin_bswap64(a: uint64): uint64 {.
  26. importc: "_bswap64", nodecl, nosideeffect.}
  27. elif defined(vcc):
  28. const useBuiltinSwap = true
  29. proc builtin_bswap16(a: uint16): uint16 {.
  30. importc: "_byteswap_ushort", nodecl, header: "<intrin.h>", nosideeffect.}
  31. proc builtin_bswap32(a: uint32): uint32 {.
  32. importc: "_byteswap_ulong", nodecl, header: "<intrin.h>", nosideeffect.}
  33. proc builtin_bswap64(a: uint64): uint64 {.
  34. importc: "_byteswap_uint64", nodecl, header: "<intrin.h>", nosideeffect.}
  35. else:
  36. const useBuiltinSwap = false
  37. when useBuiltinSwap:
  38. template swapOpImpl(T: typedesc, op: untyped) =
  39. ## We have to use `copyMem` here instead of a simple deference because they
  40. ## may point to a unaligned address. A sufficiently smart compiler _should_
  41. ## be able to elide them when they're not necessary.
  42. var tmp: T
  43. copyMem(addr tmp, inp, sizeOf(T))
  44. tmp = op(tmp)
  45. copyMem(outp, addr tmp, sizeOf(T))
  46. proc swapEndian64*(outp, inp: pointer) {.inline, nosideeffect.}=
  47. swapOpImpl(uint64, builtin_bswap64)
  48. proc swapEndian32*(outp, inp: pointer) {.inline, nosideeffect.}=
  49. swapOpImpl(uint32, builtin_bswap32)
  50. proc swapEndian16*(outp, inp: pointer) {.inline, nosideeffect.}=
  51. swapOpImpl(uint16, builtin_bswap16)
  52. else:
  53. proc swapEndian64*(outp, inp: pointer) =
  54. ## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
  55. ## contain at least 8 bytes.
  56. var i = cast[cstring](inp)
  57. var o = cast[cstring](outp)
  58. o[0] = i[7]
  59. o[1] = i[6]
  60. o[2] = i[5]
  61. o[3] = i[4]
  62. o[4] = i[3]
  63. o[5] = i[2]
  64. o[6] = i[1]
  65. o[7] = i[0]
  66. proc swapEndian32*(outp, inp: pointer) =
  67. ## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
  68. ## contain at least 4 bytes.
  69. var i = cast[cstring](inp)
  70. var o = cast[cstring](outp)
  71. o[0] = i[3]
  72. o[1] = i[2]
  73. o[2] = i[1]
  74. o[3] = i[0]
  75. proc swapEndian16*(outp, inp: pointer) =
  76. ## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
  77. ## contain at least 2 bytes.
  78. var i = cast[cstring](inp)
  79. var o = cast[cstring](outp)
  80. o[0] = i[1]
  81. o[1] = i[0]
  82. when system.cpuEndian == bigEndian:
  83. proc littleEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
  84. proc littleEndian32*(outp, inp: pointer) {.inline.} = swapEndian32(outp, inp)
  85. proc littleEndian16*(outp, inp: pointer) {.inline.} = swapEndian16(outp, inp)
  86. proc bigEndian64*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 8)
  87. proc bigEndian32*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 4)
  88. proc bigEndian16*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 2)
  89. else:
  90. proc littleEndian64*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 8)
  91. proc littleEndian32*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 4)
  92. proc littleEndian16*(outp, inp: pointer){.inline.} = copyMem(outp, inp, 2)
  93. proc bigEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
  94. proc bigEndian32*(outp, inp: pointer) {.inline.} = swapEndian32(outp, inp)
  95. proc bigEndian16*(outp, inp: pointer) {.inline.} = swapEndian16(outp, inp)