build: Add generic compiler builtins support

Wrap compiler builtins into shared functions with proper generic
implementations. __builtin_clz() isn't wrapped for now because its use
by screenshooter is pretty specific. It will be properly wrapped in
the next commit which needs a round up to the next power of 2
function.

Signed-off-by: Loïc Molinari <loic.molinari@collabora.com>
This commit is contained in:
Loïc Molinari 2024-04-30 19:48:56 +02:00 committed by Derek Foreman
parent de669aeb60
commit 0729ffbdb8
6 changed files with 63 additions and 3 deletions

View File

@ -88,7 +88,7 @@ static inline const char *
dump_format(uint32_t format, char out[4])
{
#if BYTE_ORDER == BIG_ENDIAN
format = __builtin_bswap32(format);
format = bswap32(format);
#endif
memcpy(out, &format, 4);
return out;

View File

@ -201,7 +201,7 @@ data_offer_set_actions(struct wl_client *client,
if (preferred_action &&
(!(preferred_action & dnd_actions) ||
__builtin_popcount(preferred_action) > 1)) {
bitcount32(preferred_action) > 1)) {
wl_resource_post_error(offer->resource,
WL_DATA_OFFER_ERROR_INVALID_ACTION,
"invalid action %x", preferred_action);

View File

@ -242,7 +242,7 @@ static inline const char *
dump_format(uint32_t format, char out[4])
{
#if BYTE_ORDER == BIG_ENDIAN
format = __builtin_bswap32(format);
format = bswap32(format);
#endif
memcpy(out, &format, 4);
return out;

View File

@ -252,6 +252,9 @@ static uint32_t *
output_run(uint32_t *p, uint32_t delta, int run)
{
int i;
#if !defined(HAVE_BUILTIN_CLZ)
int tmp;
#endif
while (run > 0) {
if (run <= 0xe0) {
@ -259,7 +262,11 @@ output_run(uint32_t *p, uint32_t delta, int run)
break;
}
#if defined(HAVE_BUILTIN_CLZ)
i = 24 - __builtin_clz(run);
#else
for (i = 0, tmp = u >> 8; tmp; i++, tmp >>= 1);
#endif
*p++ = delta | ((i + 0xe0) << 24);
run -= 1 << (7 + i);
}

View File

@ -94,6 +94,17 @@ foreach hdr : optional_system_headers
endif
endforeach
optional_builtins = {
'builtin_clz': 'return __builtin_clz(1);',
'builtin_bswap32': 'return __builtin_bswap32(0);',
'builtin_popcount': 'return __builtin_popcount(0);',
}
foreach name, check : optional_builtins
if cc.links('int main(void) { @0@ }'.format(check), name: name)
config_h.set('HAVE_' + name.to_upper(), 1)
endif
endforeach
env_modmap = ''
config_h.set('_GNU_SOURCE', '1')

View File

@ -22,6 +22,8 @@
#ifndef WESTON_HELPERS_H
#define WESTON_HELPERS_H
#include "config.h"
#include <stdint.h>
#ifdef __cplusplus
@ -233,6 +235,46 @@ do { \
#define unreachable(str) assert(!str)
#endif
/**
* Returns number of bits set in 32-bit value x.
*
* @param x a 32-bit value.
* @return the number of bits set.
*/
static inline int
bitcount32(uint32_t x)
{
#if defined(HAVE_BUILTIN_POPCOUNT)
return __builtin_popcount(x);
#else
int n;
for (n = 0; x; n++)
x &= x - 1;
return n;
#endif
}
/**
* Returns 32-bit value x in reversed byte order.
*
* @param x a 32-bit value.
* @return the reversed 32-bit value.
*/
static inline uint32_t
bswap32(uint32_t x)
{
#if defined(HAVE_BUILTIN_BSWAP32)
return __builtin_bswap32(x);
#else
return (x >> 24) |
((x >> 8) & 0x0000ff00) |
((x << 8) & 0x00ff0000) |
(x << 24);
#endif
}
#ifdef __cplusplus
}
#endif