Use std::byteswap instead of custom implementations.

- Also allow signed ints now because std::byteswap accepts them.
- Fix for style check.
This commit is contained in:
Priyansh Agrawal 2023-10-07 23:59:24 +00:00
parent 302291e17b
commit 2c04370e5f
5 changed files with 35 additions and 27 deletions

View File

@ -716,7 +716,7 @@ Result:
Accepts an unsigned integer `operand` and returns the integer which is obtained by swapping the **endianness** of `operand` i.e. reversing the bytes of the `operand`.
Currently, this is implemented for UInt8, UInt16, UInt32 and UInt64.
Currently, this is supported for up to 64-bit (signed and unsigned) integers.
**Example**

View File

@ -11,31 +11,10 @@ extern const int NOT_IMPLEMENTED;
namespace
{
template <typename T>
requires std::is_same_v<T, UInt8>
requires std::is_integral_v<T>
inline T byteSwap(T x)
{
return x;
}
template <typename T>
requires std::is_same_v<T, UInt16>
inline T byteSwap(T x)
{
return __builtin_bswap16(x);
}
template <typename T>
requires std::is_same_v<T, UInt32>
inline T byteSwap(T x)
{
return __builtin_bswap32(x);
}
template <typename T>
requires std::is_same_v<T, UInt64>
inline T byteSwap(T x)
{
return __builtin_bswap64(x);
return std::byteswap(x);
}
template <typename T>
@ -76,9 +55,9 @@ REGISTER_FUNCTION(ByteSwap)
factory.registerFunction<FunctionByteSwap>(
FunctionDocumentation{
.description = R"(
Accepts an unsigned integer `operand` and returns the integer which is obtained by swapping the **endianness** of `operand` i.e. reversing the bytes of the `operand`.
Accepts an integer `operand` and returns the integer which is obtained by swapping the **endianness** of `operand` i.e. reversing the bytes of the `operand`.
Currently, this is implemented for UInt8, UInt16, UInt32 and UInt64.
Currently, this is supported for up to 64-bit (signed and unsigned) integers.
**Example**

View File

@ -13,3 +13,15 @@
18439412204227788800
123294967295
18446744073709551615
0
-1
-128
32767
-9745
128
-8388609
3676125013305458687
128
-549755813889
4039370097989451775
128

View File

@ -15,4 +15,20 @@ SELECT byteSwap(4294967295);
SELECT byteSwap(4294967296);
SELECT byteSwap(123294967295);
SELECT byteSwap(18439412204227788800);
SELECT byteSwap(18446744073709551615);
SELECT byteSwap(18446744073709551615);
SELECT byteSwap(-0);
SELECT byteSwap(-1);
SELECT byteSwap(-128);
SELECT byteSwap(-129);
SELECT byteSwap(-4135);
SELECT byteSwap(-32768);
SELECT byteSwap(-32769);
SELECT byteSwap(-3351772109);
SELECT byteSwap(-2147483648);
SELECT byteSwap(-2147483649);
SELECT byteSwap(-1242525266376);
SELECT byteswap(-9223372036854775808);

View File

@ -1415,6 +1415,7 @@ encodeXMLComponent
encodings
encryptions
endian
endianness
endsWith
endsWithUTF
enum