Neels Hofmeyr | 6862cd3 | 2020-09-13 23:56:21 +0200 | [diff] [blame] | 1 | #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)); \ |
Eric | 555c293 | 2022-12-19 18:01:58 +0100 | [diff] [blame] | 17 | osmo_store##SIZE##BE_LE##_ext(val, &buf[at_idx], len); \ |
Neels Hofmeyr | 6862cd3 | 2020-09-13 23:56:21 +0200 | [diff] [blame] | 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 | \ |
Eric | 555c293 | 2022-12-19 18:01:58 +0100 | [diff] [blame] | 22 | read_val = osmo_load##SIZE##BE_LE##_ext(&buf[at_idx], len); \ |
Neels Hofmeyr | 6862cd3 | 2020-09-13 23:56:21 +0200 | [diff] [blame] | 23 | printf("osmo_load" #SIZE #BE_LE "_ext(&buf[%d], %d) = 0x%" PRIx##SIZE "\n", \ |
| 24 | at_idx, len, read_val); \ |
Neels Hofmeyr | f6db765 | 2020-09-14 00:39:37 +0200 | [diff] [blame] | 25 | \ |
| 26 | if (!strcmp(#BE_LE, "be")) { \ |
Eric | 555c293 | 2022-12-19 18:01:58 +0100 | [diff] [blame] | 27 | read_val = osmo_load##SIZE##BE_LE##_ext_2(&buf[at_idx], len); \ |
Neels Hofmeyr | f6db765 | 2020-09-14 00:39:37 +0200 | [diff] [blame] | 28 | printf("osmo_load" #SIZE #BE_LE "_ext_2(&buf[%d], %d) = 0x%" PRIx##SIZE "\n", \ |
| 29 | at_idx, len, read_val); \ |
| 30 | } \ |
Neels Hofmeyr | 6862cd3 | 2020-09-13 23:56:21 +0200 | [diff] [blame] | 31 | } \ |
| 32 | } \ |
| 33 | } while (0) |
| 34 | |
Neels Hofmeyr | f6db765 | 2020-09-14 00:39:37 +0200 | [diff] [blame] | 35 | /* 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 | |
| 40 | static inline uint64_t dummy(const void *p, uint8_t n) |
| 41 | { |
| 42 | OSMO_ASSERT(false); |
| 43 | } |
Neels Hofmeyr | 6862cd3 | 2020-09-13 23:56:21 +0200 | [diff] [blame] | 44 | |
| 45 | int 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 Hofmeyr | f6db765 | 2020-09-14 00:39:37 +0200 | [diff] [blame] | 55 | printf("--- store/load 0x112233 as 24bit big-endian, legacy\n"); |
Neels Hofmeyr | 6862cd3 | 2020-09-13 23:56:21 +0200 | [diff] [blame] | 56 | 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 Hofmeyr | f6db765 | 2020-09-14 00:39:37 +0200 | [diff] [blame] | 65 | 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 Hofmeyr | 6862cd3 | 2020-09-13 23:56:21 +0200 | [diff] [blame] | 75 | 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 | } |