summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-11-04 19:52:04 +0100
committerTom Gundersen <teg@jklm.no>2014-11-05 16:54:22 +0100
commit617e79465249e694d697fd78704571b3d00338c8 (patch)
tree10b6c91f2a508c9982cd218ed92b6a80682e5f21
parent9eec671331adeb3f9a81b390758000364388a042 (diff)
shared: unaligned - use void* instead of unaligned be16_t*
-rw-r--r--src/shared/unaligned.h48
-rw-r--r--src/test/test-unaligned.c57
2 files changed, 58 insertions, 47 deletions
diff --git a/src/shared/unaligned.h b/src/shared/unaligned.h
index b194d71017..d6181dd9a9 100644
--- a/src/shared/unaligned.h
+++ b/src/shared/unaligned.h
@@ -23,34 +23,44 @@
#include <stdint.h>
-#include "sparse-endian.h"
+static inline uint16_t unaligned_read_be16(const void *_u) {
+ const uint8_t *u = _u;
-static inline uint16_t unaligned_read_be16(const be16_t *u) {
- return (((uint16_t) ((uint8_t*) u)[0]) << 8) |
- ((uint16_t) ((uint8_t*) u)[1]);
+ return (((uint16_t) u[0]) << 8) |
+ ((uint16_t) u[1]);
}
-static inline uint32_t unaligned_read_be32(const be32_t *u) {
- return (((uint32_t) unaligned_read_be16((be16_t*) u)) << 16) |
- ((uint32_t) unaligned_read_be16((be16_t*) u + 1));
+static inline uint32_t unaligned_read_be32(const void *_u) {
+ const uint8_t *u = _u;
+
+ return (((uint32_t) unaligned_read_be16(u)) << 16) |
+ ((uint32_t) unaligned_read_be16(u + 2));
}
-static inline uint64_t unaligned_read_be64(const be64_t *u) {
- return (((uint64_t) unaligned_read_be32((be32_t*) u)) << 32) |
- ((uint64_t) unaligned_read_be32((be32_t*) u + 1));
+static inline uint64_t unaligned_read_be64(const void *_u) {
+ const uint8_t *u = _u;
+
+ return (((uint64_t) unaligned_read_be32(u)) << 32) |
+ ((uint64_t) unaligned_read_be32(u + 4));
}
-static inline void unaligned_write_be16(be16_t *u, uint16_t a) {
- ((uint8_t*) u)[0] = (uint8_t) (a >> 8);
- ((uint8_t*) u)[1] = (uint8_t) a;
+static inline void unaligned_write_be16(void *_u, uint16_t a) {
+ uint8_t *u = _u;
+
+ u[0] = (uint8_t) (a >> 8);
+ u[1] = (uint8_t) a;
}
-static inline void unaligned_write_be32(be32_t *u, uint32_t a) {
- unaligned_write_be16((be16_t*) u, (uint16_t) (a >> 16));
- unaligned_write_be16((be16_t*) u + 1, (uint16_t) a);
+static inline void unaligned_write_be32(void *_u, uint32_t a) {
+ uint8_t *u = _u;
+
+ unaligned_write_be16(u, (uint16_t) (a >> 16));
+ unaligned_write_be16(u + 2, (uint16_t) a);
}
-static inline void unaligned_write_be64(be64_t *u, uint64_t a) {
- unaligned_write_be32((be32_t*) u, (uint32_t) (a >> 32));
- unaligned_write_be32((be32_t*) u + 1, (uint32_t) a);
+static inline void unaligned_write_be64(void *_u, uint64_t a) {
+ uint8_t *u = _u;
+
+ unaligned_write_be32(u, (uint32_t) (a >> 32));
+ unaligned_write_be32(u + 4, (uint32_t) a);
}
diff --git a/src/test/test-unaligned.c b/src/test/test-unaligned.c
index 52693cd325..1754d06b2d 100644
--- a/src/test/test-unaligned.c
+++ b/src/test/test-unaligned.c
@@ -18,6 +18,7 @@
***/
#include "unaligned.h"
+#include "sparse-endian.h"
#include "util.h"
static uint8_t data[] = {
@@ -28,65 +29,65 @@ static uint8_t data[] = {
int main(int argc, const char *argv[]) {
uint8_t scratch[16];
- assert_se(unaligned_read_be16((be16_t*)&data[0]) == 0x0001);
- assert_se(unaligned_read_be16((be16_t*)&data[1]) == 0x0102);
+ assert_se(unaligned_read_be16(&data[0]) == 0x0001);
+ assert_se(unaligned_read_be16(&data[1]) == 0x0102);
- assert_se(unaligned_read_be32((be32_t*)&data[0]) == 0x00010203);
- assert_se(unaligned_read_be32((be32_t*)&data[1]) == 0x01020304);
- assert_se(unaligned_read_be32((be32_t*)&data[2]) == 0x02030405);
- assert_se(unaligned_read_be32((be32_t*)&data[3]) == 0x03040506);
+ assert_se(unaligned_read_be32(&data[0]) == 0x00010203);
+ assert_se(unaligned_read_be32(&data[1]) == 0x01020304);
+ assert_se(unaligned_read_be32(&data[2]) == 0x02030405);
+ assert_se(unaligned_read_be32(&data[3]) == 0x03040506);
- assert_se(unaligned_read_be64((be64_t*)&data[0]) == 0x0001020304050607);
- assert_se(unaligned_read_be64((be64_t*)&data[1]) == 0x0102030405060708);
- assert_se(unaligned_read_be64((be64_t*)&data[2]) == 0x0203040506070809);
- assert_se(unaligned_read_be64((be64_t*)&data[3]) == 0x030405060708090a);
- assert_se(unaligned_read_be64((be64_t*)&data[4]) == 0x0405060708090a0b);
- assert_se(unaligned_read_be64((be64_t*)&data[5]) == 0x05060708090a0b0c);
- assert_se(unaligned_read_be64((be64_t*)&data[6]) == 0x060708090a0b0c0d);
- assert_se(unaligned_read_be64((be64_t*)&data[7]) == 0x0708090a0b0c0d0e);
+ assert_se(unaligned_read_be64(&data[0]) == 0x0001020304050607);
+ assert_se(unaligned_read_be64(&data[1]) == 0x0102030405060708);
+ assert_se(unaligned_read_be64(&data[2]) == 0x0203040506070809);
+ assert_se(unaligned_read_be64(&data[3]) == 0x030405060708090a);
+ assert_se(unaligned_read_be64(&data[4]) == 0x0405060708090a0b);
+ assert_se(unaligned_read_be64(&data[5]) == 0x05060708090a0b0c);
+ assert_se(unaligned_read_be64(&data[6]) == 0x060708090a0b0c0d);
+ assert_se(unaligned_read_be64(&data[7]) == 0x0708090a0b0c0d0e);
zero(scratch);
- unaligned_write_be16((uint16_t*)&scratch[0], 0x0001);
+ unaligned_write_be16(&scratch[0], 0x0001);
assert_se(memcmp(&scratch[0], &data[0], sizeof(uint16_t)) == 0);
zero(scratch);
- unaligned_write_be16((uint16_t*)&scratch[1], 0x0102);
+ unaligned_write_be16(&scratch[1], 0x0102);
assert_se(memcmp(&scratch[1], &data[1], sizeof(uint16_t)) == 0);
zero(scratch);
- unaligned_write_be32((be32_t*)&scratch[0], 0x00010203);
+ unaligned_write_be32(&scratch[0], 0x00010203);
assert_se(memcmp(&scratch[0], &data[0], sizeof(uint32_t)) == 0);
zero(scratch);
- unaligned_write_be32((be32_t*)&scratch[1], 0x01020304);
+ unaligned_write_be32(&scratch[1], 0x01020304);
assert_se(memcmp(&scratch[1], &data[1], sizeof(uint32_t)) == 0);
zero(scratch);
- unaligned_write_be32((be32_t*)&scratch[2], 0x02030405);
+ unaligned_write_be32(&scratch[2], 0x02030405);
assert_se(memcmp(&scratch[2], &data[2], sizeof(uint32_t)) == 0);
zero(scratch);
- unaligned_write_be32((be32_t*)&scratch[3], 0x03040506);
+ unaligned_write_be32(&scratch[3], 0x03040506);
assert_se(memcmp(&scratch[3], &data[3], sizeof(uint32_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[0], 0x0001020304050607);
+ unaligned_write_be64(&scratch[0], 0x0001020304050607);
assert_se(memcmp(&scratch[0], &data[0], sizeof(uint64_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[1], 0x0102030405060708);
+ unaligned_write_be64(&scratch[1], 0x0102030405060708);
assert_se(memcmp(&scratch[1], &data[1], sizeof(uint64_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[2], 0x0203040506070809);
+ unaligned_write_be64(&scratch[2], 0x0203040506070809);
assert_se(memcmp(&scratch[2], &data[2], sizeof(uint64_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[3], 0x030405060708090a);
+ unaligned_write_be64(&scratch[3], 0x030405060708090a);
assert_se(memcmp(&scratch[3], &data[3], sizeof(uint64_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[4], 0x0405060708090a0b);
+ unaligned_write_be64(&scratch[4], 0x0405060708090a0b);
assert_se(memcmp(&scratch[4], &data[4], sizeof(uint64_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[5], 0x05060708090a0b0c);
+ unaligned_write_be64(&scratch[5], 0x05060708090a0b0c);
assert_se(memcmp(&scratch[5], &data[5], sizeof(uint64_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[6], 0x060708090a0b0c0d);
+ unaligned_write_be64(&scratch[6], 0x060708090a0b0c0d);
assert_se(memcmp(&scratch[6], &data[6], sizeof(uint64_t)) == 0);
zero(scratch);
- unaligned_write_be64((be64_t*)&scratch[7], 0x0708090a0b0c0d0e);
+ unaligned_write_be64(&scratch[7], 0x0708090a0b0c0d0e);
assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0);
}