constant: Support suffixes for integers that otherwise would be narrowed.

This commit is contained in:
Emilio Cobos Álvarez
2022-06-07 16:03:55 +02:00
parent a79a10d9a6
commit 597b033ccf
14 changed files with 129 additions and 21 deletions
+9 -1
View File
@@ -43,4 +43,12 @@ typedef struct DebugFlags {
*/
#define DebugFlags_BIGGEST_ALLOWED (DebugFlags){ .bits = (uint32_t)(1 << 31) }
void root(struct AlignFlags flags, struct DebugFlags bigger_flags);
typedef struct LargeFlags {
uint64_t bits;
} LargeFlags;
/**
* Flag with a very large shift that usually would be narrowed.
*/
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags);
+9 -1
View File
@@ -43,11 +43,19 @@ typedef struct DebugFlags {
*/
#define DebugFlags_BIGGEST_ALLOWED (DebugFlags){ .bits = (uint32_t)(1 << 31) }
typedef struct LargeFlags {
uint64_t bits;
} LargeFlags;
/**
* Flag with a very large shift that usually would be narrowed.
*/
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(struct AlignFlags flags, struct DebugFlags bigger_flags);
void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags);
#ifdef __cplusplus
} // extern "C"
+9 -1
View File
@@ -43,4 +43,12 @@ typedef struct {
*/
#define DebugFlags_BIGGEST_ALLOWED (DebugFlags){ .bits = (uint32_t)(1 << 31) }
void root(AlignFlags flags, DebugFlags bigger_flags);
typedef struct {
uint64_t bits;
} LargeFlags;
/**
* Flag with a very large shift that usually would be narrowed.
*/
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);
+9 -1
View File
@@ -43,11 +43,19 @@ typedef struct {
*/
#define DebugFlags_BIGGEST_ALLOWED (DebugFlags){ .bits = (uint32_t)(1 << 31) }
typedef struct {
uint64_t bits;
} LargeFlags;
/**
* Flag with a very large shift that usually would be narrowed.
*/
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(AlignFlags flags, DebugFlags bigger_flags);
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);
#ifdef __cplusplus
} // extern "C"
+35 -1
View File
@@ -86,8 +86,42 @@ struct DebugFlags {
/// Flag with the topmost bit set of the u32
constexpr static const DebugFlags DebugFlags_BIGGEST_ALLOWED = DebugFlags{ /* .bits = */ (uint32_t)(1 << 31) };
struct LargeFlags {
uint64_t bits;
explicit operator bool() const {
return !!bits;
}
LargeFlags operator~() const {
return {static_cast<decltype(bits)>(~bits)};
}
LargeFlags operator|(const LargeFlags& other) const {
return {static_cast<decltype(bits)>(this->bits | other.bits)};
}
LargeFlags& operator|=(const LargeFlags& other) {
*this = (*this | other);
return *this;
}
LargeFlags operator&(const LargeFlags& other) const {
return {static_cast<decltype(bits)>(this->bits & other.bits)};
}
LargeFlags& operator&=(const LargeFlags& other) {
*this = (*this & other);
return *this;
}
LargeFlags operator^(const LargeFlags& other) const {
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
}
LargeFlags& operator^=(const LargeFlags& other) {
*this = (*this ^ other);
return *this;
}
};
/// Flag with a very large shift that usually would be narrowed.
constexpr static const LargeFlags LargeFlags_LARGE_SHIFT = LargeFlags{ /* .bits = */ (uint64_t)(1ull << 44) };
extern "C" {
void root(AlignFlags flags, DebugFlags bigger_flags);
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);
} // extern "C"
+6 -1
View File
@@ -30,4 +30,9 @@ cdef extern from *:
# Flag with the topmost bit set of the u32
const DebugFlags DebugFlags_BIGGEST_ALLOWED # = <DebugFlags>{ <uint32_t>(1 << 31) }
void root(AlignFlags flags, DebugFlags bigger_flags);
ctypedef struct LargeFlags:
uint64_t bits;
# Flag with a very large shift that usually would be narrowed.
const LargeFlags LargeFlags_LARGE_SHIFT # = <LargeFlags>{ <uint64_t>(1ull << 44) }
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);
+9 -1
View File
@@ -43,4 +43,12 @@ struct DebugFlags {
*/
#define DebugFlags_BIGGEST_ALLOWED (DebugFlags){ .bits = (uint32_t)(1 << 31) }
void root(struct AlignFlags flags, struct DebugFlags bigger_flags);
struct LargeFlags {
uint64_t bits;
};
/**
* Flag with a very large shift that usually would be narrowed.
*/
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags);
+9 -1
View File
@@ -43,11 +43,19 @@ struct DebugFlags {
*/
#define DebugFlags_BIGGEST_ALLOWED (DebugFlags){ .bits = (uint32_t)(1 << 31) }
struct LargeFlags {
uint64_t bits;
};
/**
* Flag with a very large shift that usually would be narrowed.
*/
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(struct AlignFlags flags, struct DebugFlags bigger_flags);
void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags);
#ifdef __cplusplus
} // extern "C"
+6 -1
View File
@@ -30,4 +30,9 @@ cdef extern from *:
# Flag with the topmost bit set of the u32
const DebugFlags DebugFlags_BIGGEST_ALLOWED # = <DebugFlags>{ <uint32_t>(1 << 31) }
void root(AlignFlags flags, DebugFlags bigger_flags);
cdef struct LargeFlags:
uint64_t bits;
# Flag with a very large shift that usually would be narrowed.
const LargeFlags LargeFlags_LARGE_SHIFT # = <LargeFlags>{ <uint64_t>(1ull << 44) }
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);
+2 -2
View File
@@ -3,10 +3,10 @@
#include <stdint.h>
#include <stdlib.h>
#define UNSIGNED_NEEDS_ULL_SUFFIX 9223372036854775808ULL
#define UNSIGNED_NEEDS_ULL_SUFFIX 9223372036854775808ull
#define UNSIGNED_DOESNT_NEED_ULL_SUFFIX 8070450532247928832
#define SIGNED_NEEDS_ULL_SUFFIX -9223372036854775808ULL
#define SIGNED_NEEDS_ULL_SUFFIX -9223372036854775808ull
#define SIGNED_DOESNT_NEED_ULL_SUFFIX -9223372036854775807
+2 -2
View File
@@ -4,10 +4,10 @@
#include <ostream>
#include <new>
constexpr static const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX = 9223372036854775808ULL;
constexpr static const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX = 9223372036854775808ull;
constexpr static const uint64_t UNSIGNED_DOESNT_NEED_ULL_SUFFIX = 8070450532247928832;
constexpr static const int64_t SIGNED_NEEDS_ULL_SUFFIX = -9223372036854775808ULL;
constexpr static const int64_t SIGNED_NEEDS_ULL_SUFFIX = -9223372036854775808ull;
constexpr static const int64_t SIGNED_DOESNT_NEED_ULL_SUFFIX = -9223372036854775807;
+2 -2
View File
@@ -6,10 +6,10 @@ cdef extern from *:
cdef extern from *:
const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX # = 9223372036854775808ULL
const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX # = 9223372036854775808ull
const uint64_t UNSIGNED_DOESNT_NEED_ULL_SUFFIX # = 8070450532247928832
const int64_t SIGNED_NEEDS_ULL_SUFFIX # = -9223372036854775808ULL
const int64_t SIGNED_NEEDS_ULL_SUFFIX # = -9223372036854775808ull
const int64_t SIGNED_DOESNT_NEED_ULL_SUFFIX # = -9223372036854775807
+10 -1
View File
@@ -29,5 +29,14 @@ bitflags! {
}
}
bitflags! {
#[repr(C)]
pub struct LargeFlags: u64 {
/// Flag with a very large shift that usually would be narrowed.
const LARGE_SHIFT = 1u64 << 44;
}
}
#[no_mangle]
pub extern "C" fn root(flags: AlignFlags, bigger_flags: DebugFlags) {}
pub extern "C" fn root(flags: AlignFlags, bigger_flags: DebugFlags, largest_flags: LargeFlags) {}