mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 17:01:35 +01:00
Refactor cgen's base/base-{private,public}.h
This commit is contained in:
1 parent
3f3772e74b
commit
86b8408223
15 files changed
+1150
-1013
No files matched your search
@@ -0,0 +1,335 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// Copyright 2017 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Wuffs assumes that:
|
||||
// - converting a uint32_t to a size_t will never overflow.
|
||||
// - converting a size_t to a uint64_t will never overflow.
|
||||
#ifdef __WORDSIZE
|
||||
#if (__WORDSIZE != 32) && (__WORDSIZE != 64)
|
||||
#error "Wuffs requires a word size of either 32 or 64 bits"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// WUFFS_VERSION is the major.minor.patch version, as per https://semver.org/,
|
||||
// as a uint64_t. The major number is the high 32 bits. The minor number is the
|
||||
// middle 16 bits. The patch number is the low 16 bits. The version extension
|
||||
// (such as "", "beta" or "rc.1") is part of the string representation (such as
|
||||
// "1.2.3-beta") but not the uint64_t representation.
|
||||
//
|
||||
// All three of major, minor and patch being zero means that this is a
|
||||
// work-in-progress version, not a release version, and has no backwards or
|
||||
// forwards compatibility guarantees.
|
||||
//
|
||||
// !! Some code generation programs can override WUFFS_VERSION.
|
||||
#define WUFFS_VERSION ((uint64_t)0)
|
||||
#define WUFFS_VERSION_MAJOR ((uint64_t)0)
|
||||
#define WUFFS_VERSION_MINOR ((uint64_t)0)
|
||||
#define WUFFS_VERSION_PATCH ((uint64_t)0)
|
||||
#define WUFFS_VERSION_EXTENSION ""
|
||||
#define WUFFS_VERSION_STRING "0.0.0"
|
||||
|
||||
// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have
|
||||
// static storage. The motivation is discussed in the "ALLOW STATIC
|
||||
// IMPLEMENTATION" section of
|
||||
// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt
|
||||
#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS
|
||||
#define WUFFS_BASE__MAYBE_STATIC static
|
||||
#else
|
||||
#define WUFFS_BASE__MAYBE_STATIC
|
||||
#endif
|
||||
|
||||
// Clang also defines "__GNUC__".
|
||||
#if defined(__GNUC__)
|
||||
#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))
|
||||
#else
|
||||
#define WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
#endif
|
||||
|
||||
// wuffs_base__empty_struct is used when a Wuffs function returns an empty
|
||||
// struct. In C, if a function f returns void, you can't say "x = f()", but in
|
||||
// Wuffs, if a function g returns empty, you can say "y = g()".
|
||||
typedef struct {
|
||||
// private_impl is a placeholder field. It isn't explicitly used, except that
|
||||
// without it, the sizeof a struct with no fields can differ across C/C++
|
||||
// compilers, and it is undefined behavior in C99. For example, gcc says that
|
||||
// the sizeof an empty struct is 0, and g++ says that it is 1. This leads to
|
||||
// ABI incompatibility if a Wuffs .c file is processed by one compiler and
|
||||
// its .h file with another compiler.
|
||||
//
|
||||
// Instead, we explicitly insert an otherwise unused field, so that the
|
||||
// sizeof this struct is always 1.
|
||||
uint8_t private_impl;
|
||||
} wuffs_base__empty_struct;
|
||||
|
||||
// wuffs_base__utility is a placeholder receiver type. It enables what Java
|
||||
// calls static methods, as opposed to regular methods.
|
||||
typedef struct {
|
||||
// private_impl is a placeholder field. It isn't explicitly used, except that
|
||||
// without it, the sizeof a struct with no fields can differ across C/C++
|
||||
// compilers, and it is undefined behavior in C99. For example, gcc says that
|
||||
// the sizeof an empty struct is 0, and g++ says that it is 1. This leads to
|
||||
// ABI incompatibility if a Wuffs .c file is processed by one compiler and
|
||||
// its .h file with another compiler.
|
||||
//
|
||||
// Instead, we explicitly insert an otherwise unused field, so that the
|
||||
// sizeof this struct is always 1.
|
||||
uint8_t private_impl;
|
||||
} wuffs_base__utility;
|
||||
|
||||
// --------
|
||||
|
||||
// A status is either NULL (meaning OK) or a string message. That message is
|
||||
// human-readable, for programmers, but it is not for end users. It is not
|
||||
// localized, and does not contain additional contextual information such as a
|
||||
// source filename.
|
||||
//
|
||||
// Status strings are statically allocated and should never be free'd. They can
|
||||
// be compared by the == operator and not just by strcmp.
|
||||
//
|
||||
// Statuses come in four categories:
|
||||
// - OK: the request was completed, successfully.
|
||||
// - Warnings: the request was completed, unsuccessfully.
|
||||
// - Suspensions: the request was not completed, but can be re-tried.
|
||||
// - Errors: the request was not completed, permanently.
|
||||
//
|
||||
// When a function returns an incomplete status, a suspension means that that
|
||||
// function should be called again within a new context, such as after flushing
|
||||
// or re-filling an I/O buffer. An error means that an irrecoverable failure
|
||||
// state was reached.
|
||||
typedef const char* wuffs_base__status;
|
||||
|
||||
// !! INSERT wuffs_base__status names.
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_complete(wuffs_base__status z) {
|
||||
return (z == NULL) || ((*z != '$') && (*z != '?'));
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_error(wuffs_base__status z) {
|
||||
return z && (*z == '?');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_ok(wuffs_base__status z) {
|
||||
return z == NULL;
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_suspension(wuffs_base__status z) {
|
||||
return z && (*z == '$');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_warning(wuffs_base__status z) {
|
||||
return z && (*z != '$') && (*z != '?');
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a
|
||||
// second. See https://github.com/OculusVR/Flicks
|
||||
typedef int64_t wuffs_base__flicks;
|
||||
|
||||
#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)
|
||||
#define WUFFS_BASE__FLICKS_PER_MILLISECOND ((uint64_t)705600)
|
||||
|
||||
// ---------------- Numeric Types
|
||||
|
||||
static inline uint8_t //
|
||||
wuffs_base__u8__min(uint8_t x, uint8_t y) {
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
static inline uint8_t //
|
||||
wuffs_base__u8__max(uint8_t x, uint8_t y) {
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__min(uint16_t x, uint16_t y) {
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__max(uint16_t x, uint16_t y) {
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__min(uint32_t x, uint32_t y) {
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__max(uint32_t x, uint32_t y) {
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__min(uint64_t x, uint64_t y) {
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__max(uint64_t x, uint64_t y) {
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
// Saturating arithmetic (sat_add, sat_sub) branchless bit-twiddling algorithms
|
||||
// are per https://locklessinc.com/articles/sat_arithmetic/
|
||||
//
|
||||
// It is important that the underlying types are unsigned integers, as signed
|
||||
// integer arithmetic overflow is undefined behavior in C.
|
||||
|
||||
static inline uint8_t //
|
||||
wuffs_base__u8__sat_add(uint8_t x, uint8_t y) {
|
||||
uint8_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint8_t //
|
||||
wuffs_base__u8__sat_sub(uint8_t x, uint8_t y) {
|
||||
uint8_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__sat_add(uint16_t x, uint16_t y) {
|
||||
uint16_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__sat_sub(uint16_t x, uint16_t y) {
|
||||
uint16_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__sat_add(uint32_t x, uint32_t y) {
|
||||
uint32_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {
|
||||
uint32_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__sat_add(uint64_t x, uint64_t y) {
|
||||
uint64_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
|
||||
uint64_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
// ---------------- Slices and Tables
|
||||
|
||||
// WUFFS_BASE__SLICE is a 1-dimensional buffer.
|
||||
//
|
||||
// len measures a number of elements, not necessarily a size in bytes.
|
||||
//
|
||||
// A value with all fields NULL or zero is a valid, empty slice.
|
||||
#define WUFFS_BASE__SLICE(T) \
|
||||
struct { \
|
||||
T* ptr; \
|
||||
size_t len; \
|
||||
}
|
||||
|
||||
// WUFFS_BASE__TABLE is a 2-dimensional buffer.
|
||||
//
|
||||
// width height, and stride measure a number of elements, not necessarily a
|
||||
// size in bytes.
|
||||
//
|
||||
// A value with all fields NULL or zero is a valid, empty table.
|
||||
#define WUFFS_BASE__TABLE(T) \
|
||||
struct { \
|
||||
T* ptr; \
|
||||
size_t width; \
|
||||
size_t height; \
|
||||
size_t stride; \
|
||||
}
|
||||
|
||||
typedef WUFFS_BASE__SLICE(uint8_t) wuffs_base__slice_u8;
|
||||
typedef WUFFS_BASE__SLICE(uint16_t) wuffs_base__slice_u16;
|
||||
typedef WUFFS_BASE__SLICE(uint32_t) wuffs_base__slice_u32;
|
||||
typedef WUFFS_BASE__SLICE(uint64_t) wuffs_base__slice_u64;
|
||||
|
||||
typedef WUFFS_BASE__TABLE(uint8_t) wuffs_base__table_u8;
|
||||
typedef WUFFS_BASE__TABLE(uint16_t) wuffs_base__table_u16;
|
||||
typedef WUFFS_BASE__TABLE(uint32_t) wuffs_base__table_u32;
|
||||
typedef WUFFS_BASE__TABLE(uint64_t) wuffs_base__table_u64;
|
||||
|
||||
// wuffs_base__slice_u8__subslice_i returns s[i:].
|
||||
//
|
||||
// It returns an empty slice if i is out of bounds.
|
||||
static inline wuffs_base__slice_u8 //
|
||||
wuffs_base__slice_u8__subslice_i(wuffs_base__slice_u8 s, uint64_t i) {
|
||||
if ((i <= SIZE_MAX) && (i <= s.len)) {
|
||||
return ((wuffs_base__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = s.len - i,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// wuffs_base__slice_u8__subslice_j returns s[:j].
|
||||
//
|
||||
// It returns an empty slice if j is out of bounds.
|
||||
static inline wuffs_base__slice_u8 //
|
||||
wuffs_base__slice_u8__subslice_j(wuffs_base__slice_u8 s, uint64_t j) {
|
||||
if ((j <= SIZE_MAX) && (j <= s.len)) {
|
||||
return ((wuffs_base__slice_u8){
|
||||
.ptr = s.ptr,
|
||||
.len = j,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// wuffs_base__slice_u8__subslice_ij returns s[i:j].
|
||||
//
|
||||
// It returns an empty slice if i or j is out of bounds.
|
||||
static inline wuffs_base__slice_u8 //
|
||||
wuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s,
|
||||
uint64_t i,
|
||||
uint64_t j) {
|
||||
if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {
|
||||
return ((wuffs_base__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = j - i,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
Reference in new issue
Block a user