Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/46acab3b1f178b8196d6 to your computer and use it in GitHub Desktop.
Save FROGGS/46acab3b1f178b8196d6 to your computer and use it in GitHub Desktop.
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