mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 00:47:56 +01:00
Add some more sat_add and sat_sub methods
This commit is contained in:
1 parent
9c2330e6fd
commit
24c906bd52
12 files changed
+272
-7
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user