This is an automated email from the ASF dual-hosted git repository.

thiru pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 25459ec  Modernizing Zigzag (#1104)
25459ec is described below

commit 25459ec86ca469ee3ead855bb78556b2f09b8a24
Author: Thiruvalluvan M G <[email protected]>
AuthorDate: Sun Feb 28 09:09:51 2021 +0530

    Modernizing Zigzag (#1104)
---
 lang/c++/api/Zigzag.hh  | 28 ++++++++++++++-------
 lang/c++/impl/Zigzag.cc | 67 +++++++++++++++----------------------------------
 2 files changed, 39 insertions(+), 56 deletions(-)

diff --git a/lang/c++/api/Zigzag.hh b/lang/c++/api/Zigzag.hh
index d0259b8..201a2ad 100644
--- a/lang/c++/api/Zigzag.hh
+++ b/lang/c++/api/Zigzag.hh
@@ -19,7 +19,7 @@
 #ifndef avro_Encoding_hh__
 #define avro_Encoding_hh__
 
-#include <stdint.h>
+#include <cstdint>
 #include <array>
 #include <cstddef>
 
@@ -29,14 +29,24 @@
 
 namespace avro {
 
-AVRO_DECL uint64_t encodeZigzag64(int64_t input);
-AVRO_DECL int64_t decodeZigzag64(uint64_t input);
-
-AVRO_DECL uint32_t encodeZigzag32(int32_t input);
-AVRO_DECL int32_t decodeZigzag32(uint32_t input);
-
-AVRO_DECL size_t encodeInt32(int32_t input, std::array<uint8_t, 5> &output);
-AVRO_DECL size_t encodeInt64(int64_t input, std::array<uint8_t, 10> &output);
+AVRO_DECL constexpr uint64_t encodeZigzag64(int64_t input) noexcept {
+    // cppcheck-suppress shiftTooManyBitsSigned
+    return ((input << 1) ^ (input >> 63));
+}
+AVRO_DECL constexpr int64_t decodeZigzag64(uint64_t input) noexcept {
+    return static_cast<int64_t>(((input >> 1) ^ -(static_cast<int64_t>(input) 
& 1)));
+}
+
+AVRO_DECL constexpr uint32_t encodeZigzag32(int32_t input) noexcept {
+    // cppcheck-suppress shiftTooManyBitsSigned
+    return ((input << 1) ^ (input >> 31));
+}
+AVRO_DECL constexpr int32_t decodeZigzag32(uint32_t input) noexcept {
+    return static_cast<int32_t>(((input >> 1) ^ -(static_cast<int64_t>(input) 
& 1)));
+}
+
+AVRO_DECL size_t encodeInt32(int32_t input, std::array<uint8_t, 5> &output) 
noexcept;
+AVRO_DECL size_t encodeInt64(int64_t input, std::array<uint8_t, 10> &output) 
noexcept;
 
 } // namespace avro
 
diff --git a/lang/c++/impl/Zigzag.cc b/lang/c++/impl/Zigzag.cc
index 06db5b4..0b38053 100644
--- a/lang/c++/impl/Zigzag.cc
+++ b/lang/c++/impl/Zigzag.cc
@@ -20,66 +20,39 @@
 #include "Zigzag.hh"
 
 namespace avro {
-
-uint64_t
-encodeZigzag64(int64_t input)
-{
-    // cppcheck-suppress shiftTooManyBitsSigned
-    return ((input << 1) ^ (input >> 63));
-}
-
-int64_t
-decodeZigzag64(uint64_t input)
-{
-    return static_cast<int64_t>(((input >> 1) ^ -(static_cast<int64_t>(input) 
& 1)));
-}
-
-uint32_t
-encodeZigzag32(int32_t input)
-{
-    // cppcheck-suppress shiftTooManyBitsSigned
-    return ((input << 1) ^ (input >> 31));
-}
-
-int32_t
-decodeZigzag32(uint32_t input)
-{
-    return static_cast<int32_t>(((input >> 1) ^ -(static_cast<int64_t>(input) 
& 1)));
-}
-
+// TODO: The following two functions have exactly the same code except for the 
type.
+// They should be implemented as a template.
 size_t
-encodeInt64(int64_t input, std::array<uint8_t, 10> &output)
-{
-    // get the zigzag encoding
-    uint64_t val = encodeZigzag64(input);
+encodeInt64(int64_t input, std::array<uint8_t, 10> &output) noexcept {
+    auto val = encodeZigzag64(input);
 
     // put values in an array of bytes with variable length encoding
-    const int mask  = 0x7F;
-    output[0] = val & mask;
-    size_t bytesOut = 1;
-    while( val >>=7 ) {
-        output[bytesOut-1] |= 0x80;
-        output[bytesOut++] = (val & mask);
+    const int mask = 0x7F;
+    auto v = val & mask;
+    size_t bytesOut = 0;
+    while (val >>= 7) {
+        output[bytesOut++] = (v | 0x80);
+        v = val & mask;
     }
 
+    output[bytesOut++] = v;
     return bytesOut;
 }
 
 size_t
-encodeInt32(int32_t input, std::array<uint8_t, 5> &output)
-{
-    // get the zigzag encoding
-    uint32_t val = encodeZigzag32(input);
+encodeInt32(int32_t input, std::array<uint8_t, 5> &output) noexcept {
+    auto val = encodeZigzag32(input);
 
     // put values in an array of bytes with variable length encoding
-    const int mask  = 0x7F;
-    output[0] = val & mask;
-    size_t bytesOut = 1;
-    while( val >>=7 ) {
-        output[bytesOut-1] |= 0x80;
-        output[bytesOut++] = (val & mask);
+    const int mask = 0x7F;
+    auto v = val & mask;
+    size_t bytesOut = 0;
+    while (val >>= 7) {
+        output[bytesOut++] = (v | 0x80);
+        v = val & mask;
     }
 
+    output[bytesOut++] = v;
     return bytesOut;
 }
 

Reply via email to