blob: 8657bbef59c198fc17182af699c19664ef8402fc [file] [log] [blame]
Neels Hofmeyr6862cd32020-09-13 23:56:21 +02001#include <inttypes.h>
2#include <osmocom/core/bit16gen.h>
3#include <osmocom/core/bit32gen.h>
4#include <osmocom/core/bit64gen.h>
5
6#define DO_TEST(BE_LE, SIZE) do { \
7 int8_t len; \
8 printf("--- " #SIZE " " #BE_LE "\n"); \
9 for (len = SIZE / 8; len > 0; len--) { \
10 uint8_t buf[len * 2]; \
11 uint8_t at_idx; \
12 uint##SIZE##_t val = (uint##SIZE##_t)0x8877665544332211; \
13 \
14 for (at_idx = 0; at_idx < len; at_idx++) { \
15 uint##SIZE##_t read_val = 0; \
16 memset(buf, 0, sizeof(buf)); \
17 osmo_store##SIZE####BE_LE##_ext(val, &buf[at_idx], len); \
18 printf("osmo_store" #SIZE #BE_LE "_ext(0x%" PRIx##SIZE ", &buf[%d], %d) = %s\n", \
19 val, \
20 at_idx, len, osmo_hexdump(buf, sizeof(buf))); \
21 \
22 read_val = osmo_load##SIZE####BE_LE##_ext(&buf[at_idx], len); \
23 printf("osmo_load" #SIZE #BE_LE "_ext(&buf[%d], %d) = 0x%" PRIx##SIZE "\n", \
24 at_idx, len, read_val); \
Neels Hofmeyrf6db7652020-09-14 00:39:37 +020025 \
26 if (!strcmp(#BE_LE, "be")) { \
27 read_val = osmo_load##SIZE####BE_LE##_ext_2(&buf[at_idx], len); \
28 printf("osmo_load" #SIZE #BE_LE "_ext_2(&buf[%d], %d) = 0x%" PRIx##SIZE "\n", \
29 at_idx, len, read_val); \
30 } \
Neels Hofmeyr6862cd32020-09-13 23:56:21 +020031 } \
32 } \
33 } while (0)
34
Neels Hofmeyrf6db7652020-09-14 00:39:37 +020035/* Shims to allow compiling, the *le_ext_2 are not actually invoked because of the strcmp() condition above. */
36#define osmo_load16le_ext_2 dummy
37#define osmo_load32le_ext_2 dummy
38#define osmo_load64le_ext_2 dummy
39
40static inline uint64_t dummy(const void *p, uint8_t n)
41{
42 OSMO_ASSERT(false);
43}
Neels Hofmeyr6862cd32020-09-13 23:56:21 +020044
45int main(int argc, char **argv)
46{
47 DO_TEST(be, 16);
48 DO_TEST(le, 16);
49 DO_TEST(be, 32);
50 DO_TEST(le, 32);
51 DO_TEST(be, 64);
52 DO_TEST(le, 64);
53
54 {
Neels Hofmeyrf6db7652020-09-14 00:39:37 +020055 printf("--- store/load 0x112233 as 24bit big-endian, legacy\n");
Neels Hofmeyr6862cd32020-09-13 23:56:21 +020056 uint8_t buf[4];
57 memset(buf, 0, sizeof(buf));
58 osmo_store32be_ext(0x00112233, buf, 3); // stores 11 22 33
59 printf("%s\n", osmo_hexdump(buf, 4));
60 uint32_t r = osmo_load32be_ext(buf, 3); // returns 0x11223300, not 0x00112233
61 printf("0x%x\n", r);
62 }
63
64 {
Neels Hofmeyrf6db7652020-09-14 00:39:37 +020065 printf("--- store/load 0x112233 as 24bit big-endian\n");
66 uint8_t buf[4];
67 memset(buf, 0, sizeof(buf));
68 osmo_store32be_ext(0x00112233, buf, 3); // stores 11 22 33
69 printf("%s\n", osmo_hexdump(buf, 4));
70 uint32_t r = osmo_load32be_ext_2(buf, 3); // returns 0x00112233
71 printf("0x%x\n", r);
72 }
73
74 {
Neels Hofmeyr6862cd32020-09-13 23:56:21 +020075 printf("--- store/load 0x112233 as 24bit little-endian\n");
76 uint8_t buf[4];
77 memset(buf, 0, sizeof(buf));
78 osmo_store32le_ext(0x00112233, buf, 3); // stores 33 22 11
79 printf("%s\n", osmo_hexdump(buf, 4));
80 uint32_t r = osmo_load32le_ext(buf, 3); // returns 0x00112233
81 printf("0x%x\n", r);
82 }
83
84 return 0;
85}