Skip to content

Commit

Permalink
Use bit-precise integer types when available
Browse files Browse the repository at this point in the history
  • Loading branch information
runer112 committed Nov 11, 2024
1 parent eab2d36 commit 059b8aa
Showing 1 changed file with 53 additions and 38 deletions.
91 changes: 53 additions & 38 deletions examples/standalone_examples/math_test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@

# define x_printf printf

# ifndef __cplusplus
# if __STDC_VERSION__ >= 202311L

typedef int32_t int24_t;
typedef uint32_t uint24_t;
typedef int64_t int48_t;
typedef uint64_t uint48_t;
typedef _BitInt(24) int24_t;
typedef unsigned _BitInt(24) uint24_t;
typedef _BitInt(48) int48_t;
typedef unsigned _BitInt(48) uint48_t;

# else
# elif defined(__clang__)

typedef _ExtInt(24) int24_t;
typedef unsigned _ExtInt(24) uint24_t;
typedef _ExtInt(48) int48_t;
typedef unsigned _ExtInt(48) uint48_t;

# elif defined(__cplusplus)

template <unsigned bits, typename U>
struct IntN final
Expand All @@ -56,6 +63,10 @@ using int24_t = IntN<24, int_fast32_t>;
using uint48_t = IntN<48, uint_fast64_t>;
using int48_t = IntN<48, int_fast64_t>;

# else

#error "Use a more modern language standard/compiler"

# endif

static int24_t __builtin_bitreverse24(int24_t x)
Expand Down Expand Up @@ -85,6 +96,10 @@ static int __builtin_popcounti48(uint48_t x)
{
return __builtin_popcountll(x & ((1LL << 48) - 1));
}
static int24_t iabs(int24_t x)
{
return x < 0 ? (int24_t)-x : x;
}
static int48_t i48abs(int48_t x)
{
return x < 0 ? (int48_t)-x : x;
Expand Down Expand Up @@ -127,18 +142,18 @@ static void finishOutput()
return (type)(op(x)); \
}

#define DEFINE_BINOP_FUNC_FUNC(type, name, func, post) \
static type name##_(type x, type y) \
{ \
return (type)(func(x, y)post); \
}

#define DEFINE_BINOP_INFIX_FUNC(type, name, op) \
static type name##_(type x, type y) \
{ \
return (type)(x op y); \
}

#define DEFINE_BINOP_DIV_LIKE_FUNC(type, name, func, post) \
static type name##_(type x, type y) \
{ \
return (type)(func(x + 0, y + 0)post); \
}

#define DEFINE_UNOP_PREFIX_FUNC_B(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC(u##int8_t, b##name, op)
#define DEFINE_UNOP_PREFIX_FUNC_S(u, name, op) \
Expand All @@ -152,19 +167,6 @@ static void finishOutput()
#define DEFINE_UNOP_PREFIX_FUNC_LL(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC(u##int64_t, ll##name, op)

#define DEFINE_BINOP_FUNC_FUNC_B(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int8_t, b##name, b##func, post)
#define DEFINE_BINOP_FUNC_FUNC_S(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int16_t, s##name, s##func, post)
#define DEFINE_BINOP_FUNC_FUNC_I(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int24_t, i##name, func, post)
#define DEFINE_BINOP_FUNC_FUNC_L(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int32_t, l##name, l##func, post)
#define DEFINE_BINOP_FUNC_FUNC_I48(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int48_t, i48##name, i48##func, post)
#define DEFINE_BINOP_FUNC_FUNC_LL(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int64_t, ll##name, ll##func, post)

#define DEFINE_BINOP_INFIX_FUNC_B(u, name, op) \
DEFINE_BINOP_INFIX_FUNC(u##int8_t, b##name, op)
#define DEFINE_BINOP_INFIX_FUNC_S(u, name, op) \
Expand All @@ -178,6 +180,19 @@ static void finishOutput()
#define DEFINE_BINOP_INFIX_FUNC_LL(u, name, op) \
DEFINE_BINOP_INFIX_FUNC(u##int64_t, ll##name, op)

#define DEFINE_BINOP_DIV_LIKE_FUNC_B(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC(u##int8_t, b##name, b##func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_S(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC(u##int16_t, s##name, s##func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_I(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC(u##int24_t, i##name, func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_L(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC(u##int32_t, l##name, l##func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_I48(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC(u##int48_t, i48##name, i48##func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_LL(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC(u##int64_t, ll##name, ll##func, post)

#define DEFINE_UNOP_PREFIX_FUNC_B_TO_S(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_S(u, name, op)
Expand Down Expand Up @@ -210,15 +225,15 @@ static void finishOutput()
DEFINE_BINOP_INFIX_FUNC_B_TO_I48(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_LL(u, name, op)

#define DEFINE_BINOP_FUNC_FUNC_I_TO_L(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_I(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_L(u, name, func, post)
#define DEFINE_BINOP_FUNC_FUNC_I_TO_I48(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_I_TO_L(u, name, func, post) \
// DEFINE_BINOP_FUNC_FUNC_I48(u, name, func, post)
#define DEFINE_BINOP_FUNC_FUNC_I_TO_LL(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_I_TO_I48(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_LL(u, name, func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_I_TO_L(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC_I(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC_L(u, name, func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_I_TO_I48(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC_I_TO_L(u, name, func, post) \
// DEFINE_BINOP_DIV_LIKE_FUNC_I48(u, name, func, post)
#define DEFINE_BINOP_DIV_LIKE_FUNC_I_TO_LL(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC_I_TO_I48(u, name, func, post) \
DEFINE_BINOP_DIV_LIKE_FUNC_LL(u, name, func, post)


#define DEFINE_UNOP_TYPE(u) \
Expand Down Expand Up @@ -383,7 +398,8 @@ static void testBinOp(const BinOp *op, int64_t x, int64_t y)
DEFINE_UNOP_PREFIX_B_TO_LL( , not, ~)
DEFINE_UNOP_PREFIX_B_TO_LL( , neg, -)

DEFINE_UNOP_PREFIX_FUNC_B_TO_I( , abs, abs)
DEFINE_UNOP_PREFIX_FUNC_B_TO_S( , abs, abs)
DEFINE_UNOP_PREFIX_FUNC_I( , abs, iabs)
DEFINE_UNOP_PREFIX_FUNC_L( , abs, labs)
DEFINE_UNOP_PREFIX_FUNC_I48( , abs, i48abs)
DEFINE_UNOP_PREFIX_FUNC_LL( , abs, llabs)
Expand Down Expand Up @@ -426,11 +442,10 @@ DEFINE_BINOP_INFIX_B_TO_LL(u, remu, %)
DEFINE_BINOP_INFIX_B_TO_LL( , divs, /)
DEFINE_BINOP_INFIX_B_TO_LL( , rems, %)


DEFINE_BINOP_FUNC_FUNC_I_TO_LL( , div_q, div, .quot)
DEFINE_BINOP_DIV_LIKE_FUNC_I_TO_LL( , div_q, div, .quot)
DEFINE_BINOP_STRUCT_I_TO_LL( , div_q)

DEFINE_BINOP_FUNC_FUNC_I_TO_LL( , div_r, div, .rem)
DEFINE_BINOP_DIV_LIKE_FUNC_I_TO_LL( , div_r, div, .rem)
DEFINE_BINOP_STRUCT_I_TO_LL( , div_r)


Expand Down

0 comments on commit 059b8aa

Please sign in to comment.