Skip to content

Commit 6c5d9ee

Browse files
Add variable-length-quantity exercise
1 parent 722d6f7 commit 6c5d9ee

File tree

12 files changed

+3898
-0
lines changed

12 files changed

+3898
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@
370370
"prerequisites": [],
371371
"difficulty": 6
372372
},
373+
{
374+
"slug": "variable-length-quantity",
375+
"name": "Variable Length Quantity",
376+
"uuid": "52f28bf9-4da4-46bc-a293-2054106c6e55",
377+
"practices": [],
378+
"prerequisites": [],
379+
"difficulty": 6
380+
},
373381
{
374382
"slug": "pythagorean-triplet",
375383
"name": "Pythagorean Triplet",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Instructions
2+
3+
Implement variable length quantity encoding and decoding.
4+
5+
The goal of this exercise is to implement [VLQ][vlq] encoding/decoding.
6+
7+
In short, the goal of this encoding is to encode integer values in a way that would save bytes.
8+
Only the first 7 bits of each byte are significant (right-justified; sort of like an ASCII byte).
9+
So, if you have a 32-bit value, you have to unpack it into a series of 7-bit bytes.
10+
Of course, you will have a variable number of bytes depending upon your integer.
11+
To indicate which is the last byte of the series, you leave bit #7 clear.
12+
In all of the preceding bytes, you set bit #7.
13+
14+
So, if an integer is between `0-127`, it can be represented as one byte.
15+
Although VLQ can deal with numbers of arbitrary sizes, for this exercise we will restrict ourselves to only numbers that fit in a 32-bit unsigned integer.
16+
Here are examples of integers as 32-bit values, and the variable length quantities that they translate to:
17+
18+
```text
19+
NUMBER VARIABLE QUANTITY
20+
00000000 00
21+
00000040 40
22+
0000007F 7F
23+
00000080 81 00
24+
00002000 C0 00
25+
00003FFF FF 7F
26+
00004000 81 80 00
27+
00100000 C0 80 00
28+
001FFFFF FF FF 7F
29+
00200000 81 80 80 00
30+
08000000 C0 80 80 00
31+
0FFFFFFF FF FF FF 7F
32+
```
33+
34+
[vlq]: https://en.wikipedia.org/wiki/Variable-length_quantity
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"variable_length_quantity.s"
8+
],
9+
"test": [
10+
"variable_length_quantity_test.c"
11+
],
12+
"example": [
13+
".meta/example.s"
14+
]
15+
},
16+
"blurb": "Implement variable length quantity encoding and decoding.",
17+
"source": "A poor Splice developer having to implement MIDI encoding/decoding.",
18+
"source_url": "https://splice.com"
19+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.text
2+
.globl encode
3+
.globl decode
4+
5+
/* extern size_t encode(uint8_t *buffer, const uint32_t *integers, size_t integer_count); */
6+
encode:
7+
lsl x2, x2, #2 /* size of integers array, in bytes */
8+
add x2, x1, x2 /* end of integers */
9+
mov x3, x0 /* start of output */
10+
11+
.encode_loop:
12+
cmp x1, x2
13+
beq .encode_end
14+
15+
ldr w4, [x1], #4
16+
cmp w4, #127
17+
bls .one
18+
19+
lsr w5, w4, #7
20+
cmp w5, #127
21+
bls .two
22+
23+
lsr w6, w5, #7
24+
cmp w6, #127
25+
bls .three
26+
27+
lsr w7, w6, #7
28+
cmp w7, #127
29+
bls .four
30+
31+
lsr w8, w7, #7
32+
orr w8, w8, #128
33+
strb w8, [x0], #1
34+
35+
.four:
36+
orr w7, w7, #128
37+
strb w7, [x0], #1
38+
39+
.three:
40+
orr w6, w6, #128
41+
strb w6, [x0], #1
42+
43+
.two:
44+
orr w5, w5, #128
45+
strb w5, [x0], #1
46+
47+
.one:
48+
and w4, w4, #127
49+
strb w4, [x0], #1
50+
b .encode_loop
51+
52+
.encode_end:
53+
sub x0, x0, x3
54+
ret
55+
56+
/* extern size_t decode(uint32_t *buffer, const uint8_t *integers, size_t integer_count); */
57+
decode:
58+
add x2, x1, x2 /* end of integers array */
59+
mov x3, x0 /* start of output */
60+
61+
.loop:
62+
mov w4, wzr
63+
cmp x1, x2
64+
beq .end
65+
66+
.read:
67+
ldrb w5, [x1], #1
68+
lsl w4, w4, #7
69+
and w6, w5, #127
70+
orr w4, w4, w6
71+
tst w5, #128
72+
bne .read
73+
74+
str w4, [x0], #4
75+
b .loop
76+
77+
.end:
78+
sub x0, x0, x3 /* length of output, in bytes */
79+
lsr x0, x0, #2 /* number of uint32_t values output */
80+
ret
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[35c9db2e-f781-4c52-b73b-8e76427defd0]
13+
description = "Encode a series of integers, producing a series of bytes. -> zero"
14+
15+
[be44d299-a151-4604-a10e-d4b867f41540]
16+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary single byte"
17+
18+
[ea399615-d274-4af6-bbef-a1c23c9e1346]
19+
description = "Encode a series of integers, producing a series of bytes. -> largest single byte"
20+
21+
[77b07086-bd3f-4882-8476-8dcafee79b1c]
22+
description = "Encode a series of integers, producing a series of bytes. -> smallest double byte"
23+
24+
[63955a49-2690-4e22-a556-0040648d6b2d]
25+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary double byte"
26+
27+
[29da7031-0067-43d3-83a7-4f14b29ed97a]
28+
description = "Encode a series of integers, producing a series of bytes. -> largest double byte"
29+
30+
[3345d2e3-79a9-4999-869e-d4856e3a8e01]
31+
description = "Encode a series of integers, producing a series of bytes. -> smallest triple byte"
32+
33+
[5df0bc2d-2a57-4300-a653-a75ee4bd0bee]
34+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary triple byte"
35+
36+
[f51d8539-312d-4db1-945c-250222c6aa22]
37+
description = "Encode a series of integers, producing a series of bytes. -> largest triple byte"
38+
39+
[da78228b-544f-47b7-8bfe-d16b35bbe570]
40+
description = "Encode a series of integers, producing a series of bytes. -> smallest quadruple byte"
41+
42+
[11ed3469-a933-46f1-996f-2231e05d7bb6]
43+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quadruple byte"
44+
45+
[d5f3f3c3-e0f1-4e7f-aad0-18a44f223d1c]
46+
description = "Encode a series of integers, producing a series of bytes. -> largest quadruple byte"
47+
48+
[91a18b33-24e7-4bfb-bbca-eca78ff4fc47]
49+
description = "Encode a series of integers, producing a series of bytes. -> smallest quintuple byte"
50+
51+
[5f34ff12-2952-4669-95fe-2d11b693d331]
52+
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quintuple byte"
53+
54+
[7489694b-88c3-4078-9864-6fe802411009]
55+
description = "Encode a series of integers, producing a series of bytes. -> maximum 32-bit integer input"
56+
57+
[f9b91821-cada-4a73-9421-3c81d6ff3661]
58+
description = "Encode a series of integers, producing a series of bytes. -> two single-byte values"
59+
60+
[68694449-25d2-4974-ba75-fa7bb36db212]
61+
description = "Encode a series of integers, producing a series of bytes. -> two multi-byte values"
62+
63+
[51a06b5c-de1b-4487-9a50-9db1b8930d85]
64+
description = "Encode a series of integers, producing a series of bytes. -> many multi-byte values"
65+
66+
[baa73993-4514-4915-bac0-f7f585e0e59a]
67+
description = "Decode a series of bytes, producing a series of integers. -> one byte"
68+
69+
[72e94369-29f9-46f2-8c95-6c5b7a595aee]
70+
description = "Decode a series of bytes, producing a series of integers. -> two bytes"
71+
72+
[df5a44c4-56f7-464e-a997-1db5f63ce691]
73+
description = "Decode a series of bytes, producing a series of integers. -> three bytes"
74+
75+
[1bb58684-f2dc-450a-8406-1f3452aa1947]
76+
description = "Decode a series of bytes, producing a series of integers. -> four bytes"
77+
78+
[cecd5233-49f1-4dd1-a41a-9840a40f09cd]
79+
description = "Decode a series of bytes, producing a series of integers. -> maximum 32-bit integer"
80+
81+
[e7d74ba3-8b8e-4bcb-858d-d08302e15695]
82+
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error"
83+
include = false
84+
85+
[aa378291-9043-4724-bc53-aca1b4a3fcb6]
86+
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error, even if value is zero"
87+
include = false
88+
89+
[a91e6f5a-c64a-48e3-8a75-ce1a81e0ebee]
90+
description = "Decode a series of bytes, producing a series of integers. -> multiple values"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AS = aarch64-linux-gnu-as
2+
CC = aarch64-linux-gnu-gcc
3+
4+
CFLAGS = -g -Wall -Wextra -pedantic -Werror
5+
LDFLAGS =
6+
7+
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
8+
9+
ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
10+
ALL_LDFLAGS += $(LDFLAGS)
11+
12+
C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
13+
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
14+
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)
15+
16+
CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<
17+
18+
all: tests
19+
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<
20+
21+
tests: $(ALL_OBJS)
22+
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)
23+
24+
%.o: %.s
25+
@$(AS) -o $@ $<
26+
27+
%.o: %.c
28+
@$(CC_CMD)
29+
30+
vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
31+
@$(CC_CMD)
32+
33+
clean:
34+
@rm -f *.o vendor/*.o tests
35+
36+
.PHONY: all clean
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.text
2+
.globl encode
3+
.globl decode
4+
5+
encode:
6+
ret
7+
8+
decode:
9+
ret

0 commit comments

Comments
 (0)