io.binary.fast: adding fast alien version of >le and >be.

db4
John Benediktsson 2014-06-02 16:30:12 -07:00
parent 714d986326
commit 48c6bba132
2 changed files with 27 additions and 4 deletions

View File

@ -7,7 +7,7 @@ IN: endian
SINGLETONS: big-endian little-endian ;
: compute-native-endianness ( -- class )
1 int <ref> char deref 0 = big-endian little-endian ? ;
1 int <ref> char deref 0 = big-endian little-endian ? ; foldable
SYMBOL: native-endianness
native-endianness [ compute-native-endianness ] initialize

View File

@ -1,10 +1,13 @@
! Copyright (C) 2011 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators combinators.smart fry kernel macros math
math.ranges sequences sequences.generalizations io.binary
locals ;
USING: alien.data combinators combinators.smart endian fry
io.binary kernel locals macros math math.ranges sequences
sequences.generalizations ;
QUALIFIED-WITH: alien.c-types c
RENAME: be> io.binary => slow-be>
RENAME: le> io.binary => slow-le>
RENAME: >be io.binary => >slow-be
RENAME: >le io.binary => >slow-le
IN: io.binary.fast
ERROR: bad-length bytes n ;
@ -57,3 +60,23 @@ MACRO: reassemble-le ( n -- quot ) le-range reassemble-bytes ;
{ 8 [ 8le> ] }
[ drop slow-le> ]
} case ;
: >le ( x n -- bytes )
compute-native-endianness little-endian = [
{
{ 2 [ c:short <ref> ] }
{ 4 [ c:int <ref> ] }
{ 8 [ c:longlong <ref> ] }
[ >slow-le ]
} case
] [ >slow-le ] if ;
: >be ( x n -- bytes )
compute-native-endianness big-endian = [
{
{ 2 [ c:short <ref> ] }
{ 4 [ c:int <ref> ] }
{ 8 [ c:longlong <ref> ] }
[ >slow-be ]
} case
] [ >slow-be ] if ;