blob: 016d5c8c6da166e4146d056a283e6418adb7cb9a [file] [log] [blame] [raw]
/*
* C utilities
*
* Copyright (c) 2016 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef CUTILS_H
#define CUTILS_H
#include <inttypes.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define force_inline inline __attribute__((always_inline))
#define no_inline __attribute__((noinline))
#define __maybe_unused __attribute__((unused))
#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)
#define stringify(s) tostring(s)
#define tostring(s) #s
#ifndef offsetof
#define offsetof(type, field) ((size_t) &((type *)0)->field)
#endif
#define countof(x) (sizeof(x) / sizeof(x[0]))
#define DLL_PUBLIC __attribute__ ((visibility ("default")))
#ifndef _BOOL_defined
#define _BOOL_defined
#undef FALSE
#undef TRUE
typedef int BOOL;
enum {
FALSE = 0,
TRUE = 1,
};
#endif
/* this test works at least with gcc */
#if defined(__SIZEOF_INT128__)
#define HAVE_INT128
#elif CONFIG_RISCV_MAX_XLEN == 128
//#warning "int128 type not available for CONFIG_RISCV_MAX_XLEN=128"
#endif
#ifdef HAVE_INT128
typedef __int128 int128_t;
typedef unsigned __int128 uint128_t;
#else
typedef struct {
int64_t l, h;
} int128_t;
typedef struct {
uint64_t l, h;
} uint128_t;
#endif
static inline int max_int(int a, int b)
{
if (a > b)
return a;
else
return b;
}
static inline int min_int(int a, int b)
{
if (a < b)
return a;
else
return b;
}
void *mallocz(size_t size);
#ifdef __GNUC__
#define bswap16 __builtin_bswap16
#define bswap32 __builtin_bswap32
#define bswap_16 __builtin_bswap16
#define bswap_32 __builtin_bswap32
#elif defined __GLIBC__
#include <byteswap.h>
#define bswap16 bswap_16
#define bswap32 bswap_32
#elif defined __SVR4 && defined __sun
#include <sys/byteorder.h>
#define bswap16 BSWAP_16
#define bswap32 BSWAP_32
#define bswap_16 BSWAP_16
#define bswap_32 BSWAP_32
#elif defined __FreeBSD__ || defined __DragonFly__
#include <sys/endian.h>
#define bswap_16 bswap16
#define bswap_32 bswap32
#else
static inline uint16_t bswap16(uint16_t v) {
return ((v & 0xff00) >> 8) | ((v & 0x00ff) << 8);
}
static inline uint32_t bswap32(uint32_t v)
{
return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) |
((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24);
}
#define bswap_16 bswap16
#define bswap_32 bswap32
#endif
#ifdef __BYTE_ORDER__
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define HOST_WORDS_BIGENDIAN
#endif
#elif defined __ARMEB__ || defined __MIPSEB || defined _MIPSEB || defined MIPSEB || defined __MICROBLAZEEB__
#define HOST_WORDS_BIGENDIAN
#endif
#define get_le8(p) (*(p))
static inline uint16_t get_le16(const uint8_t *ptr)
{
return ptr[0] | (ptr[1] << 8);
}
static inline uint32_t get_le32(const uint8_t *ptr)
{
return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
}
static inline uint64_t get_le64(const uint8_t *ptr)
{
return get_le32(ptr) | ((uint64_t)get_le32(ptr + 4) << 32);
}
#if CONFIG_RISCV_MAX_XLEN == 128
static inline uint128_t get_le128(const uint8_t *p) {
return get_le64(p) | ((uint128_t)get_le64(p + 8) << 64);
}
#endif
#define put_le8(p,v) (*(p) = (v))
static inline void put_le16(uint8_t *ptr, uint16_t v)
{
ptr[0] = v;
ptr[1] = v >> 8;
}
static inline void put_le32(uint8_t *ptr, uint32_t v)
{
ptr[0] = v;
ptr[1] = v >> 8;
ptr[2] = v >> 16;
ptr[3] = v >> 24;
}
static inline void put_le64(uint8_t *ptr, uint64_t v)
{
put_le32(ptr, v);
put_le32(ptr + 4, v >> 32);
}
#if CONFIG_RISCV_MAX_XLEN == 128
static inline void put_le128(uint8_t *p, uint128_t v) {
put_le64(p, v);
put_le64(p + 8, v >> 64);
}
#endif
static inline uint32_t get_be32(const uint8_t *d)
{
return (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
}
static inline void put_be32(uint8_t *d, uint32_t v)
{
d[0] = v >> 24;
d[1] = v >> 16;
d[2] = v >> 8;
d[3] = v >> 0;
}
static inline void put_be64(uint8_t *d, uint64_t v)
{
put_be32(d, v >> 32);
put_be32(d + 4, v);
}
#ifdef HOST_WORDS_BIGENDIAN
#define cpu_to_be16(v) (v)
#define cpu_to_be32(v) (v)
#define cpu_to_le16(v) bswap16(v)
#define cpu_to_le32(v) bswap32(v)
#define le16_to_cpu(v) bswap16(v)
#define le32_to_cpu(v) bswap32(v)
#else
#define cpu_to_be16(v) bswap16(v)
#define cpu_to_be32(v) bswap32(v)
#define cpu_to_le16(v) (v)
#define cpu_to_le32(v) (v)
#define le16_to_cpu(v) (v)
#define le32_to_cpu(v) (v)
#endif
/* XXX: optimize */
static inline int ctz32(uint32_t a)
{
int i;
if (a == 0)
return 32;
for(i = 0; i < 32; i++) {
if ((a >> i) & 1)
return i;
}
return 32;
}
void *mallocz(size_t size);
void pstrcpy(char *buf, int buf_size, const char *str);
char *pstrcat(char *buf, int buf_size, const char *s);
int strstart(const char *str, const char *val, const char **ptr);
typedef struct {
uint8_t *buf;
size_t size;
size_t allocated_size;
} DynBuf;
void dbuf_init(DynBuf *s);
void dbuf_write(DynBuf *s, size_t offset, const uint8_t *data, size_t len);
void dbuf_putc(DynBuf *s, uint8_t c);
void dbuf_putstr(DynBuf *s, const char *str);
void dbuf_free(DynBuf *s);
#endif /* CUTILS_H */