Add some more sat_add and sat_sub methods

This commit is contained in:
Nigel Tao committed 2018-04-17 10:59:21 +10:00
1 parent 9c2330e6fd
commit 24c906bd52
12 files changed
+272 -7

No files matched your search

+24
View File
@@ -53,6 +53,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+8 -7
View File
@@ -20,13 +20,14 @@ const baseHeader = "" +
"#ifndef WUFFS_BASE_HEADER_H\n#define WUFFS_BASE_HEADER_H\n\n// Copyright 2017 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Wuffs assumes that:\n// - converting a uint32_t to a size_t will never overflow.\n// - converting a size_t to a uint64_t will never overflow.\n#if (__WORDSIZE != 32) && (__WORDSIZE != 64)\n#error \"Wuffs requires a word size of either 32 or 64 bits\"\n#endif\n\n// WUFFS_VERSION is the major.minor version number " +
"as a uint32_t. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define WUFFS_VERSION ((uint32_t)0x00001)\n\n" +
"" +
"// ---------------- Fundamentals\n\n// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a\n// second. See https://github.com/OculusVR/Flicks\ntypedef uint64_t wuffs_base__flicks;\n\n#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)\n\n// Saturating arithmetic (sat_add, sat_sub) branchless bit-twiddling algorithms\n// are per https://locklessinc.com/articles/sat_arithmetic/\n//\n// It is important that the underlying types are unsigned integers, as signed\n// integer arithmetic overflow is undefined behavior in C.\n\nstatic inline uint32_t wuffs_base__u32__sat_add(uint32_t x, uint32_t y) {\n uint32_t res = x + y;\n res |= -(res < x);\n return res;\n}\n\nstatic inline uint32_t wuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {\n uint32_t res = x - y;\n res &= -(res <= x);\n return res;\n}\n\nstatic inline uint64_t wuffs_base__u64__sat_add(uint64_t x, uint64_t y) {\n uint64_t res = x + y;\n res |= -(res < x);\n return res;\n}\n\nstatic inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {\n" +
" uint64_t res = x - y;\n res &= -(res <= x);\n return res;\n}\n\n// Ranges are either inclusive (\"range_ii\") or exclusive (\"range_ie\") on the\n// high end. Both the \"ii\" and \"ie\" flavors are useful in practice.\n//\n// The \"ei\" and \"ee\" flavors also exist in theory, but aren't widely used. In\n// Wuffs, the low end is always inclusive.\n//\n// The \"ii\" (closed interval) flavor is useful when refining e.g. \"the set of\n// all uint32_t values\" to a contiguous subset: \"uint32_t values in the closed\n// interval [M, N]\", for uint32_t values M and N. An unrefined type (in other\n// words, the set of all uint32_t values) is not representable in the \"ie\"\n// flavor because if N equals ((1<<32) - 1) then (N + 1) will overflow.\n//\n// On the other hand, the \"ie\" (half-open interval) flavor is recommended by\n// Dijkstra's \"Why numbering should start at zero\" at\n// http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF and a further\n// discussion of motivating rationale is at\n// https://www.quora.com/Why-are-Python-ranges-half-open-e" +
"xclusive-instead-of-closed-inclusive\n//\n// For example, with \"ie\", the number of elements in \"uint32_t values in the\n// half-open interval [M, N)\" is equal to max(0, N-M). Furthermore, that number\n// of elements (in one dimension, a length, in two dimensions, a width or\n// height) is itself representable as a uint32_t without overflow, again for\n// uint32_t values M and N. In the contrasting \"ii\" flavor, the length of the\n// closed interval [0, (1<<32) - 1] is 1<<32, which cannot be represented as a\n// uint32_t. In Wuffs, because of this potential overflow, the \"ie\" flavor has\n// length / width / height methods, but the \"ii\" flavor does not.\n//\n// It is valid for min > max (for range_ii) or for min >= max (for range_ie),\n// in which case the range is empty. There are multiple representations of an\n// empty range.\n\ntypedef struct {\n uint32_t min_inclusive;\n uint32_t max_inclusive;\n} wuffs_base__range_ii_u32;\n\nstatic inline bool wuffs_base__range_ii_u32__contains(\n wuffs_base__range_ii_u32 r,\n uint32_t " +
"x) {\n return (r.min_inclusive <= x) && (x <= r.max_inclusive);\n}\n\ntypedef struct {\n uint32_t min_inclusive;\n uint32_t max_exclusive;\n} wuffs_base__range_ie_u32;\n\nstatic inline bool wuffs_base__range_ie_u32__contains(\n wuffs_base__range_ie_u32 r,\n uint32_t x) {\n return (r.min_inclusive <= x) && (x < r.max_exclusive);\n}\n\nstatic inline uint32_t wuffs_base__range_ie_u32__length(\n wuffs_base__range_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_exclusive, r.min_inclusive);\n}\n\ntypedef struct {\n uint64_t min_inclusive;\n uint64_t max_inclusive;\n} wuffs_base__range_ii_u64;\n\nstatic inline bool wuffs_base__range_ii_u64__contains(\n wuffs_base__range_ii_u64 r,\n uint64_t x) {\n return (r.min_inclusive <= x) && (x <= r.max_inclusive);\n}\n\ntypedef struct {\n uint64_t min_inclusive;\n uint64_t max_exclusive;\n} wuffs_base__range_ie_u64;\n\nstatic inline bool wuffs_base__range_ie_u64__contains(\n wuffs_base__range_ie_u64 r,\n uint64_t x) {\n return (r.min_inclusive <= x) && (x < r.max_exclusive);\n}\n\ns" +
"tatic inline uint64_t wuffs_base__range_ie_u64__length(\n wuffs_base__range_ie_u64 r) {\n return wuffs_base__u64__sat_sub(r.max_exclusive, r.min_inclusive);\n}\n\n// wuffs_base__rect_ii_u32 is a rectangle (a 2-dimensional range) on the\n// integer grid. The \"ii\" means that the bounds are inclusive on the low end\n// and inclusive on the high end. It contains all points (x, y) such that\n// ((min_inclusive_x <= x) && (x <= max_inclusive_x)) and likewise for y.\n//\n// It is valid for min > max, in which case the rectangle is empty. There are\n// multiple representations of an empty rectangle.\n//\n// The X and Y axes increase right and down.\ntypedef struct {\n uint32_t min_inclusive_x;\n uint32_t min_inclusive_y;\n uint32_t max_inclusive_x;\n uint32_t max_inclusive_y;\n} wuffs_base__rect_ii_u32;\n\nstatic inline bool wuffs_base__rect_ii_u32__contains(wuffs_base__rect_ii_u32 r,\n uint32_t x,\n uint32_t y) {\n return (r.min" +
"_inclusive_x <= x) && (x <= r.max_inclusive_x) &&\n (r.min_inclusive_y <= y) && (y <= r.max_inclusive_y);\n}\n\n// wuffs_base__rect_ie_u32 is a rectangle (a 2-dimensional range) on the\n// integer grid. The \"ie\" means that the bounds are inclusive on the low end\n// and exclusive on the high end. It contains all points (x, y) such that\n// ((min_inclusive_x <= x) && (x < max_exclusive_x)) and likewise for y.\n//\n// It is valid for min >= max, in which case the rectangle is empty. There are\n// multiple representations of an empty rectangle, including a value with all\n// fields zero.\n//\n// The X and Y axes increase right and down.\ntypedef struct {\n uint32_t min_inclusive_x;\n uint32_t min_inclusive_y;\n uint32_t max_exclusive_x;\n uint32_t max_exclusive_y;\n} wuffs_base__rect_ie_u32;\n\nstatic inline bool wuffs_base__rect_ie_u32__contains(wuffs_base__rect_ie_u32 r,\n uint32_t x,\n uint32_t y) {\n return (r.min_in" +
"clusive_x <= x) && (x < r.max_exclusive_x) &&\n (r.min_inclusive_y <= y) && (y < r.max_exclusive_y);\n}\n\nstatic inline uint32_t wuffs_base__rect_ie_u32__width(\n wuffs_base__rect_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_exclusive_x, r.min_inclusive_x);\n}\n\nstatic inline uint32_t wuffs_base__rect_ie_u32__height(\n wuffs_base__rect_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_exclusive_y, r.min_inclusive_y);\n}\n\n// wuffs_base__slice_u8 is a 1-dimensional buffer.\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} wuffs_base__slice_u8;\n\n// wuffs_base__table_u8 is a 2-dimensional buffer.\n//\n// A value with all fields NULL or zero is a valid, empty table.\ntypedef struct {\n uint8_t* ptr;\n size_t width;\n size_t height;\n size_t stride;\n} wuffs_base__table_u8;\n\n" +
"// ---------------- Fundamentals\n\n// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a\n// second. See https://github.com/OculusVR/Flicks\ntypedef uint64_t wuffs_base__flicks;\n\n#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)\n\n// Saturating arithmetic (sat_add, sat_sub) branchless bit-twiddling algorithms\n// are per https://locklessinc.com/articles/sat_arithmetic/\n//\n// It is important that the underlying types are unsigned integers, as signed\n// integer arithmetic overflow is undefined behavior in C.\n\nstatic inline uint8_t wuffs_base__u8__sat_add(uint8_t x, uint8_t y) {\n uint8_t res = x + y;\n res |= -(res < x);\n return res;\n}\n\nstatic inline uint8_t wuffs_base__u8__sat_sub(uint8_t x, uint8_t y) {\n uint8_t res = x - y;\n res &= -(res <= x);\n return res;\n}\n\nstatic inline uint16_t wuffs_base__u16__sat_add(uint16_t x, uint16_t y) {\n uint16_t res = x + y;\n res |= -(res < x);\n return res;\n}\n\nstatic inline uint16_t wuffs_base__u16__sat_sub(uint16_t x, uint16_t y) {\n uint16_t" +
" res = x - y;\n res &= -(res <= x);\n return res;\n}\n\nstatic inline uint32_t wuffs_base__u32__sat_add(uint32_t x, uint32_t y) {\n uint32_t res = x + y;\n res |= -(res < x);\n return res;\n}\n\nstatic inline uint32_t wuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {\n uint32_t res = x - y;\n res &= -(res <= x);\n return res;\n}\n\nstatic inline uint64_t wuffs_base__u64__sat_add(uint64_t x, uint64_t y) {\n uint64_t res = x + y;\n res |= -(res < x);\n return res;\n}\n\nstatic inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {\n uint64_t res = x - y;\n res &= -(res <= x);\n return res;\n}\n\n// Ranges are either inclusive (\"range_ii\") or exclusive (\"range_ie\") on the\n// high end. Both the \"ii\" and \"ie\" flavors are useful in practice.\n//\n// The \"ei\" and \"ee\" flavors also exist in theory, but aren't widely used. In\n// Wuffs, the low end is always inclusive.\n//\n// The \"ii\" (closed interval) flavor is useful when refining e.g. \"the set of\n// all uint32_t values\" to a contiguous subset: \"uint32_t values in the clo" +
"sed\n// interval [M, N]\", for uint32_t values M and N. An unrefined type (in other\n// words, the set of all uint32_t values) is not representable in the \"ie\"\n// flavor because if N equals ((1<<32) - 1) then (N + 1) will overflow.\n//\n// On the other hand, the \"ie\" (half-open interval) flavor is recommended by\n// Dijkstra's \"Why numbering should start at zero\" at\n// http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF and a further\n// discussion of motivating rationale is at\n// https://www.quora.com/Why-are-Python-ranges-half-open-exclusive-instead-of-closed-inclusive\n//\n// For example, with \"ie\", the number of elements in \"uint32_t values in the\n// half-open interval [M, N)\" is equal to max(0, N-M). Furthermore, that number\n// of elements (in one dimension, a length, in two dimensions, a width or\n// height) is itself representable as a uint32_t without overflow, again for\n// uint32_t values M and N. In the contrasting \"ii\" flavor, the length of the\n// closed interval [0, (1<<32) - 1] is 1<<32, which cannot be " +
"represented as a\n// uint32_t. In Wuffs, because of this potential overflow, the \"ie\" flavor has\n// length / width / height methods, but the \"ii\" flavor does not.\n//\n// It is valid for min > max (for range_ii) or for min >= max (for range_ie),\n// in which case the range is empty. There are multiple representations of an\n// empty range.\n\ntypedef struct {\n uint32_t min_inclusive;\n uint32_t max_inclusive;\n} wuffs_base__range_ii_u32;\n\nstatic inline bool wuffs_base__range_ii_u32__contains(\n wuffs_base__range_ii_u32 r,\n uint32_t x) {\n return (r.min_inclusive <= x) && (x <= r.max_inclusive);\n}\n\ntypedef struct {\n uint32_t min_inclusive;\n uint32_t max_exclusive;\n} wuffs_base__range_ie_u32;\n\nstatic inline bool wuffs_base__range_ie_u32__contains(\n wuffs_base__range_ie_u32 r,\n uint32_t x) {\n return (r.min_inclusive <= x) && (x < r.max_exclusive);\n}\n\nstatic inline uint32_t wuffs_base__range_ie_u32__length(\n wuffs_base__range_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_exclusive, r.min_inclusive" +
");\n}\n\ntypedef struct {\n uint64_t min_inclusive;\n uint64_t max_inclusive;\n} wuffs_base__range_ii_u64;\n\nstatic inline bool wuffs_base__range_ii_u64__contains(\n wuffs_base__range_ii_u64 r,\n uint64_t x) {\n return (r.min_inclusive <= x) && (x <= r.max_inclusive);\n}\n\ntypedef struct {\n uint64_t min_inclusive;\n uint64_t max_exclusive;\n} wuffs_base__range_ie_u64;\n\nstatic inline bool wuffs_base__range_ie_u64__contains(\n wuffs_base__range_ie_u64 r,\n uint64_t x) {\n return (r.min_inclusive <= x) && (x < r.max_exclusive);\n}\n\nstatic inline uint64_t wuffs_base__range_ie_u64__length(\n wuffs_base__range_ie_u64 r) {\n return wuffs_base__u64__sat_sub(r.max_exclusive, r.min_inclusive);\n}\n\n// wuffs_base__rect_ii_u32 is a rectangle (a 2-dimensional range) on the\n// integer grid. The \"ii\" means that the bounds are inclusive on the low end\n// and inclusive on the high end. It contains all points (x, y) such that\n// ((min_inclusive_x <= x) && (x <= max_inclusive_x)) and likewise for y.\n//\n// It is valid for min > " +
"max, in which case the rectangle is empty. There are\n// multiple representations of an empty rectangle.\n//\n// The X and Y axes increase right and down.\ntypedef struct {\n uint32_t min_inclusive_x;\n uint32_t min_inclusive_y;\n uint32_t max_inclusive_x;\n uint32_t max_inclusive_y;\n} wuffs_base__rect_ii_u32;\n\nstatic inline bool wuffs_base__rect_ii_u32__contains(wuffs_base__rect_ii_u32 r,\n uint32_t x,\n uint32_t y) {\n return (r.min_inclusive_x <= x) && (x <= r.max_inclusive_x) &&\n (r.min_inclusive_y <= y) && (y <= r.max_inclusive_y);\n}\n\n// wuffs_base__rect_ie_u32 is a rectangle (a 2-dimensional range) on the\n// integer grid. The \"ie\" means that the bounds are inclusive on the low end\n// and exclusive on the high end. It contains all points (x, y) such that\n// ((min_inclusive_x <= x) && (x < max_exclusive_x)) and likewise for y.\n//\n// It is valid for min >= max, in which case the rectangle is empty. The" +
"re are\n// multiple representations of an empty rectangle, including a value with all\n// fields zero.\n//\n// The X and Y axes increase right and down.\ntypedef struct {\n uint32_t min_inclusive_x;\n uint32_t min_inclusive_y;\n uint32_t max_exclusive_x;\n uint32_t max_exclusive_y;\n} wuffs_base__rect_ie_u32;\n\nstatic inline bool wuffs_base__rect_ie_u32__contains(wuffs_base__rect_ie_u32 r,\n uint32_t x,\n uint32_t y) {\n return (r.min_inclusive_x <= x) && (x < r.max_exclusive_x) &&\n (r.min_inclusive_y <= y) && (y < r.max_exclusive_y);\n}\n\nstatic inline uint32_t wuffs_base__rect_ie_u32__width(\n wuffs_base__rect_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_exclusive_x, r.min_inclusive_x);\n}\n\nstatic inline uint32_t wuffs_base__rect_ie_u32__height(\n wuffs_base__rect_ie_u32 r) {\n return wuffs_base__u32__sat_sub(r.max_exclusive_y, r.min_inclusive_y);\n}\n\n// wuffs_base__slice_u8 is a 1-dimensional buffe" +
"r.\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} wuffs_base__slice_u8;\n\n// wuffs_base__table_u8 is a 2-dimensional buffer.\n//\n// A value with all fields NULL or zero is a valid, empty table.\ntypedef struct {\n uint8_t* ptr;\n size_t width;\n size_t height;\n size_t stride;\n} wuffs_base__table_u8;\n\n" +
"" +
"// ---------------- I/O\n\n// wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, plus an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} wuffs_base__io_buffer;\n\n// wuffs_base__io_limit provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct wuffs_base__io_limit {\n uint64_t* ptr_to_len; // Pointer to N.\n struct wuffs_base__io_limit* next; // Linked list of limits.\n} wuffs_base__io_limit;\n\ntypedef" +
" struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n wuffs_base__io_buffer* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__io_limit limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__io_reader;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n wuffs_base__io_buffer* buf;\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__io_limit limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__io_writer;\n\n" +
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);
+24
View File
@@ -56,6 +56,30 @@ typedef uint64_t wuffs_base__flicks;
// 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);