blob: 0c9821e22cf53643c6820e2b510a3ec74f54ff44 [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); \
25 } \
26 } \
27 } while (0)
28
29
30int main(int argc, char **argv)
31{
32 DO_TEST(be, 16);
33 DO_TEST(le, 16);
34 DO_TEST(be, 32);
35 DO_TEST(le, 32);
36 DO_TEST(be, 64);
37 DO_TEST(le, 64);
38
39 {
40 printf("--- store/load 0x112233 as 24bit big-endian\n");
41 uint8_t buf[4];
42 memset(buf, 0, sizeof(buf));
43 osmo_store32be_ext(0x00112233, buf, 3); // stores 11 22 33
44 printf("%s\n", osmo_hexdump(buf, 4));
45 uint32_t r = osmo_load32be_ext(buf, 3); // returns 0x11223300, not 0x00112233
46 printf("0x%x\n", r);
47 }
48
49 {
50 printf("--- store/load 0x112233 as 24bit little-endian\n");
51 uint8_t buf[4];
52 memset(buf, 0, sizeof(buf));
53 osmo_store32le_ext(0x00112233, buf, 3); // stores 33 22 11
54 printf("%s\n", osmo_hexdump(buf, 4));
55 uint32_t r = osmo_load32le_ext(buf, 3); // returns 0x00112233
56 printf("0x%x\n", r);
57 }
58
59 return 0;
60}