iio: And turn channel value processing into a macro

So that the code is shared between all 4 value sizes.
This commit is contained in:
Bastien Nocera
2019-05-13 17:20:06 +02:00
committed by hadess
parent b468ed6384
commit 1ed26fd8e4

View File

@@ -486,6 +486,25 @@ size_from_channelarray (iio_channel_info **channels,
return bytes; return bytes;
} }
#define GUINT8_FROM_BE(x) (x)
#define GUINT8_FROM_LE(x) (x)
#define PROCESS_CHANNEL_BITS(bits) \
{ \
guint##bits input; \
\
input = *(guint##bits *)(data + info->location); \
input = info->be ? GUINT##bits##_FROM_BE (input) : GUINT##bits##_FROM_LE (input);\
input >>= info->shift; \
input &= info->mask; \
if (info->is_signed) { \
gint##bits val = (gint##bits)(input << (bits - info->bits_used)) >> (bits - info->bits_used); \
val += info->offset; \
*ch_val = val; \
} else { \
*ch_val = input + info->offset; \
} \
}
/** /**
* process_scan_1() - get an integer value for a particular channel * process_scan_1() - get an integer value for a particular channel
* @data: pointer to the start of the scan * @data: pointer to the start of the scan
@@ -519,76 +538,21 @@ process_scan_1 (char *data,
info->shift, info->bits_used); info->shift, info->bits_used);
switch (info->bytes) { switch (info->bytes) {
case 1: { case 1:
guint8 input; #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wduplicated-branches"
input = *(guint8 *)(data + info->location); PROCESS_CHANNEL_BITS(8);
input >>= info->shift; #pragma GCC diagnostic pop
input &= info->mask;
if (info->is_signed) {
gint8 val = (gint8)(input << (8 - info->bits_used)) >>
(8 - info->bits_used);
val += info->offset;
*ch_val = val;
} else {
*ch_val = input + info->offset;
}
break; break;
} case 2:
case 2: { PROCESS_CHANNEL_BITS(16);
guint16 input;
input = *(guint16 *)(data + info->location);
input = info->be ? GUINT16_FROM_BE (input) : GUINT16_FROM_LE (input);
input >>= info->shift;
input &= info->mask;
if (info->is_signed) {
gint16 val = (gint16)(input << (16 - info->bits_used)) >>
(16 - info->bits_used);
val += info->offset;
*ch_val = val;
} else {
*ch_val = input + info->offset;
}
break; break;
} case 4:
case 4: { PROCESS_CHANNEL_BITS(32);
guint32 input;
input = *(guint32 *)(data + info->location);
input = info->be ? GUINT32_FROM_BE (input) : GUINT32_FROM_LE (input);
input >>= info->shift;
input &= info->mask;
if (info->is_signed) {
gint32 val = (gint32)(input << (32 - info->bits_used)) >>
(32 - info->bits_used);
val += info->offset;
*ch_val = val;
} else {
*ch_val = input + info->offset;
}
break; break;
} case 8:
case 8: { PROCESS_CHANNEL_BITS(64);
guint64 input;
input = *(guint64 *)(data + info->location);
input = info->be ? GUINT64_FROM_BE (input) : GUINT64_FROM_LE (input);
input >>= info->shift;
input &= info->mask;
if (info->is_signed) {
gint64 val = (gint64)(input << (64 - info->bits_used)) >>
(64 - info->bits_used);
val += info->offset;
*ch_val = val;
} else {
*ch_val = input + info->offset;
}
break; break;
}
default: default:
g_error ("Process %d bytes channels not supported", info->bytes); g_error ("Process %d bytes channels not supported", info->bytes);
break; break;