base: add optional_u63 type

This commit is contained in:
Nigel Tao committed 2023-11-19 12:02:24 +11:00
1 parent e517ca4bee
commit 82ea1985b4
32 files changed
+470 -295

No files matched your search

+3
View File
@@ -185,6 +185,9 @@ wuffs_private_impl__u64__sat_sub_indirect(uint64_t* x, uint64_t y) {
#define wuffs_base__utility__make_bitvec256(e00, e01, e02, e03) \
wuffs_base__make_bitvec256(e00, e01, e02, e03)
#define wuffs_base__utility__make_optional_u63(h, v) \
wuffs_base__make_optional_u63(h, v)
// ---------------- Slices and Tables
// wuffs_private_impl__slice_u8__prefix returns up to the first up_to bytes of
+64
View File
@@ -894,6 +894,70 @@ wuffs_base__make_bitvec256(uint64_t e00,
// --------
// wuffs_base__optional_u63 is like a std::optional<uint64_t>, but for C (not
// just C++) and the value can only hold 63 bits (not 64).
//
// Do not manipulate repr directly; it is a private implementation detail.
typedef struct wuffs_base__optional_u63__struct {
uint64_t repr;
#ifdef __cplusplus
inline bool has_value() const;
inline uint64_t value() const;
inline uint64_t value_or(uint64_t default_value) const;
#endif // __cplusplus
} wuffs_base__optional_u63;
// wuffs_base__make_optional_u63 ignores value when has_value is false.
//
// Preconditions:
// - value < (1 << 63).
static inline wuffs_base__optional_u63 //
wuffs_base__make_optional_u63(bool has_value, uint64_t value) {
wuffs_base__optional_u63 res;
res.repr = has_value ? ((value << 1u) | 1u) : 0u;
return res;
}
static inline bool //
wuffs_base__optional_u63__has_value(const wuffs_base__optional_u63* o) {
return o->repr;
}
// wuffs_base__optional_u63__value returns zero when o does not have a value.
static inline uint64_t //
wuffs_base__optional_u63__value(const wuffs_base__optional_u63* o) {
return o->repr >> 1u;
}
static inline uint64_t //
wuffs_base__optional_u63__value_or(const wuffs_base__optional_u63* o,
uint64_t default_value) {
return o->repr ? (o->repr >> 1u) : default_value;
}
#ifdef __cplusplus
inline bool //
wuffs_base__optional_u63::has_value() const {
return wuffs_base__optional_u63__has_value(this);
}
inline uint64_t //
wuffs_base__optional_u63::value() const {
return wuffs_base__optional_u63__value(this);
}
inline uint64_t //
wuffs_base__optional_u63::value_or(uint64_t default_value) const {
return wuffs_base__optional_u63__value_or(this, default_value);
}
#endif // __cplusplus
// --------
// The "defined(__clang__)" isn't redundant. While vanilla clang defines
// __GNUC__, clang-cl (which mimics MSVC's cl.exe) does not.
#if (defined(__GNUC__) || defined(__clang__)) && (__SIZEOF_LONG__ == 8)
+2 -1
View File
@@ -169,7 +169,8 @@ wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {
// wuffs_base__io_buffer__compact_retaining moves any written but unread bytes
// closer to the start of the buffer. It retains H bytes of history (the most
// recently read bytes), where H is min(buf->meta.ri, history_retain_length).
// A postcondition is that buf->meta.ri == H.
// It is therefore a no-op if history_retain_length is UINT64_MAX. A
// postcondition is that buf->meta.ri == H.
//
// wuffs_base__io_buffer__compact_retaining(0) is equivalent to
// wuffs_base__io_buffer__compact().
+2 -1
View File
@@ -451,7 +451,8 @@ wuffs_base__token_buffer__compact(wuffs_base__token_buffer* buf) {
// wuffs_base__token_buffer__compact_retaining moves any written but unread
// tokens closer to the start of the buffer. It retains H tokens of history
// (the most recently read tokens), where H is min(buf->meta.ri,
// history_retain_length). A postcondition is that buf->meta.ri == H.
// history_retain_length). It is therefore a no-op if history_retain_length is
// UINT64_MAX. A postcondition is that buf->meta.ri == H.
//
// wuffs_base__token_buffer__compact_retaining(0) is equivalent to
// wuffs_base__token_buffer__compact().