iio: adc: ti-ads1015: Add static assert to test if shifted realbits fit into storagebits
authorMarek Vasut <marex@denx.de>
Mon, 28 Mar 2022 19:47:22 +0000 (21:47 +0200)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 4 Apr 2022 08:11:26 +0000 (09:11 +0100)
Add compile-time static_assert wrapper to verify that shifted realbits
fit into storagebits. The macro is implemented in a more generic way so
it can be used to verify other values if required.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Cc: Daniel Baluta <daniel.baluta@nxp.com>
Link: https://lore.kernel.org/r/20220328194725.149150-7-marex@denx.de
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/adc/ti-ads1015.c

index 51ab8bb3d9f1d259656b6010814acfe3385ea1a6..73d848804a12d03b10a8903f8e7f0cc7dc90bd38 100644 (file)
@@ -135,6 +135,28 @@ static const struct iio_event_spec ads1015_events[] = {
        },
 };
 
+/*
+ * Compile-time check whether _fitbits can accommodate up to _testbits
+ * bits. Returns _fitbits on success, fails to compile otherwise.
+ *
+ * The test works such that it multiplies constant _fitbits by constant
+ * double-negation of size of a non-empty structure, i.e. it multiplies
+ * constant _fitbits by constant 1 in each successful compilation case.
+ * The non-empty structure may contain C11 _Static_assert(), make use of
+ * this and place the kernel variant of static assert in there, so that
+ * it performs the compile-time check for _testbits <= _fitbits. Note
+ * that it is not possible to directly use static_assert in compound
+ * statements, hence this convoluted construct.
+ */
+#define FIT_CHECK(_testbits, _fitbits)                                 \
+       (                                                               \
+               (_fitbits) *                                            \
+               !!sizeof(struct {                                       \
+                       static_assert((_testbits) <= (_fitbits));       \
+                       int pad;                                        \
+               })                                                      \
+       )
+
 #define ADS1015_V_CHAN(_chan, _addr, _realbits, _shift, _event_spec, _num_event_specs) { \
        .type = IIO_VOLTAGE,                                    \
        .indexed = 1,                                           \
@@ -147,7 +169,7 @@ static const struct iio_event_spec ads1015_events[] = {
        .scan_type = {                                          \
                .sign = 's',                                    \
                .realbits = (_realbits),                        \
-               .storagebits = 16,                              \
+               .storagebits = FIT_CHECK((_realbits) + (_shift), 16),   \
                .shift = (_shift),                              \
                .endianness = IIO_CPU,                          \
        },                                                      \
@@ -170,7 +192,7 @@ static const struct iio_event_spec ads1015_events[] = {
        .scan_type = {                                          \
                .sign = 's',                                    \
                .realbits = (_realbits),                        \
-               .storagebits = 16,                              \
+               .storagebits = FIT_CHECK((_realbits) + (_shift), 16),   \
                .shift = (_shift),                              \
                .endianness = IIO_CPU,                          \
        },                                                      \