mirror of
https://github.com/protobuf-c/protobuf-c.git
synced 2025-01-14 17:30:07 +08:00
Merge pull request #400 from protobuf-c/ilya-zigzag
protobuf-c.c: Make zigzag encoding more compact
This commit is contained in:
commit
ef868b712f
@ -312,10 +312,9 @@ int32_size(int32_t v)
|
|||||||
static inline uint32_t
|
static inline uint32_t
|
||||||
zigzag32(int32_t v)
|
zigzag32(int32_t v)
|
||||||
{
|
{
|
||||||
if (v < 0)
|
// Note: the right-shift must be arithmetic
|
||||||
return (-(uint32_t)v) * 2 - 1;
|
// Note: left shift must be unsigned because of overflow
|
||||||
else
|
return ((uint32_t)(v) << 1) ^ (uint32_t)(v >> 31);
|
||||||
return (uint32_t)(v) * 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -377,10 +376,9 @@ uint64_size(uint64_t v)
|
|||||||
static inline uint64_t
|
static inline uint64_t
|
||||||
zigzag64(int64_t v)
|
zigzag64(int64_t v)
|
||||||
{
|
{
|
||||||
if (v < 0)
|
// Note: the right-shift must be arithmetic
|
||||||
return (-(uint64_t)v) * 2 - 1;
|
// Note: left shift must be unsigned because of overflow
|
||||||
else
|
return ((uint64_t)(v) << 1) ^ (uint64_t)(v >> 63);
|
||||||
return (uint64_t)(v) * 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2423,10 +2421,8 @@ parse_int32(unsigned len, const uint8_t *data)
|
|||||||
static inline int32_t
|
static inline int32_t
|
||||||
unzigzag32(uint32_t v)
|
unzigzag32(uint32_t v)
|
||||||
{
|
{
|
||||||
if (v & 1)
|
// Note: Using unsigned types prevents undefined behavior
|
||||||
return -(v >> 1) - 1;
|
return (int32_t)((v >> 1) ^ (~(v & 1) + 1));
|
||||||
else
|
|
||||||
return v >> 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint32_t
|
static inline uint32_t
|
||||||
@ -2467,10 +2463,8 @@ parse_uint64(unsigned len, const uint8_t *data)
|
|||||||
static inline int64_t
|
static inline int64_t
|
||||||
unzigzag64(uint64_t v)
|
unzigzag64(uint64_t v)
|
||||||
{
|
{
|
||||||
if (v & 1)
|
// Note: Using unsigned types prevents undefined behavior
|
||||||
return -(v >> 1) - 1;
|
return (int64_t)((v >> 1) ^ (~(v & 1) + 1));
|
||||||
else
|
|
||||||
return v >> 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint64_t
|
static inline uint64_t
|
||||||
|
Loading…
x
Reference in New Issue
Block a user