endians.nim 4.0 KB

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