Skip to content

Commit b26313b

Browse files
committed
channel: Small cleanup
Avoid conditionally compiled code by adding a "is_little_endian()" macro. Signed-off-by: Paul Cercueil <[email protected]>
1 parent eefe69c commit b26313b

File tree

2 files changed

+47
-53
lines changed

2 files changed

+47
-53
lines changed

channel.c

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -409,27 +409,23 @@ static void shift_bits(uint8_t *dst, size_t shift, size_t len, bool left)
409409
size_t i, shift_bytes = shift / 8;
410410
shift %= 8;
411411

412-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
413-
if (!left)
414-
#else
415-
if (left)
416-
#endif
412+
if (is_little_endian() ^ left)
417413
{
418414
if (shift_bytes) {
419415
memmove(dst, dst + shift_bytes, len - shift_bytes);
420416
memset(dst + len - shift_bytes, 0, shift_bytes);
421417
}
422418
if (shift) {
423419
for (i = 0; i < len; i++) {
424-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
425-
dst[i] >>= shift;
426-
if (i < len - 1)
427-
dst[i] |= dst[i + 1] << (8 - shift);
428-
#else
429-
dst[i] <<= shift;
430-
if (i < len - 1)
431-
dst[i] |= dst[i + 1] >> (8 - shift);
432-
#endif
420+
if (is_little_endian()) {
421+
dst[i] >>= shift;
422+
if (i < len - 1)
423+
dst[i] |= dst[i + 1] << (8 - shift);
424+
} else {
425+
dst[i] <<= shift;
426+
if (i < len - 1)
427+
dst[i] |= dst[i + 1] >> (8 - shift);
428+
}
433429
}
434430
}
435431
} else {
@@ -439,15 +435,15 @@ static void shift_bits(uint8_t *dst, size_t shift, size_t len, bool left)
439435
}
440436
if (shift) {
441437
for (i = len; i > 0; i--) {
442-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
443-
dst[i - 1] <<= shift;
444-
if (i > 1)
445-
dst[i - 1] |= dst[i - 2] >> (8 - shift);
446-
#else
447-
dst[i - 1] >>= shift;
448-
if (i > 1)
449-
dst[i - 1] |= dst[i - 2] << (8 - shift);
450-
#endif
438+
if (is_little_endian()) {
439+
dst[i - 1] <<= shift;
440+
if (i > 1)
441+
dst[i - 1] |= dst[i - 2] >> (8 - shift);
442+
} else {
443+
dst[i - 1] >>= shift;
444+
if (i > 1)
445+
dst[i - 1] |= dst[i - 2] << (8 - shift);
446+
}
451447
}
452448
}
453449
}
@@ -458,22 +454,22 @@ static void sign_extend(uint8_t *dst, size_t bits, size_t len)
458454
size_t upper_bytes = ((len * 8 - bits) / 8);
459455
uint8_t msb, msb_bit = 1 << ((bits - 1) % 8);
460456

461-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
462-
msb = dst[len - 1 - upper_bytes] & msb_bit;
463-
if (upper_bytes)
464-
memset(dst + len - upper_bytes, msb ? 0xff : 0x00, upper_bytes);
465-
if (msb)
466-
dst[len - 1 - upper_bytes] |= ~(msb_bit - 1);
467-
else
468-
dst[len - 1 - upper_bytes] &= (msb_bit - 1);
469-
#else
470-
/* XXX: untested */
471-
msb = dst[upper_bytes] & msb_bit;
472-
if (upper_bytes)
473-
memset(dst, msb ? 0xff : 0x00, upper_bytes);
474-
if (msb)
475-
dst[upper_bytes] |= ~(msb_bit - 1);
476-
#endif
457+
if (is_little_endian()) {
458+
msb = dst[len - 1 - upper_bytes] & msb_bit;
459+
if (upper_bytes)
460+
memset(dst + len - upper_bytes, msb ? 0xff : 0x00, upper_bytes);
461+
if (msb)
462+
dst[len - 1 - upper_bytes] |= ~(msb_bit - 1);
463+
else
464+
dst[len - 1 - upper_bytes] &= (msb_bit - 1);
465+
} else {
466+
/* XXX: untested */
467+
msb = dst[upper_bytes] & msb_bit;
468+
if (upper_bytes)
469+
memset(dst, msb ? 0xff : 0x00, upper_bytes);
470+
if (msb)
471+
dst[upper_bytes] |= ~(msb_bit - 1);
472+
}
477473
}
478474

479475
static void mask_upper_bits(uint8_t *dst, size_t bits, size_t len)
@@ -497,11 +493,7 @@ void iio_channel_convert(const struct iio_channel *chn,
497493
unsigned int len = chn->format.length / 8;
498494
ptrdiff_t end = len * chn->format.repeat;
499495
uintptr_t end_ptr = src_ptr + end;
500-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
501-
bool swap = chn->format.is_be;
502-
#else
503-
bool swap = !chn->format.is_be;
504-
#endif
496+
bool swap = is_little_endian() ^ !chn->format.is_be;
505497

506498
for (src_ptr = (uintptr_t) src; src_ptr < end_ptr;
507499
src_ptr += len, dst_ptr += len) {
@@ -533,11 +525,7 @@ void iio_channel_convert_inverse(const struct iio_channel *chn,
533525
unsigned int len = chn->format.length / 8;
534526
ptrdiff_t end = len * chn->format.repeat;
535527
uintptr_t end_ptr = dst_ptr + end;
536-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
537-
bool swap = chn->format.is_be;
538-
#else
539-
bool swap = !chn->format.is_be;
540-
#endif
528+
bool swap = is_little_endian() ^ !chn->format.is_be;
541529
uint8_t buf[1024];
542530

543531
/* Somehow I doubt we will have samples of 8192 bits each. */

iio-private.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,27 @@
7575
#define MAX_ATTR_NAME NAME_MAX /* encoded in the sysfs filename */
7676
#define MAX_ATTR_VALUE (8 * PAGESIZE) /* 8x Linux page size, could be anything */
7777

78+
#ifdef _MSC_BUILD
79+
/* Windows only runs on little-endian systems */
80+
#define is_little_endian() true
81+
#else
82+
#define is_little_endian() (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
83+
#endif
84+
7885
/* ntohl/htonl are a nightmare to use in cross-platform applications,
7986
* since they are defined in different headers on different platforms.
8087
* iio_be32toh/iio_htobe32 are just clones of ntohl/htonl. */
8188
static inline uint32_t iio_be32toh(uint32_t word)
8289
{
83-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
90+
if (!is_little_endian())
91+
return word;
92+
8493
#ifdef __GNUC__
8594
return __builtin_bswap32(word);
8695
#else
8796
return ((word & 0xff) << 24) | ((word & 0xff00) << 8) |
8897
((word >> 8) & 0xff00) | ((word >> 24) & 0xff);
8998
#endif
90-
#else
91-
return word;
92-
#endif
9399
}
94100

95101
static inline uint32_t iio_htobe32(uint32_t word)

0 commit comments

Comments
 (0)