Merge pull request #5180 from Parashurama/optimize_byteswapping
optimize byte-swapping in endians.nim
This commit is contained in:
commit
fb14ec9aae
1 changed files with 79 additions and 30 deletions
|
|
@ -10,38 +10,87 @@
|
||||||
## This module contains helpers that deal with different byte orders
|
## This module contains helpers that deal with different byte orders
|
||||||
## (`endian`:idx:).
|
## (`endian`:idx:).
|
||||||
|
|
||||||
proc swapEndian64*(outp, inp: pointer) =
|
when defined(gcc) or defined(llvm_gcc) or defined(clang):
|
||||||
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
const useBuiltinSwap = true
|
||||||
## contain at least 8 bytes.
|
proc builtin_bswap16(a: uint16): uint16 {.
|
||||||
var i = cast[cstring](inp)
|
importc: "__builtin_bswap16", nodecl, nosideeffect.}
|
||||||
var o = cast[cstring](outp)
|
|
||||||
o[0] = i[7]
|
|
||||||
o[1] = i[6]
|
|
||||||
o[2] = i[5]
|
|
||||||
o[3] = i[4]
|
|
||||||
o[4] = i[3]
|
|
||||||
o[5] = i[2]
|
|
||||||
o[6] = i[1]
|
|
||||||
o[7] = i[0]
|
|
||||||
|
|
||||||
proc swapEndian32*(outp, inp: pointer) =
|
proc builtin_bswap32(a: uint32): uint32 {.
|
||||||
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
importc: "__builtin_bswap32", nodecl, nosideeffect.}
|
||||||
## contain at least 4 bytes.
|
|
||||||
var i = cast[cstring](inp)
|
|
||||||
var o = cast[cstring](outp)
|
|
||||||
o[0] = i[3]
|
|
||||||
o[1] = i[2]
|
|
||||||
o[2] = i[1]
|
|
||||||
o[3] = i[0]
|
|
||||||
|
|
||||||
proc swapEndian16*(outp, inp: pointer) =
|
proc builtin_bswap64(a: uint64): uint64 {.
|
||||||
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
importc: "__builtin_bswap64", nodecl, nosideeffect.}
|
||||||
## contain at least 2 bytes.
|
elif defined(icc):
|
||||||
var
|
const useBuiltinSwap = true
|
||||||
i = cast[cstring](inp)
|
proc builtin_bswap16(a: uint16): uint16 {.
|
||||||
o = cast[cstring](outp)
|
importc: "_bswap16", nodecl, nosideeffect.}
|
||||||
o[0] = i[1]
|
|
||||||
o[1] = i[0]
|
proc builtin_bswap32(a: uint32): uint32 {.
|
||||||
|
importc: "_bswap", nodecl, nosideeffect.}
|
||||||
|
|
||||||
|
proc builtin_bswap64(a: uint64): uint64 {.
|
||||||
|
importc: "_bswap64", nodecl, nosideeffect.}
|
||||||
|
elif defined(vcc):
|
||||||
|
const useBuiltinSwap = true
|
||||||
|
proc builtin_bswap16(a: uint16): uint16 {.
|
||||||
|
importc: "_byteswap_ushort", nodecl, header: "<intrin.h>", nosideeffect.}
|
||||||
|
|
||||||
|
proc builtin_bswap32(a: uint32): uint32 {.
|
||||||
|
importc: "_byteswap_ulong", nodecl, header: "<intrin.h>", nosideeffect.}
|
||||||
|
|
||||||
|
proc builtin_bswap64(a: uint64): uint64 {.
|
||||||
|
importc: "_byteswap_uint64", nodecl, header: "<intrin.h>", nosideeffect.}
|
||||||
|
else:
|
||||||
|
const useBuiltinSwap = false
|
||||||
|
|
||||||
|
when useBuiltinSwap:
|
||||||
|
proc swapEndian64*(outp, inp: pointer) {.inline, nosideeffect.}=
|
||||||
|
var i = cast[ptr uint64](inp)
|
||||||
|
var o = cast[ptr uint64](outp)
|
||||||
|
o[] = builtin_bswap64(i[])
|
||||||
|
|
||||||
|
proc swapEndian32*(outp, inp: pointer) {.inline, nosideeffect.}=
|
||||||
|
var i = cast[ptr uint32](inp)
|
||||||
|
var o = cast[ptr uint32](outp)
|
||||||
|
o[] = builtin_bswap32(i[])
|
||||||
|
|
||||||
|
proc swapEndian16*(outp, inp: pointer) {.inline, nosideeffect.}=
|
||||||
|
var i = cast[ptr uint16](inp)
|
||||||
|
var o = cast[ptr uint16](outp)
|
||||||
|
o[] = builtin_bswap16(i[])
|
||||||
|
|
||||||
|
else:
|
||||||
|
proc swapEndian64*(outp, inp: pointer) =
|
||||||
|
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
||||||
|
## contain at least 8 bytes.
|
||||||
|
var i = cast[cstring](inp)
|
||||||
|
var o = cast[cstring](outp)
|
||||||
|
o[0] = i[7]
|
||||||
|
o[1] = i[6]
|
||||||
|
o[2] = i[5]
|
||||||
|
o[3] = i[4]
|
||||||
|
o[4] = i[3]
|
||||||
|
o[5] = i[2]
|
||||||
|
o[6] = i[1]
|
||||||
|
o[7] = i[0]
|
||||||
|
|
||||||
|
proc swapEndian32*(outp, inp: pointer) =
|
||||||
|
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
||||||
|
## contain at least 4 bytes.
|
||||||
|
var i = cast[cstring](inp)
|
||||||
|
var o = cast[cstring](outp)
|
||||||
|
o[0] = i[3]
|
||||||
|
o[1] = i[2]
|
||||||
|
o[2] = i[1]
|
||||||
|
o[3] = i[0]
|
||||||
|
|
||||||
|
proc swapEndian16*(outp, inp: pointer) =
|
||||||
|
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
||||||
|
## contain at least 2 bytes.
|
||||||
|
var i = cast[cstring](inp)
|
||||||
|
var o = cast[cstring](outp)
|
||||||
|
o[0] = i[1]
|
||||||
|
o[1] = i[0]
|
||||||
|
|
||||||
when system.cpuEndian == bigEndian:
|
when system.cpuEndian == bigEndian:
|
||||||
proc littleEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
|
proc littleEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue