From 13bc1a22a5568af4003dd6dc786e06f8e423eb11 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 2 May 2018 15:31:18 +1000 Subject: [PATCH] Add wuffs_base__uxx__byte_swapped funcs --- cmd/wuffs-c/internal/cgen/base-header.h | 34 +++++++++++++++++++++++++ cmd/wuffs-c/internal/cgen/data.go | 2 ++ gen/c/std/adler32.c | 34 +++++++++++++++++++++++++ gen/c/std/crc32.c | 34 +++++++++++++++++++++++++ gen/c/std/deflate.c | 34 +++++++++++++++++++++++++ gen/c/std/gif.c | 34 +++++++++++++++++++++++++ gen/c/std/gzip.c | 34 +++++++++++++++++++++++++ gen/c/std/zlib.c | 34 +++++++++++++++++++++++++ gen/h/std/adler32.h | 34 +++++++++++++++++++++++++ gen/h/std/crc32.h | 34 +++++++++++++++++++++++++ gen/h/std/deflate.h | 34 +++++++++++++++++++++++++ gen/h/std/gif.h | 34 +++++++++++++++++++++++++ gen/h/std/gzip.h | 34 +++++++++++++++++++++++++ gen/h/std/zlib.h | 34 +++++++++++++++++++++++++ 14 files changed, 444 insertions(+) diff --git a/cmd/wuffs-c/internal/cgen/base-header.h b/cmd/wuffs-c/internal/cgen/base-header.h index f0926a32..16beda64 100644 --- a/cmd/wuffs-c/internal/cgen/base-header.h +++ b/cmd/wuffs-c/internal/cgen/base-header.h @@ -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 diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 5a0007ef..c0f20d8f 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -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" + diff --git a/gen/c/std/adler32.c b/gen/c/std/adler32.c index 5a2220c2..3c942d0d 100644 --- a/gen/c/std/adler32.c +++ b/gen/c/std/adler32.c @@ -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 diff --git a/gen/c/std/crc32.c b/gen/c/std/crc32.c index 82603f76..fef0a1b6 100644 --- a/gen/c/std/crc32.c +++ b/gen/c/std/crc32.c @@ -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 diff --git a/gen/c/std/deflate.c b/gen/c/std/deflate.c index d11753af..455efc24 100644 --- a/gen/c/std/deflate.c +++ b/gen/c/std/deflate.c @@ -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 diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 50acc6b3..9e98f302 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -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 diff --git a/gen/c/std/gzip.c b/gen/c/std/gzip.c index 0cf5a532..d86545de 100644 --- a/gen/c/std/gzip.c +++ b/gen/c/std/gzip.c @@ -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 diff --git a/gen/c/std/zlib.c b/gen/c/std/zlib.c index 5ace73c0..8ddea9a7 100644 --- a/gen/c/std/zlib.c +++ b/gen/c/std/zlib.c @@ -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 diff --git a/gen/h/std/adler32.h b/gen/h/std/adler32.h index 35cdf3fd..243e76d4 100644 --- a/gen/h/std/adler32.h +++ b/gen/h/std/adler32.h @@ -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 diff --git a/gen/h/std/crc32.h b/gen/h/std/crc32.h index beca8b9d..8569cecd 100644 --- a/gen/h/std/crc32.h +++ b/gen/h/std/crc32.h @@ -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 diff --git a/gen/h/std/deflate.h b/gen/h/std/deflate.h index 10560a17..f3002578 100644 --- a/gen/h/std/deflate.h +++ b/gen/h/std/deflate.h @@ -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 diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index af5fe7ce..8901b1c0 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -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 diff --git a/gen/h/std/gzip.h b/gen/h/std/gzip.h index ae968e4e..93f14bb0 100644 --- a/gen/h/std/gzip.h +++ b/gen/h/std/gzip.h @@ -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 diff --git a/gen/h/std/zlib.h b/gen/h/std/zlib.h index 4a0c922f..2393c683 100644 --- a/gen/h/std/zlib.h +++ b/gen/h/std/zlib.h @@ -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