summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2017-10-18 16:13:24 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2017-10-18 16:13:24 +0000
commit45923c08b00ccdfd888caf6d2b555a137c563988 (patch)
tree5dc167cf4b8e3ff372f59ca9fb6ba0940387c83c /src
parent62a04cdd0b6811c07bbafae37fe906a36fa800b1 (diff)
downloadskalibs-45923c08b00ccdfd888caf6d2b555a137c563988.tar.xz
Better packing / unpacking code
Godbolted for x86_64 with gcc and clang.
Diffstat (limited to 'src')
-rw-r--r--src/headers/bits-bendian3
-rw-r--r--src/headers/bits-lendian5
-rw-r--r--src/headers/bits-template1
-rw-r--r--src/libstddjb/uint16_bswap.c9
-rw-r--r--src/libstddjb/uint16_pack.c7
-rw-r--r--src/libstddjb/uint16_pack_big.c7
-rw-r--r--src/libstddjb/uint16_reverse.c1
-rw-r--r--src/libstddjb/uint16_unpack.c8
-rw-r--r--src/libstddjb/uint16_unpack_big.c8
-rw-r--r--src/libstddjb/uint32_bswap.c13
-rw-r--r--src/libstddjb/uint32_pack.c9
-rw-r--r--src/libstddjb/uint32_pack_big.c9
-rw-r--r--src/libstddjb/uint32_reverse.c14
-rw-r--r--src/libstddjb/uint32_unpack.c10
-rw-r--r--src/libstddjb/uint32_unpack_big.c10
-rw-r--r--src/libstddjb/uint64_bswap.c16
-rw-r--r--src/libstddjb/uint64_pack.c12
-rw-r--r--src/libstddjb/uint64_pack_big.c12
-rw-r--r--src/libstddjb/uint64_reverse.c11
-rw-r--r--src/libstddjb/uint64_unpack.c14
-rw-r--r--src/libstddjb/uint64_unpack_big.c14
21 files changed, 106 insertions, 87 deletions
diff --git a/src/headers/bits-bendian b/src/headers/bits-bendian
index ef57f58..6caf1e7 100644
--- a/src/headers/bits-bendian
+++ b/src/headers/bits-bendian
@@ -1,3 +1,6 @@
#define uint@BITS@_little_endian(s, n) uint@BITS@_reverse((s), (n))
#define uint@BITS@_big_endian(s, n)
+#define uint@BITS@_little(u) uint@BITS@_bswap(u)
+#define uint@BITS@_big(u) (u)
+
diff --git a/src/headers/bits-lendian b/src/headers/bits-lendian
index 278af3e..7528d46 100644
--- a/src/headers/bits-lendian
+++ b/src/headers/bits-lendian
@@ -1,3 +1,6 @@
#define uint@BITS@_little_endian(s, n)
-#define uint@BITS@_big_endian(s, n) uint16_reverse((s), (n))
+#define uint@BITS@_big_endian(s, n) uint@BITS@_reverse((s), (n))
+
+#define uint@BITS@_little(u) (u)
+#define uint@BITS@_big(u) uint@BITS@_bswap(u)
diff --git a/src/headers/bits-template b/src/headers/bits-template
index 4b43de2..b09036b 100644
--- a/src/headers/bits-template
+++ b/src/headers/bits-template
@@ -3,6 +3,7 @@ extern void uint@BITS@_pack_big (char *, uint@BITS@_t) ;
extern void uint@BITS@_unpack (char const *, uint@BITS@_t *) ;
extern void uint@BITS@_unpack_big (char const *, uint@BITS@_t *) ;
extern void uint@BITS@_reverse (char *, size_t) ;
+extern uint@BITS@_t uint@BITS@_bswap (uint@BITS@_t) ;
#define UINT@BITS@_FMT @DFMT@
#define UINT@BITS@_OFMT @OFMT@
diff --git a/src/libstddjb/uint16_bswap.c b/src/libstddjb/uint16_bswap.c
new file mode 100644
index 0000000..6ab96ed
--- /dev/null
+++ b/src/libstddjb/uint16_bswap.c
@@ -0,0 +1,9 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <skalibs/uint16.h>
+
+uint16_t uint16_bswap (uint16_t a)
+{
+ return (a & 0x00ffu) << 8 | (a & 0xff00u) >> 8 ;
+}
diff --git a/src/libstddjb/uint16_pack.c b/src/libstddjb/uint16_pack.c
index de0a09b..e7ffe24 100644
--- a/src/libstddjb/uint16_pack.c
+++ b/src/libstddjb/uint16_pack.c
@@ -1,10 +1,11 @@
/* ISC license. */
+#include <stdint.h>
+#include <string.h>
#include <skalibs/uint16.h>
-#include <skalibs/bytestr.h>
void uint16_pack (char *s, uint16_t u)
{
- ((unsigned char *)s)[0] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[1] = T8(u) ;
+ u = uint16_little(u) ;
+ memcpy(s, &u, sizeof(uint16_t)) ;
}
diff --git a/src/libstddjb/uint16_pack_big.c b/src/libstddjb/uint16_pack_big.c
index f73f88b..c24f78f 100644
--- a/src/libstddjb/uint16_pack_big.c
+++ b/src/libstddjb/uint16_pack_big.c
@@ -1,10 +1,11 @@
/* ISC license. */
+#include <stdint.h>
+#include <string.h>
#include <skalibs/uint16.h>
-#include <skalibs/bytestr.h>
void uint16_pack_big (char *s, uint16_t u)
{
- ((unsigned char *)s)[1] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[0] = T8(u) ;
+ u = uint16_big(u) ;
+ memcpy(s, &u, sizeof(uint16_t)) ;
}
diff --git a/src/libstddjb/uint16_reverse.c b/src/libstddjb/uint16_reverse.c
index b4fe2c8..4fa0a7b 100644
--- a/src/libstddjb/uint16_reverse.c
+++ b/src/libstddjb/uint16_reverse.c
@@ -1,5 +1,6 @@
/* ISC license. */
+#include <string.h>
#include <skalibs/uint16.h>
void uint16_reverse (char *s, size_t n)
diff --git a/src/libstddjb/uint16_unpack.c b/src/libstddjb/uint16_unpack.c
index 1497671..ea31ebc 100644
--- a/src/libstddjb/uint16_unpack.c
+++ b/src/libstddjb/uint16_unpack.c
@@ -1,12 +1,12 @@
/* ISC license. */
#include <stdint.h>
+#include <string.h>
#include <skalibs/uint16.h>
-#include <skalibs/bytestr.h>
void uint16_unpack (char const *s, uint16_t *u)
{
- uint16_t r = T8((unsigned char)s[1]) ; r <<= 8 ;
- r += T8((unsigned char)s[0]) ;
- *u = r ;
+ uint16_t x ;
+ memcpy(&x, s, sizeof(uint16_t)) ;
+ *u = uint16_little(x) ;
}
diff --git a/src/libstddjb/uint16_unpack_big.c b/src/libstddjb/uint16_unpack_big.c
index 73b94da..3f6f0df 100644
--- a/src/libstddjb/uint16_unpack_big.c
+++ b/src/libstddjb/uint16_unpack_big.c
@@ -1,12 +1,12 @@
/* ISC license. */
#include <stdint.h>
+#include <string.h>
#include <skalibs/uint16.h>
-#include <skalibs/bytestr.h>
void uint16_unpack_big (char const *s, uint16_t *u)
{
- uint16_t r = T8((unsigned char)s[0]) ; r <<= 8 ;
- r += T8((unsigned char)s[1]) ;
- *u = r ;
+ uint16_t x ;
+ memcpy(&x, s, sizeof(uint16_t)) ;
+ *u = uint16_big(x) ;
}
diff --git a/src/libstddjb/uint32_bswap.c b/src/libstddjb/uint32_bswap.c
new file mode 100644
index 0000000..b9dbcab
--- /dev/null
+++ b/src/libstddjb/uint32_bswap.c
@@ -0,0 +1,13 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <skalibs/uint32.h>
+
+uint32_t uint32_bswap (uint32_t a)
+{
+ return
+ (a & 0x000000ffu) << 24 |
+ (a & 0x0000ff00u) << 8 |
+ (a & 0x00ff0000u) >> 8 |
+ (a & 0xff000000u) >> 24 ;
+}
diff --git a/src/libstddjb/uint32_pack.c b/src/libstddjb/uint32_pack.c
index a5320e0..a06ccf1 100644
--- a/src/libstddjb/uint32_pack.c
+++ b/src/libstddjb/uint32_pack.c
@@ -1,12 +1,11 @@
/* ISC license. */
+#include <stdint.h>
+#include <string.h>
#include <skalibs/uint32.h>
-#include <skalibs/bytestr.h>
void uint32_pack (char *s, uint32_t u)
{
- ((unsigned char *)s)[0] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[1] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[2] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[3] = T8(u) ;
+ u = uint32_little(u) ;
+ memcpy(s, &u, sizeof(uint32_t)) ;
}
diff --git a/src/libstddjb/uint32_pack_big.c b/src/libstddjb/uint32_pack_big.c
index 2a17a7b..e861e7e 100644
--- a/src/libstddjb/uint32_pack_big.c
+++ b/src/libstddjb/uint32_pack_big.c
@@ -1,12 +1,11 @@
/* ISC license. */
+#include <stdint.h>
+#include <string.h>
#include <skalibs/uint32.h>
-#include <skalibs/bytestr.h>
void uint32_pack_big (char *s, uint32_t u)
{
- ((unsigned char *)s)[3] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[2] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[1] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[0] = T8(u) ;
+ u = uint32_big(u) ;
+ memcpy(s, &u, sizeof(uint32_t)) ;
}
diff --git a/src/libstddjb/uint32_reverse.c b/src/libstddjb/uint32_reverse.c
index 51c9877..2d3f915 100644
--- a/src/libstddjb/uint32_reverse.c
+++ b/src/libstddjb/uint32_reverse.c
@@ -1,17 +1,17 @@
/* ISC license. */
+#include <stdint.h>
+#include <string.h>
#include <skalibs/uint32.h>
void uint32_reverse (char *s, size_t n)
{
while (n--)
{
- char c = s[0] ;
- s[0] = s[3] ;
- s[3] = c ;
- c = s[1] ;
- s[1] = s[2] ;
- s[2] = c ;
- s += 4 ;
+ uint32_t x ;
+ memcpy(&x, s, sizeof(uint32_t)) ;
+ x = uint32_bswap(x) ;
+ memcpy(s, &x, sizeof(uint32_t)) ;
+ s += sizeof(uint32_t) ;
}
}
diff --git a/src/libstddjb/uint32_unpack.c b/src/libstddjb/uint32_unpack.c
index a9cabbb..78aca79 100644
--- a/src/libstddjb/uint32_unpack.c
+++ b/src/libstddjb/uint32_unpack.c
@@ -1,14 +1,12 @@
/* ISC license. */
#include <stdint.h>
+#include <string.h>
#include <skalibs/uint32.h>
-#include <skalibs/bytestr.h>
void uint32_unpack (char const *s, uint32_t *u)
{
- uint32_t r = T8((unsigned char)s[3]) ; r <<= 8 ;
- r += T8((unsigned char)s[2]) ; r <<= 8 ;
- r += T8((unsigned char)s[1]) ; r <<= 8 ;
- r += T8((unsigned char)s[0]) ;
- *u = r ;
+ uint32_t x ;
+ memcpy(&x, s, sizeof(uint32_t)) ;
+ *u = uint32_little(x) ;
}
diff --git a/src/libstddjb/uint32_unpack_big.c b/src/libstddjb/uint32_unpack_big.c
index defde56..307141f 100644
--- a/src/libstddjb/uint32_unpack_big.c
+++ b/src/libstddjb/uint32_unpack_big.c
@@ -1,14 +1,12 @@
/* ISC license. */
#include <stdint.h>
+#include <string.h>
#include <skalibs/uint32.h>
-#include <skalibs/bytestr.h>
void uint32_unpack_big (char const *s, uint32_t *u)
{
- uint32_t r = T8((unsigned char)s[0]) ; r <<= 8 ;
- r += T8((unsigned char)s[1]) ; r <<= 8 ;
- r += T8((unsigned char)s[2]) ; r <<= 8 ;
- r += T8((unsigned char)s[3]) ;
- *u = r ;
+ uint32_t x ;
+ memcpy(&x, s, sizeof(uint32_t)) ;
+ *u = uint32_big(x) ;
}
diff --git a/src/libstddjb/uint64_bswap.c b/src/libstddjb/uint64_bswap.c
new file mode 100644
index 0000000..3dfaf18
--- /dev/null
+++ b/src/libstddjb/uint64_bswap.c
@@ -0,0 +1,16 @@
+/* ISC license. */
+
+#include <skalibs/uint64.h>
+
+uint64_t uint64_bswap (uint64_t a)
+{
+ return
+ (a & 0x00000000000000ffull) << 56 |
+ (a & 0x000000000000ff00ull) << 40 |
+ (a & 0x0000000000ff0000ull) << 24 |
+ (a & 0x00000000ff000000ull) << 8 |
+ (a & 0x000000ff00000000ull) >> 8 |
+ (a & 0x0000ff0000000000ull) >> 24 |
+ (a & 0x00ff000000000000ull) >> 40 |
+ (a & 0xff00000000000000ull) >> 56 ;
+}
diff --git a/src/libstddjb/uint64_pack.c b/src/libstddjb/uint64_pack.c
index 5559a90..18033da 100644
--- a/src/libstddjb/uint64_pack.c
+++ b/src/libstddjb/uint64_pack.c
@@ -1,16 +1,10 @@
/* ISC license. */
+#include <string.h>
#include <skalibs/uint64.h>
-#include <skalibs/bytestr.h>
void uint64_pack (char *s, uint64_t u)
{
- ((unsigned char *)s)[0] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[1] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[2] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[3] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[4] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[5] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[6] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[7] = T8(u) ;
+ u = uint64_little(u) ;
+ memcpy(s, &u, sizeof(uint64_t)) ;
}
diff --git a/src/libstddjb/uint64_pack_big.c b/src/libstddjb/uint64_pack_big.c
index 381f105..c0bbcd7 100644
--- a/src/libstddjb/uint64_pack_big.c
+++ b/src/libstddjb/uint64_pack_big.c
@@ -1,16 +1,10 @@
/* ISC license. */
+#include <string.h>
#include <skalibs/uint64.h>
-#include <skalibs/bytestr.h>
void uint64_pack_big (char *s, uint64_t u)
{
- ((unsigned char *)s)[7] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[6] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[5] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[4] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[3] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[2] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[1] = T8(u) ; u >>= 8 ;
- ((unsigned char *)s)[0] = T8(u) ;
+ u = uint64_big(u) ;
+ memcpy(s, &u, sizeof(uint64_t)) ;
}
diff --git a/src/libstddjb/uint64_reverse.c b/src/libstddjb/uint64_reverse.c
index 5a616f5..a870b31 100644
--- a/src/libstddjb/uint64_reverse.c
+++ b/src/libstddjb/uint64_reverse.c
@@ -1,15 +1,16 @@
/* ISC license. */
+#include <string.h>
#include <skalibs/uint64.h>
void uint64_reverse (char *s, size_t n)
{
while (n--)
{
- char c = s[0] ; s[0] = s[7] ; s[7] = c ;
- c = s[1] ; s[1] = s[6] ; s[6] = c ;
- c = s[2] ; s[2] = s[5] ; s[5] = c ;
- c = s[3] ; s[3] = s[4] ; s[4] = c ;
- s += 8 ;
+ uint64_t x ;
+ memcpy(&x, s, sizeof(uint64_t)) ;
+ x = uint64_bswap(x) ;
+ memcpy(s, &x, sizeof(uint64_t)) ;
+ s += sizeof(uint64_t) ;
}
}
diff --git a/src/libstddjb/uint64_unpack.c b/src/libstddjb/uint64_unpack.c
index 452d3ea..ca7aad9 100644
--- a/src/libstddjb/uint64_unpack.c
+++ b/src/libstddjb/uint64_unpack.c
@@ -1,17 +1,11 @@
/* ISC license. */
+#include <string.h>
#include <skalibs/uint64.h>
-#include <skalibs/bytestr.h>
void uint64_unpack (char const *s, uint64_t *u)
{
- uint64_t r = T8((unsigned char)s[7]) ; r <<= 8 ;
- r += T8((unsigned char)s[6]) ; r <<= 8 ;
- r += T8((unsigned char)s[5]) ; r <<= 8 ;
- r += T8((unsigned char)s[4]) ; r <<= 8 ;
- r += T8((unsigned char)s[3]) ; r <<= 8 ;
- r += T8((unsigned char)s[2]) ; r <<= 8 ;
- r += T8((unsigned char)s[1]) ; r <<= 8 ;
- r += T8((unsigned char)s[0]) ;
- *u = r ;
+ uint64_t x ;
+ memcpy(&x, s, sizeof(uint64_t)) ;
+ *u = uint64_little(x) ;
}
diff --git a/src/libstddjb/uint64_unpack_big.c b/src/libstddjb/uint64_unpack_big.c
index 3554dcc..f4e7d52 100644
--- a/src/libstddjb/uint64_unpack_big.c
+++ b/src/libstddjb/uint64_unpack_big.c
@@ -1,17 +1,11 @@
/* ISC license. */
+#include <string.h>
#include <skalibs/uint64.h>
-#include <skalibs/bytestr.h>
void uint64_unpack_big (char const *s, uint64_t *u)
{
- uint64_t r = T8((unsigned char)s[0]) ; r <<= 8 ;
- r += T8((unsigned char)s[1]) ; r <<= 8 ;
- r += T8((unsigned char)s[2]) ; r <<= 8 ;
- r += T8((unsigned char)s[3]) ; r <<= 8 ;
- r += T8((unsigned char)s[4]) ; r <<= 8 ;
- r += T8((unsigned char)s[5]) ; r <<= 8 ;
- r += T8((unsigned char)s[6]) ; r <<= 8 ;
- r += T8((unsigned char)s[7]) ;
- *u = r ;
+ uint64_t x ;
+ memcpy(&x, s, sizeof(uint64_t)) ;
+ *u = uint64_big(x) ;
}