Commit Graph

3 Commits

Author SHA1 Message Date
Eric Wild 555c293867 bitgen test: fix concat macro
..compilers might not like concatenating "nothing", and it looks weird.

Change-Id: Ibcd49a5c5c4f4fd0406fa1697edfd59307eac6f7
2023-01-11 09:01:18 +00:00
Neels Hofmeyr f6db765327 bitXXgen: add osmo_loadXXbe_ext_2() to get right-adjusted values
As shown in the recently added bitgen_test.c, using osmo_loadXXbe_ext() with a
smaller n produces results aligned on the most significant bytes, which is
cumbersome, since it does not return a previously stored value. This problem
exists only for the big-endian functions, the little-endian osmo_loadXXle_ext()
properly return values adjusted on the least significant octets.

Add osmo_loadXXbe_ext_2() variants that properly right-adjust the returned
value. Prominently highlight this behavior in API doc. Test the new functions
in bitgen_test.c.

For example, this eases handling of 24bit integers (e.g. loaded from buffer to
uint32_t, and stored into buffer from uint32_t). Also explicitly show this 24
bit case in bitgen_test.c

Change-Id: I2806df6f0f7bf1ad705d52fa386d4525b892b928
2020-09-14 11:53:46 +00:00
Neels Hofmeyr 6862cd38a5 bitXXgen: add bitgen_test.c
The autogenerated bitXXgen.h headers for osmo_load16le_ext() thru
osmo_store64_be() are not actually tested at all. Add a test.

The test output shows that the osmo_load*be_ext for a shorter len do not return
nicely matching results. A practical example showing the difficulty in storing
and loading 24bit integer values as/from big-endian:

	uint8_t buf[4];
	memset(buf, 0, sizeof(buf));
	osmo_store32be_ext(0x00112233, buf, 3); // stores 11 22 33
	printf("%s\n", osmo_hexdump(buf, 4));
	uint32_t r = osmo_load32be_ext(buf, 3); // returns 0x11223300, not 0x00112233
	printf("0x%x\n", r);

output is:

	11 22 33 00
	0x11223300

In contrast, the little-endian variant properly aligns the loaded bytes on the
least significant octet:

	uint8_t buf[4];
	memset(buf, 0, sizeof(buf));
	osmo_store32le_ext(0x00112233, buf, 3); // stores 33 22 11
	printf("%s\n", osmo_hexdump(buf, 4));
	uint32_t r = osmo_load32le_ext(buf, 3); // returns 0x00112233 as expected
	printf("0x%x\n", r);

output for le is:

	33 22 11 00
	0x112233

Change-Id: I5542ace54376a206aa8574812d4c742c86c293b4
2020-09-14 11:53:46 +00:00