sfxge(4): cope with clang warning on negative shift

clang 4.0.1-6 on Ubuntu generates false positive warning that shift
is negative.  It is done regardless of the fact that the branch is
not taken because of previous check.

The warning is generate in EFX_INSERT_NATIVE32 used by
EFX_INSERT_FIELD_NATIVE32. All similar cases are fixed as well.

It is undesirable to suppress the warning completely.

Sponsored by:   Solarflare Communications, Inc.
Differential Revision:  https://reviews.freebsd.org/D18229
This commit is contained in:
Andrew Rybchenko 2018-11-29 06:44:20 +00:00
parent 2a67ba3018
commit ce6c2c8ffb

View File

@ -356,6 +356,16 @@ extern int fix_lint;
#define FIX_LINT(_x) (_x)
#endif
/*
* Saturation arithmetic subtract with minimum equal to zero.
*
* Use saturating arithmetic to ensure a non-negative result. This
* avoids undefined behaviour (and compiler warnings) when used as a
* shift count.
*/
#define EFX_SSUB(_val, _sub) \
((_val) > (_sub) ? ((_val) - (_sub)) : 0)
/*
* Extract bit field portion [low,high) from the native-endian element
* which contains bits [min,max).
@ -375,8 +385,8 @@ extern int fix_lint;
((FIX_LINT(_low > _max) || FIX_LINT(_high < _min)) ? \
0U : \
((_low > _min) ? \
((_element) >> (_low - _min)) : \
((_element) << (_min - _low))))
((_element) >> EFX_SSUB(_low, _min)) : \
((_element) << EFX_SSUB(_min, _low))))
/*
* Extract bit field portion [low,high) from the 64-bit little-endian
@ -565,29 +575,29 @@ extern int fix_lint;
(((_low > _max) || (_high < _min)) ? \
0U : \
((_low > _min) ? \
(((uint64_t)(_value)) << (_low - _min)) : \
(((uint64_t)(_value)) >> (_min - _low))))
(((uint64_t)(_value)) << EFX_SSUB(_low, _min)) :\
(((uint64_t)(_value)) >> EFX_SSUB(_min, _low))))
#define EFX_INSERT_NATIVE32(_min, _max, _low, _high, _value) \
(((_low > _max) || (_high < _min)) ? \
0U : \
((_low > _min) ? \
(((uint32_t)(_value)) << (_low - _min)) : \
(((uint32_t)(_value)) >> (_min - _low))))
(((uint32_t)(_value)) << EFX_SSUB(_low, _min)) :\
(((uint32_t)(_value)) >> EFX_SSUB(_min, _low))))
#define EFX_INSERT_NATIVE16(_min, _max, _low, _high, _value) \
(((_low > _max) || (_high < _min)) ? \
0U : \
(uint16_t)((_low > _min) ? \
((_value) << (_low - _min)) : \
((_value) >> (_min - _low))))
((_value) << EFX_SSUB(_low, _min)) : \
((_value) >> EFX_SSUB(_min, _low))))
#define EFX_INSERT_NATIVE8(_min, _max, _low, _high, _value) \
(((_low > _max) || (_high < _min)) ? \
0U : \
(uint8_t)((_low > _min) ? \
((_value) << (_low - _min)) : \
((_value) >> (_min - _low))))
((_value) << EFX_SSUB(_low, _min)) : \
((_value) >> EFX_SSUB(_min, _low))))
/*
* Construct bit field portion
@ -1316,22 +1326,22 @@ extern int fix_lint;
#define EFX_SHIFT64(_bit, _base) \
(((_bit) >= (_base) && (_bit) < (_base) + 64) ? \
((uint64_t)1 << ((_bit) - (_base))) : \
((uint64_t)1 << EFX_SSUB((_bit), (_base))) : \
0U)
#define EFX_SHIFT32(_bit, _base) \
(((_bit) >= (_base) && (_bit) < (_base) + 32) ? \
((uint32_t)1 << ((_bit) - (_base))) : \
((uint32_t)1 << EFX_SSUB((_bit),(_base))) : \
0U)
#define EFX_SHIFT16(_bit, _base) \
(((_bit) >= (_base) && (_bit) < (_base) + 16) ? \
(uint16_t)(1 << ((_bit) - (_base))) : \
(uint16_t)(1 << EFX_SSUB((_bit), (_base))) : \
0U)
#define EFX_SHIFT8(_bit, _base) \
(((_bit) >= (_base) && (_bit) < (_base) + 8) ? \
(uint8_t)(1 << ((_bit) - (_base))) : \
(uint8_t)(1 << EFX_SSUB((_bit), (_base))) : \
0U)
#define EFX_SET_OWORD_BIT64(_oword, _bit) \