Implements C transpiler for pack function.

This commit is contained in:
2020-12-31 00:00:00 +00:00
parent 899aceb118
commit bbc8af1a5e
5 changed files with 196 additions and 26 deletions

36
c/pack.h Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define pack_Nbit_(N) \
static inline void biner_pack_l##N(const int##N##_t* v, uint8_t* c, size_t s) { \
assert(s < N/8); \
*c = (*v >> s*8) & 0xff; \
} \
static inline void biner_pack_b##N(const int##N##_t* v, uint8_t* c, size_t s) { \
assert(s < N/8); \
biner_pack_l##N(v, c, N/8-s-1); \
}
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define pack_Nbit_(N) \
static inline void biner_pack_b##N(const int##N##_t* v, uint8_t* c, size_t s) { \
assert(s < N/8); \
*c = (*v >> s*8) & 0xff; \
} \
static inline void biner_pack_l##N(const int##N##_t* v, uint8_t* c, size_t s) { \
assert(s < N/8); \
biner_pack_b##N(v, c, N/8-s-1); \
}
#else
# error "byte order unknown"
#endif
pack_Nbit_(8);
pack_Nbit_(16);
pack_Nbit_(32);
pack_Nbit_(64);
#undef pack_Nbit_

View File

@@ -8,7 +8,7 @@
# define unpack_Nbit_(N) \
static inline void biner_unpack_l##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \
*v = (*v & (0xff << (s*8))) | (c << (s*8)); \
*v = (*v & ~((uint##N##_t) 0xff << (s*8))) | (c << (s*8)); \
} \
static inline void biner_unpack_b##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \
@@ -18,7 +18,7 @@
# define unpack_Nbit_(N) \
static inline void biner_unpack_b##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \
*v = (*v & (0xff << (s*8))) | (c << (s*8)); \
*v = (*v & ~((uint##N_t) 0xff << (s*8))) | (c << (s*8)); \
} \
static inline void biner_unpack_l##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \