Add wuffs_base__uxx__byte_swapped funcs

This commit is contained in:
Nigel Tao committed 2018-05-02 15:31:18 +10:00
1 parent 4e55710afc
commit 13bc1a22a5
14 files changed
+444

No files matched your search

+34
View File
@@ -162,6 +162,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+2
View File
@@ -27,6 +27,8 @@ const baseHeader = "" +
"// --------\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" +
"" +
"// --------\n\n// Clang also defines \"__GNUC__\".\n\nstatic inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {\n#if defined(__GNUC__)\n return __builtin_bswap16(x);\n#else\n return (x >> 8) | (x << 8);\n#endif\n}\n\nstatic inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {\n#if defined(__GNUC__)\n return __builtin_bswap32(x);\n#else\n static const uint32_t mask8 = 0x00FF00FF;\n x = ((x >> 8) & mask8) | ((x & mask8) << 8);\n return (x >> 16) | (x << 16);\n#endif\n}\n\nstatic inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {\n#if defined(__GNUC__)\n return __builtin_bswap64(x);\n#else\n static const uint64_t mask8 = 0x00FF00FF00FF00FF;\n static const uint64_t mask16 = 0x0000FFFF0000FFFF;\n x = ((x >> 8) & mask8) | ((x & mask8) << 8);\n x = ((x >> 16) & mask16) | ((x & mask16) << 16);\n return (x >> 32) | (x << 32);\n#endif\n}\n\n" +
"" +
"// ---------------- Ranges and Rects\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-exclusive-instead-of-close" +
"d-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__is_empty(\n wuffs_base__range_ii_u32 r) {\n return r.min_inclusive > r.max_inc" +
"lusive;\n}\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\nstatic inline wuffs_base__range_ii_u32 wuffs_base__range_ii_u32__intersection(\n wuffs_base__range_ii_u32 r,\n wuffs_base__range_ii_u32 s) {\n r.min_inclusive = wuffs_base__u32__max(r.min_inclusive, s.min_inclusive);\n r.max_inclusive = wuffs_base__u32__min(r.max_inclusive, s.max_inclusive);\n return r;\n}\n\nstatic inline wuffs_base__range_ii_u32 wuffs_base__range_ii_u32__union(\n wuffs_base__range_ii_u32 r,\n wuffs_base__range_ii_u32 s) {\n if (wuffs_base__range_ii_u32__is_empty(r)) {\n return s;\n }\n if (wuffs_base__range_ii_u32__is_empty(s)) {\n return r;\n }\n r.min_inclusive = wuffs_base__u32__min(r.min_inclusive, s.min_inclusive);\n r.max_inclusive = wuffs_base__u32__max(r.max_inclusive, s.max_inclusive);\n return r;\n}\n\n" +
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
+34
View File
@@ -165,6 +165,40 @@ static inline uint64_t wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
return res;
}
// --------
// Clang also defines "__GNUC__".
static inline uint16_t wuffs_base__u16__byte_swapped(uint16_t x) {
#if defined(__GNUC__)
return __builtin_bswap16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
static inline uint32_t wuffs_base__u32__byte_swapped(uint32_t x) {
#if defined(__GNUC__)
return __builtin_bswap32(x);
#else
static const uint32_t mask8 = 0x00FF00FF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
return (x >> 16) | (x << 16);
#endif
}
static inline uint64_t wuffs_base__u64__byte_swapped(uint64_t x) {
#if defined(__GNUC__)
return __builtin_bswap64(x);
#else
static const uint64_t mask8 = 0x00FF00FF00FF00FF;
static const uint64_t mask16 = 0x0000FFFF0000FFFF;
x = ((x >> 8) & mask8) | ((x & mask8) << 8);
x = ((x >> 16) & mask16) | ((x & mask16) << 16);
return (x >> 32) | (x << 32);
#endif
}
// ---------------- Ranges and Rects
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the