Skip to content

Commit

Permalink
vlq_base128_le: fix type issues in statically typed langs (C#, C++)
Browse files Browse the repository at this point in the history
Fix #559
  • Loading branch information
generalmimon committed Oct 5, 2023
1 parent 36ded65 commit 51fc327
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions common/vlq_base128_le.ksy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ meta:
id: vlq_base128_le
title: Variable length quantity, unsigned/signed integer, base128, little-endian
license: CC0-1.0
ks-version: 0.7
ks-version: 0.9
bit-endian: be
doc: |
A variable-length unsigned/signed integer using base128 encoding. 1-byte groups
consist of 1-bit flag of continuation and 7-bit value chunk, and are ordered
Expand Down Expand Up @@ -35,31 +36,28 @@ types:
doc: |
One byte group, clearly divided into 7-bit "value" chunk and 1-bit "continuation" flag.
seq:
- id: b
type: u1
instances:
has_next:
value: (b & 0b1000_0000) != 0
- id: has_next
type: b1
doc: If true, then we have more bytes to read
value:
value: b & 0b0111_1111
- id: value
type: b7
doc: The 7-bit (base128) numeric value chunk of this group
instances:
len:
value: groups.size
value:
value: >-
groups[0].value
value: |
(groups[0].value
+ (len >= 2 ? (groups[1].value << 7) : 0)
+ (len >= 3 ? (groups[2].value << 14) : 0)
+ (len >= 4 ? (groups[3].value << 21) : 0)
+ (len >= 5 ? (groups[4].value << 28) : 0)
+ (len >= 6 ? (groups[5].value << 35) : 0)
+ (len >= 7 ? (groups[6].value << 42) : 0)
+ (len >= 8 ? (groups[7].value << 49) : 0)
+ (len >= 8 ? (groups[7].value << 49) : 0)).as<u8>
doc: Resulting unsigned value as normal integer
sign_bit:
value: '1 << (7 * len - 1)'
value: '(1.as<u8> << (7 * len - 1)).as<u8>'
value_signed:
value: '(value ^ sign_bit) - sign_bit'
value: '((value ^ sign_bit).as<s8> - sign_bit.as<s8>).as<s8>'
doc-ref: https://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend

0 comments on commit 51fc327

Please sign in to comment.