Skip to content

Commit

Permalink
Raise minimum count for iterate to 2 alternate to 3
Browse files Browse the repository at this point in the history
Sligly saves space but mostly removes overlapping encodings
  • Loading branch information
ariscop authored and Rangi42 committed Mar 8, 2023
1 parent 8444a78 commit ca95feb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
4 changes: 1 addition & 3 deletions home/decompress.asm
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ DEF LZ_LONG_HI EQU %00000011
ld [hli], a
pop de

; assume count >= 3, because < 3 is nonsense
dec bc
dec bc
jr .lz_copy_repeat

.lz_copy
Expand Down Expand Up @@ -288,6 +285,7 @@ DEF LZ_LONG_HI EQU %00000011

.lz_iterate
ld a, [de]
ld [hli], a
inc de
jr .fill

Expand Down
6 changes: 4 additions & 2 deletions tools/lz/dpcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void process_input(void) {
unsigned count = 0;
do {
count++;
consider(plen, (struct command) {
if (!current_byte || count >= 2)
consider(plen, (struct command) {
.command = current_byte ? LZ_REPEAT : LZ_ZERO,
.count = count,
.value = current_byte,
Expand All @@ -78,7 +79,8 @@ void process_input(void) {
count = 1;
do {
count++;
consider(plen, (struct command) {
if (count >= 3)
consider(plen, (struct command) {
.command = LZ_ALTERNATE,
.count = count,
.value = (data[plen - count + 1] << 8) | (data[plen - count]),
Expand Down
8 changes: 6 additions & 2 deletions tools/lz/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ void write_commands_to_file (const char * file, const struct command * commands,
}

void write_command_to_file (FILE * fp, struct command command, const unsigned char * input_stream) {
if ((!command.count) || (command.count > MAX_COMMAND_COUNT)) error_exit(2, "invalid command in output stream");
unsigned char buf[4];
unsigned char * pos = buf;
int n;
command.count --;

command.count -= minimum_count(command.command);

/* Check for over and under sized commands */
if (command.count > MAX_COMMAND_COUNT - 1) error_exit(2, "invalid command in output stream");

if (command.count < SHORT_COMMAND_COUNT)
*(pos ++) = (command.command << 5) + command.count;
else {
Expand Down
1 change: 1 addition & 0 deletions tools/lz/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ unsigned char * get_uncompressed_data(const struct command *, const unsigned cha
// util.c
noreturn error_exit(int, const char *, ...);
unsigned char * read_file_into_buffer(const char *, unsigned short *);
unsigned minimum_count(unsigned command);
short command_size(struct command);
unsigned short compressed_length(const struct command *, unsigned short);

Expand Down
2 changes: 1 addition & 1 deletion tools/lz/uncomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct command * get_commands_from_file (const unsigned char * data, unsigned sh
if (!(remaining --)) goto error;
current -> count |= *(rp ++);
}
current -> count ++;
current -> count += minimum_count(current -> command);
switch (current -> command) {
case LZ_DATA:
if (remaining <= current -> count) goto error;
Expand Down
13 changes: 12 additions & 1 deletion tools/lz/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,19 @@ unsigned char * read_file_into_buffer (const char * file, unsigned short * size)
return buf;
}

unsigned minimum_count (unsigned command) {
switch (command) {
case LZ_ALTERNATE:
return 3;
case LZ_REPEAT:
return 2;
default:
return 1;
}
}

short command_size (struct command command) {
short header_size = 1 + (command.count > SHORT_COMMAND_COUNT);
short header_size = 1 + (command.count - minimum_count(command.command) > SHORT_COMMAND_COUNT - 1);
if (command.command & 4) return header_size + 1 + (command.value >= 0);
return header_size + command.command[(short []) {command.count, 1, 2, 0}];
}
Expand Down

0 comments on commit ca95feb

Please sign in to comment.