-
-
Save FROGGS/46acab3b1f178b8196d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iff --git a/src/6model/serialization.c b/src/6model/serialization.c | |
index ce3157e..9d1aec9 100644 | |
--- a/src/6model/serialization.c | |
+++ b/src/6model/serialization.c | |
@@ -201,10 +201,25 @@ static void write_double(char *buffer, size_t offset, double value) { | |
/* Writes an int64 into up to 128 bits of storage. | |
* Returns how far to advance the offset. */ | |
static size_t varintsize(int64_t value) { | |
- int8_t sign_nudge = value < 0 ? 0: 1; | |
- size_t varlog = ceil(log(abs(value) + sign_nudge) / log(2)); | |
- size_t needed_bytes = floor((varlog) / 7) + 1; | |
- return needed_bytes; | |
+ if(value < 0) | |
+ value = abs(value++); | |
+ if(value < 64) /* 7 bits */ | |
+ return 1; | |
+ if(value < 8192) /* 14 bits */ | |
+ return 2; | |
+ if(value < 1048576) /* 21 bits */ | |
+ return 3; | |
+ if(value < 134217728) /* 28 bits */ | |
+ return 4; | |
+ if(value < 17179869184) /* 35 bits */ | |
+ return 5; | |
+ if(value < 2199023255552) /* 42 bits */ | |
+ return 6; | |
+ if(value < 281474976710656) /* 49 bits */ | |
+ return 7; | |
+ if(value < 36028797018963968) /* 56 bits */ | |
+ return 8; | |
+ return 9; | |
} | |
static size_t write_varint9(MVMuint8 *buffer, size_t offset, int64_t value) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment