misc.h: move out checked_int64_(add|subtract)

These inline functions are used only in exactly one place,
so no need to keep them in a public header.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1808>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-02-17 18:54:04 +01:00 committed by Marge Bot
parent b664b44869
commit de0aed0543
2 changed files with 29 additions and 29 deletions

View File

@ -312,6 +312,35 @@ SyncCheckTriggerFence(SyncTrigger * pTrigger, int64_t unused)
return (pFence == NULL || pFence->funcs.CheckTriggered(pFence)); return (pFence == NULL || pFence->funcs.CheckTriggered(pFence));
} }
static inline Bool
checked_int64_add(int64_t *out, int64_t a, int64_t b)
{
/* Do the potentially overflowing math as uint64_t, as signed
* integers in C are undefined on overflow (and the compiler may
* optimize out our overflow check below, otherwise)
*/
int64_t result = (uint64_t)a + (uint64_t)b;
/* signed addition overflows if operands have the same sign, and
* the sign of the result doesn't match the sign of the inputs.
*/
Bool overflow = (a < 0) == (b < 0) && (a < 0) != (result < 0);
*out = result;
return overflow;
}
static inline Bool
checked_int64_subtract(int64_t *out, int64_t a, int64_t b)
{
int64_t result = (uint64_t)a - (uint64_t)b;
Bool overflow = (a < 0) != (b < 0) && (a < 0) != (result < 0);
*out = result;
return overflow;
}
static int static int
SyncInitTrigger(ClientPtr client, SyncTrigger * pTrigger, XID syncObject, SyncInitTrigger(ClientPtr client, SyncTrigger * pTrigger, XID syncObject,
RESTYPE resType, Mask changes) RESTYPE resType, Mask changes)

View File

@ -285,35 +285,6 @@ bswap_32(uint32_t x)
((x & 0x000000FF) << 24)); ((x & 0x000000FF) << 24));
} }
static inline Bool
checked_int64_add(int64_t *out, int64_t a, int64_t b)
{
/* Do the potentially overflowing math as uint64_t, as signed
* integers in C are undefined on overflow (and the compiler may
* optimize out our overflow check below, otherwise)
*/
int64_t result = (uint64_t)a + (uint64_t)b;
/* signed addition overflows if operands have the same sign, and
* the sign of the result doesn't match the sign of the inputs.
*/
Bool overflow = (a < 0) == (b < 0) && (a < 0) != (result < 0);
*out = result;
return overflow;
}
static inline Bool
checked_int64_subtract(int64_t *out, int64_t a, int64_t b)
{
int64_t result = (uint64_t)a - (uint64_t)b;
Bool overflow = (a < 0) != (b < 0) && (a < 0) != (result < 0);
*out = result;
return overflow;
}
#define swapl(x) do { \ #define swapl(x) do { \
if (sizeof(*(x)) != 4) \ if (sizeof(*(x)) != 4) \
wrong_size(); \ wrong_size(); \