Created custom functions to convert (h)words to and from bytecode format
Instead of using endian.h that is not portable AND doesn't work with C++, I'll just write my own using a forced union based type punning trick. I've decided to use little endian for the format as well: it seems to be used by most desktop computers so it should make these functions faster to run for most CPUs.
This commit is contained in:
72
lib/base.c
72
lib/base.c
@@ -14,23 +14,87 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
union hword_pun
|
||||
{
|
||||
hword h;
|
||||
byte bytes[HWORD_SIZE];
|
||||
};
|
||||
|
||||
union word_pun
|
||||
{
|
||||
word h;
|
||||
byte bytes[WORD_SIZE];
|
||||
};
|
||||
|
||||
hword hword_htobc(hword w)
|
||||
{
|
||||
#if __LITTLE_ENDIAN__
|
||||
return w;
|
||||
#else
|
||||
union hword_pun x = {w};
|
||||
union hword_pun y = {0};
|
||||
for (size_t i = 0, j = HWORD_SIZE; i < HWORD_SIZE; ++i, --j)
|
||||
y.bytes[j - 1] = x.bytes[i];
|
||||
return y.h;
|
||||
#endif
|
||||
}
|
||||
|
||||
hword hword_bctoh(hword w)
|
||||
{
|
||||
#if __LITTLE_ENDIAN__
|
||||
return w;
|
||||
#else
|
||||
union hword_pun x = {w};
|
||||
union hword_pun y = {0};
|
||||
for (size_t i = 0, j = HWORD_SIZE; i < HWORD_SIZE; ++i, --j)
|
||||
y.bytes[j - 1] = x.bytes[i];
|
||||
return y.h;
|
||||
#endif
|
||||
}
|
||||
|
||||
word word_htobc(word w)
|
||||
{
|
||||
#if __LITTLE_ENDIAN__
|
||||
return w;
|
||||
#else
|
||||
union word_pun x = {w};
|
||||
union word_pun y = {0};
|
||||
for (size_t i = 0, j = WORD_SIZE; i < WORD_SIZE; ++i, --j)
|
||||
y.bytes[j - 1] = x.bytes[i];
|
||||
return y.h;
|
||||
#endif
|
||||
}
|
||||
|
||||
word word_bctoh(word w)
|
||||
{
|
||||
#if __LITTLE_ENDIAN__
|
||||
return w;
|
||||
#else
|
||||
union word_pun x = {w};
|
||||
union word_pun y = {0};
|
||||
for (size_t i = 0, j = WORD_SIZE; i < WORD_SIZE; ++i, --j)
|
||||
y.bytes[j - 1] = x.bytes[i];
|
||||
return y.h;
|
||||
#endif
|
||||
}
|
||||
|
||||
hword convert_bytes_to_hword(byte *bytes)
|
||||
{
|
||||
hword be_h = 0;
|
||||
memcpy(&be_h, bytes, HWORD_SIZE);
|
||||
hword h = be32toh(be_h);
|
||||
hword h = hword_bctoh(be_h);
|
||||
return h;
|
||||
}
|
||||
|
||||
void convert_hword_to_bytes(hword w, byte *bytes)
|
||||
{
|
||||
hword be_h = htobe32(w);
|
||||
hword be_h = hword_htobc(w);
|
||||
memcpy(bytes, &be_h, HWORD_SIZE);
|
||||
}
|
||||
|
||||
void convert_word_to_bytes(word w, byte *bytes)
|
||||
{
|
||||
word be_w = htobe64(w);
|
||||
word be_w = word_htobc(w);
|
||||
memcpy(bytes, &be_w, WORD_SIZE);
|
||||
}
|
||||
|
||||
@@ -38,6 +102,6 @@ word convert_bytes_to_word(byte *bytes)
|
||||
{
|
||||
word be_w = 0;
|
||||
memcpy(&be_w, bytes, WORD_SIZE);
|
||||
word w = be64toh(be_w);
|
||||
word w = word_bctoh(be_w);
|
||||
return w;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user