Skip to content

Commit

Permalink
fix compile
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 23, 2024
1 parent 2e78fe7 commit a4c55ec
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ add_executable(test_nothrow ${TEST_NOTHROW})
add_executable(test_util ${TEST_UTIL})
add_executable(test_pb ${TEST_PB})

option(HAS_PROTOBUF "import protobuf" OFF)
if(HAS_PROTOBUF)
find_package(Protobuf QUIET)
if(Protobuf_FOUND)
add_definitions(-DSTRUCT_PB_WITH_PROTO)
find_package(Protobuf REQUIRED)
if(Protobuf_FOUND)
message(STATUS "Found Protobuf: ${Protobuf_VERSION}")
Expand Down
44 changes: 43 additions & 1 deletion iguana/pb_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,55 @@ IGUANA_INLINE size_t variant_uint32_size(uint32_t value) {
return static_cast<size_t>((log2value * 9 + 73) / 64);
}

IGUANA_INLINE int Log2FloorNonZero_Portable(uint32_t n) {
if (n == 0)
return -1;
int log = 0;
uint32_t value = n;
for (int i = 4; i >= 0; --i) {
int shift = (1 << i);
uint32_t x = value >> shift;
if (x != 0) {
value = x;
log += shift;
}
}
assert(value == 1);
return log;
}

IGUANA_INLINE uint32_t Log2FloorNonZero(uint32_t n) {
#if defined(__GNUC__)
return 31 ^ static_cast<uint32_t>(__builtin_clz(n));
#elif defined(_MSC_VER)
unsigned long where;
_BitScanReverse(&where, n);
return where;
#else
return Log2FloorNonZero_Portable(n);
#endif
}

IGUANA_INLINE int Log2FloorNonZero64_Portable(uint64_t n) {
const uint32_t topbits = static_cast<uint32_t>(n >> 32);
if (topbits == 0) {
// Top bits are zero, so scan in bottom bits
return static_cast<int>(Log2FloorNonZero(static_cast<uint32_t>(n)));
}
else {
return 32 + static_cast<int>(Log2FloorNonZero(topbits));
}
}

IGUANA_INLINE uint32_t log2_floor_uint64(uint64_t n) {
#if defined(__GNUC__)
return 63 ^ static_cast<uint32_t>(__builtin_clzll(n));
#else
#elif defined(_MSC_VER) && defined(_M_X64)
unsigned long where;
_BitScanReverse64(&where, n);
return where;
#else
return Log2FloorNonZero64_Portable(n);
#endif
}

Expand Down
48 changes: 26 additions & 22 deletions test/proto/unittest_proto3.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include "data_def.pb.h"
#include "iguana/pb_reader.hpp"
#include "iguana/pb_writer.hpp"
#if defined(STRUCT_PB_WITH_PROTO)
#include "data_def.pb.h"
#include "unittest_proto3.pb.h" // protoc gen
#endif

#define PB_CHECK assert
#define PUBLIC : iguana::pb_base
Expand Down Expand Up @@ -204,6 +206,28 @@ REFLECTION(bench_int32, a, b, c, d);

} // namespace stpb

inline auto create_person() {
stpb::person p{0, 432798, std::string(1024, 'A'), 24, 65536.42};
return p;
}

inline stpb::Monster create_sp_monster() {
stpb::Monster m = {
0,
{0, 1, 2, 3},
16,
24,
"it is a test",
"\1\2\3\4",
stpb::Color::Red,
{{0, "gun", 42}, {0, "shotgun", 56}},
{0, "air craft", 67},
{{0, 7, 8, 9}, {0, 71, 81, 91}},
};
return m;
}

#if defined(STRUCT_PB_WITH_PROTO)
namespace protobuf_sample {
inline mygame::person create_person() {
mygame::person p;
Expand Down Expand Up @@ -250,27 +274,6 @@ inline mygame::Monster create_monster() {
}
} // namespace protobuf_sample

inline auto create_person() {
stpb::person p{0, 432798, std::string(1024, 'A'), 24, 65536.42};
return p;
}

inline stpb::Monster create_sp_monster() {
stpb::Monster m = {
0,
{0, 1, 2, 3},
16,
24,
"it is a test",
"\1\2\3\4",
stpb::Color::Red,
{{0, "gun", 42}, {0, "shotgun", 56}},
{0, "air craft", 67},
{{0, 7, 8, 9}, {0, 71, 81, 91}},
};
return m;
}

void SetBaseTypeMsg(const stpb::BaseTypeMsg& st, pb::BaseTypeMsg& msg) {
msg.set_optional_int32(st.optional_int32);
msg.set_optional_int64(st.optional_int64);
Expand Down Expand Up @@ -572,6 +575,7 @@ void CheckNestOneofMsg(const stpb::NestOneofMsg& st,
},
st.nest_one_of_msg);
}
#endif

inline void print_hex_str(const std::string& str) {
std::ostringstream oss;
Expand Down
2 changes: 2 additions & 0 deletions test/test_proto3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "doctest.h"
#include "proto/unittest_proto3.h" // msg reflection

#if defined(STRUCT_PB_WITH_PROTO)
TEST_CASE("test BaseTypeMsg") {
{ // normal test
stpb::BaseTypeMsg se_st{0, 100, 200, 300, 400,
Expand Down Expand Up @@ -553,6 +554,7 @@ TEST_CASE("test NestOneofMsg ") {
CheckNestOneofMsg(dese_st, dese_msg);
}
}
#endif

DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
int main(int argc, char **argv) { return doctest::Context(argc, argv).run(); }
Expand Down

0 comments on commit a4c55ec

Please sign in to comment.