Skip to content
This repository was archived by the owner on Jul 23, 2020. It is now read-only.

Commit 1ed26fd

Browse files
committed
iio: And turn channel value processing into a macro
So that the code is shared between all 4 value sizes.
1 parent b468ed6 commit 1ed26fd

File tree

1 file changed

+30
-66
lines changed

1 file changed

+30
-66
lines changed

Diff for: src/iio-buffer-utils.c

+30-66
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,25 @@ size_from_channelarray (iio_channel_info **channels,
486486
return bytes;
487487
}
488488

489+
#define GUINT8_FROM_BE(x) (x)
490+
#define GUINT8_FROM_LE(x) (x)
491+
#define PROCESS_CHANNEL_BITS(bits) \
492+
{ \
493+
guint##bits input; \
494+
\
495+
input = *(guint##bits *)(data + info->location); \
496+
input = info->be ? GUINT##bits##_FROM_BE (input) : GUINT##bits##_FROM_LE (input);\
497+
input >>= info->shift; \
498+
input &= info->mask; \
499+
if (info->is_signed) { \
500+
gint##bits val = (gint##bits)(input << (bits - info->bits_used)) >> (bits - info->bits_used); \
501+
val += info->offset; \
502+
*ch_val = val; \
503+
} else { \
504+
*ch_val = input + info->offset; \
505+
} \
506+
}
507+
489508
/**
490509
* process_scan_1() - get an integer value for a particular channel
491510
* @data: pointer to the start of the scan
@@ -519,76 +538,21 @@ process_scan_1 (char *data,
519538
info->shift, info->bits_used);
520539

521540
switch (info->bytes) {
522-
case 1: {
523-
guint8 input;
524-
525-
input = *(guint8 *)(data + info->location);
526-
input >>= info->shift;
527-
input &= info->mask;
528-
if (info->is_signed) {
529-
gint8 val = (gint8)(input << (8 - info->bits_used)) >>
530-
(8 - info->bits_used);
531-
val += info->offset;
532-
*ch_val = val;
533-
} else {
534-
*ch_val = input + info->offset;
535-
}
541+
case 1:
542+
#pragma GCC diagnostic push
543+
#pragma GCC diagnostic ignored "-Wduplicated-branches"
544+
PROCESS_CHANNEL_BITS(8);
545+
#pragma GCC diagnostic pop
536546
break;
537-
}
538-
case 2: {
539-
guint16 input;
540-
541-
input = *(guint16 *)(data + info->location);
542-
input = info->be ? GUINT16_FROM_BE (input) : GUINT16_FROM_LE (input);
543-
input >>= info->shift;
544-
input &= info->mask;
545-
546-
if (info->is_signed) {
547-
gint16 val = (gint16)(input << (16 - info->bits_used)) >>
548-
(16 - info->bits_used);
549-
val += info->offset;
550-
*ch_val = val;
551-
} else {
552-
*ch_val = input + info->offset;
553-
}
547+
case 2:
548+
PROCESS_CHANNEL_BITS(16);
554549
break;
555-
}
556-
case 4: {
557-
guint32 input;
558-
559-
input = *(guint32 *)(data + info->location);
560-
input = info->be ? GUINT32_FROM_BE (input) : GUINT32_FROM_LE (input);
561-
input >>= info->shift;
562-
input &= info->mask;
563-
564-
if (info->is_signed) {
565-
gint32 val = (gint32)(input << (32 - info->bits_used)) >>
566-
(32 - info->bits_used);
567-
val += info->offset;
568-
*ch_val = val;
569-
} else {
570-
*ch_val = input + info->offset;
571-
}
550+
case 4:
551+
PROCESS_CHANNEL_BITS(32);
572552
break;
573-
}
574-
case 8: {
575-
guint64 input;
576-
577-
input = *(guint64 *)(data + info->location);
578-
input = info->be ? GUINT64_FROM_BE (input) : GUINT64_FROM_LE (input);
579-
input >>= info->shift;
580-
input &= info->mask;
581-
582-
if (info->is_signed) {
583-
gint64 val = (gint64)(input << (64 - info->bits_used)) >>
584-
(64 - info->bits_used);
585-
val += info->offset;
586-
*ch_val = val;
587-
} else {
588-
*ch_val = input + info->offset;
589-
}
553+
case 8:
554+
PROCESS_CHANNEL_BITS(64);
590555
break;
591-
}
592556
default:
593557
g_error ("Process %d bytes channels not supported", info->bytes);
594558
break;

0 commit comments

Comments
 (0)