-/* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
-#define _bt_piecewise_rshift(_v, _shift) \
-({ \
- typeof(_v) ___v = (_v); \
- typeof(_shift) ___shift = (_shift); \
- unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
- unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
- \
- for (; sb; sb--) \
- ___v >>= sizeof(___v) * CHAR_BIT - 1; \
- ___v >>= final; \
-})
-
-#define _bt_piecewise_lshift(_v, _shift) \
-({ \
- typeof(_v) ___v = (_v); \
- typeof(_shift) ___shift = (_shift); \
- unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
- unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
- \
- for (; sb; sb--) \
- ___v <<= sizeof(___v) * CHAR_BIT - 1; \
- ___v <<= final; \
-})
+/*
+ * This header strictly follows the C99 standard, except for use of the
+ * compiler-specific __typeof__.
+ */
+
+/*
+ * This bitfield header requires the compiler representation of signed
+ * integers to be two's complement.
+ */
+#if (-1 != ~0)
+#error "bitfield.h requires the compiler representation of signed integers to be two's complement."
+#endif
+
+#define _bt_is_signed_type(type) ((type) -1 < (type) 0)
+
+/*
+ * Produce a build-time error if the condition `cond` is non-zero.
+ * Evaluates as a size_t expression.
+ */
+#define _BT_BUILD_ASSERT(cond) \
+ sizeof(struct { int f:(2 * !!(cond) - 1); })
+
+/*
+ * Cast value `v` to an unsigned integer of the same size as `v`.
+ */
+#define _bt_cast_value_to_unsigned(v) \
+ (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
+ sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
+ sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
+ sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
+ _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
+
+/*
+ * Cast value `v` to an unsigned integer type of the size of type `type`
+ * *without* sign-extension.
+ *
+ * The unsigned cast ensures that we're not shifting a negative value,
+ * which is undefined in C. However, this limits the maximum type size
+ * of `type` to 64-bit. Generate a compile-time error if the size of
+ * `type` is larger than 64-bit.
+ */
+#define _bt_cast_value_to_unsigned_type(type, v) \
+ (sizeof(type) == sizeof(uint8_t) ? \
+ (uint8_t) _bt_cast_value_to_unsigned(v) : \
+ sizeof(type) == sizeof(uint16_t) ? \
+ (uint16_t) _bt_cast_value_to_unsigned(v) : \
+ sizeof(type) == sizeof(uint32_t) ? \
+ (uint32_t) _bt_cast_value_to_unsigned(v) : \
+ sizeof(type) == sizeof(uint64_t) ? \
+ (uint64_t) _bt_cast_value_to_unsigned(v) : \
+ _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
+
+/*
+ * _bt_fill_mask evaluates to a "type" integer with all bits set.
+ */
+#define _bt_fill_mask(type) ((type) ~(type) 0)
+
+/*
+ * Left shift a value `v` of `shift` bits.
+ *
+ * The type of `v` can be signed or unsigned integer.
+ * The value of `shift` must be less than the size of `v` (in bits),
+ * otherwise the behavior is undefined.
+ * Evaluates to the result of the shift operation.
+ *
+ * According to the C99 standard, left shift of a left hand-side signed
+ * type is undefined if it has a negative value or if the result cannot
+ * be represented in the result type. This bitfield header discards the
+ * bits that are left-shifted beyond the result type representation,
+ * which is the behavior of an unsigned type left shift operation.
+ * Therefore, always perform left shift on an unsigned type.
+ *
+ * This macro should not be used if `shift` can be greater or equal than
+ * the bitwidth of `v`. See `_bt_safe_lshift`.
+ */
+#define _bt_lshift(v, shift) \
+ ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))