From 679b8bdb5c40ca1909b025db10ddb5a98967c60e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 20 Dec 2015 14:31:40 +0100 Subject: optimise implementations of {hton,ntoh}{s,l,ll} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- gen/bits/intconf.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ gen/bits/intconf.h | 18 ++++++++++++ 2 files changed, 100 insertions(+) (limited to 'gen/bits') diff --git a/gen/bits/intconf.c b/gen/bits/intconf.c index 5a52d12..361dd16 100644 --- a/gen/bits/intconf.c +++ b/gen/bits/intconf.c @@ -139,6 +139,82 @@ static int fast(int bits) #undef TEST +/** + * For a 16-bit integer with the value 0x0102, print, + * to a string, the bytes it is constructed by one the + * host machine, in storage order. + * + * @return The bytes in the integer juxtaposed. + */ +static char* byteorder_16(void) +{ + static char buf[16 / 4 + 1]; + union + { + short int all; + struct + { + char a; char b; + }; + } test = { .all = 0x0102 }; + + sprintf(buf, "%02i%02i", test.a, test.b); + return buf; +} + + +/** + * For a 32-bit integer with the value 0x01020304, print, + * to a string, the bytes it is constructed by one the + * host machine, in storage order. + * + * @return The bytes in the integer juxtaposed. + */ +static char* byteorder_32(void) +{ + static char buf[32 / 4 + 1]; + union + { + int all; + struct + { + char a; char b; char c; char d; + }; + } test = { .all = 0x01020304 }; + + sprintf(buf, "%02i%02i%02i%02i", + test.a, test.b, test.c, test.d); + return buf; +} + + +/** + * For a 16-bit integer with the value 0x0102030405060708, + * print, to a string, the bytes it is constructed by one + * the host machine, in storage order. + * + * @return The bytes in the integer juxtaposed. + */ +static char* byteorder_64(void) +{ + static char buf[64 / 4 + 1]; + union + { + long long int all; + struct + { + char a; char b; char c; char d; + char e; char f; char g; char h; + }; + } test = { .all = 0x0102030405060708LL }; + + sprintf(buf, "%02i%02i%02i%02i%02i%02i%02i%02i", + test.a, test.b, test.c, test.d, + test.e, test.f, test.g, test.h); + return buf; +} + + /** * @param argc The number of command line arguments, should * be either 1 (print integer width information) @@ -167,12 +243,18 @@ int main(int argc, char* argv[]) r |= printf("PTR_BIT %zu\n", 8 * sizeof(void*)); r |= printf("WCHAR_BIT %zu\n", 8 * sizeof(L'\0')); + /* Print byte orders. */ + r |= printf("INT16_BYTEORDER 0x%s\n", byteorder_16()); + r |= printf("INT32_BYTEORDER 0x%s\n", byteorder_32()); + r |= printf("INT64_BYTEORDER 0x%sLL\n", byteorder_64()); + /* Print the intrinsic type for specific numbers of bits. */ r |= printf("INT%zu %s\n", 8 * sizeof(char), "char"); r |= printf("INT%zu %s\n", 8 * sizeof(short int), "short int"); r |= printf("INT%zu %s\n", 8 * sizeof(int), "int"); r |= printf("INT%zu %s\n", 8 * sizeof(long int), "long int"); r |= printf("INT%zu %s\n", 8 * sizeof(long long int), "long long int"); + return r < 0 ? 1 : 0; } else if (argc == 2) diff --git a/gen/bits/intconf.h b/gen/bits/intconf.h index 69ea473..4d22435 100644 --- a/gen/bits/intconf.h +++ b/gen/bits/intconf.h @@ -96,6 +96,24 @@ */ #define __WCHAR_BIT //(bin/gen/bits/intconf | grep ^WCHAR_BIT | sed "s/^[^ ]* //") +/** + * The 16-bit integer 0x0102 but with the + * bytes swapped to the storage order. + */ +#define __INT16_BYTEORDER //(bin/gen/bits/intconf | grep ^INT16_BYTEORDER | sed "s/^[^ ]* //") + +/** + * The 32-bit integer 0x01020304 but with + * the bytes swapped to the storage order. + */ +#define __INT32_BYTEORDER //(bin/gen/bits/intconf | grep ^INT32_BYTEORDER | sed "s/^[^ ]* //") + +/** + * The 64-bit integer 0x0102030405060708 but with + * the bytes swapped to the storage order. + */ +#define __INT64_BYTEORDER //(bin/gen/bits/intconf | grep ^INT64_BYTEORDER | sed "s/^[^ ]* //") + /** * The underlaying intrinsic type for `int8_t` or `uint8_t`. */ -- cgit v1.2.3-70-g09d2