Date: Wednesday, February 1, 2006 @ 16:39:16
Author: marc
Path: /cvsroot/carob/carob/src
Modified: SQLDataSerialization.cpp (1.24 -> 1.25)
Unrolled loops in portable int/float-point converters. Thanks to Zsolt for the
benchmarking. CAROB-31.
--------------------------+
SQLDataSerialization.cpp | 53 +++++++++++++++++++++++++++++----------------
1 files changed, 35 insertions(+), 18 deletions(-)
Index: carob/src/SQLDataSerialization.cpp
diff -u carob/src/SQLDataSerialization.cpp:1.24
carob/src/SQLDataSerialization.cpp:1.25
--- carob/src/SQLDataSerialization.cpp:1.24 Wed Feb 1 16:26:08 2006
+++ carob/src/SQLDataSerialization.cpp Wed Feb 1 16:39:16 2006
@@ -103,7 +103,7 @@
// Float
-uint32_t
+inline uint32_t
SQLDataSerialization::floatToU32Bits(float f)
{
// "union casting" using { float; uint32_t; } or the simpler:
@@ -115,19 +115,23 @@
// The more portable code below should not cost more than a couple
// of CPU cycles/converted float
const unsigned char *bytes = reinterpret_cast<const unsigned char *>(&f);
- uint32_t ires = 0;
- for (int c=0, shift=0; c<4; c++, shift+=8)
- ires |= (uint32_t) bytes[c] << shift;
- return ires;
+ return (uint32_t) bytes[3] << 24
+ | (uint32_t) bytes[2] << 16
+ | (uint32_t) bytes[1] << 8
+ | (uint32_t) bytes[0];
}
-float
+inline float
SQLDataSerialization::U32BitsToFloat(const uint32_t ui)
{
float res;
- unsigned char *bytes = reinterpret_cast<unsigned char *>(&res);
- for (int c=0, shift=0; c<4; c++, shift+=8)
- bytes[c] = ui >> shift;
+ unsigned char *f_bytes = reinterpret_cast<unsigned char *>(&res);
+
+ f_bytes[3] = ui >> 24;
+ f_bytes[2] = ui >> 16;
+ f_bytes[1] = ui >> 8;
+ f_bytes[0] = ui;
+
return res;
}
@@ -157,23 +161,36 @@
}
// Double
-uint64_t
+inline uint64_t
SQLDataSerialization::doubleToU64Bits(double d)
{
- uint64_t ires = 0;
const unsigned char *bytes = reinterpret_cast<const unsigned char *>(&d);
- for (int c=0, shift=0; c<8; c++, shift+=8)
- ires |= (uint64_t) bytes[c] << shift;
- return ires;
+
+ return (uint64_t) bytes[7] << 56
+ | (uint64_t) bytes[6] << 48
+ | (uint64_t) bytes[5] << 40
+ | (uint64_t) bytes[4] << 32
+ | (uint64_t) bytes[3] << 24
+ | (uint64_t) bytes[2] << 16
+ | (uint64_t) bytes[1] << 8
+ | (uint64_t) bytes[0];
}
-double
+inline double
SQLDataSerialization::U64BitsToDouble(const uint64_t ui)
{
double res;
- unsigned char *bytes = reinterpret_cast<unsigned char *>(&res);
- for (int c=0, shift=0; c<8; c++, shift+=8)
- bytes[c] = ui >> shift;
+ unsigned char *d_bytes = reinterpret_cast<unsigned char *>(&res);
+
+ d_bytes[7] = ui >> 56;
+ d_bytes[6] = ui >> 48;
+ d_bytes[5] = ui >> 40;
+ d_bytes[4] = ui >> 32;
+ d_bytes[3] = ui >> 24;
+ d_bytes[2] = ui >> 16;
+ d_bytes[1] = ui >> 8;
+ d_bytes[0] = ui;
+
return res;
}
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits