From faf8c4b64114b5f81673867726ba0dc81ea6360f Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 23 Apr 2024 03:19:07 -0500 Subject: [PATCH] Tweaked alt-tag encoding to match color/dir naming order This is mainly to avoid mistakes caused by names/encodings disagreeing: LFSR_TAG_ALT 0x4kkk v1cd kkkk -kkk kkkk ^ ^^ '------+-----' '-||--------|------- valid bit '|--------|------- color '--------|------- dir '------- key Notably, the LFSR_TAG_ALT() macro has already caused issues by being both 1. ambiguous, and 2. not really type-checkable. It's easy to get the order wrong and things not really break, just behave poorly, it's really not great! To be honest the exact order is a bit arbitrary, the color->dir naming appeared by accident because I guess it felt more natural. Maybe because of English's weird implicit adjective ordering? Maybe because of how often conditions show up as the last part of the name in other instruction sets? At least one plus is that this moves the dir-bit next to the key. This makes it so all of the condition information is encoding is the lowest 13-bits of the tag, which may lead to minor optimization tricks for implementing flips and such. Code changes: code stack before: 34080 2864 after: 34068 (-0.0%) 2864 (+0.0%) --- lfs.c | 30 +++++++++++++++--------------- scripts/dbgbmap.py | 4 ++-- scripts/dbgbtree.py | 4 ++-- scripts/dbglfs.py | 4 ++-- scripts/dbgmtree.py | 4 ++-- scripts/dbgrbyd.py | 8 ++++---- scripts/dbgtag.py | 4 ++-- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lfs.c b/lfs.c index 9adf76db..79b111a7 100644 --- a/lfs.c +++ b/lfs.c @@ -758,10 +758,10 @@ enum lfsr_tag { // alt pointers form the inner nodes of our rbyd trees LFSR_TAG_ALT = 0x4000, - LFSR_TAG_LE = 0x0000, - LFSR_TAG_GT = 0x2000, LFSR_TAG_B = 0x0000, - LFSR_TAG_R = 0x1000, + LFSR_TAG_R = 0x2000, + LFSR_TAG_LE = 0x0000, + LFSR_TAG_GT = 0x1000, // checksum tags LFSR_TAG_CKSUM = 0x3000, @@ -783,10 +783,10 @@ enum lfsr_tag { }; // some other tag encodings with their own subfields -#define LFSR_TAG_ALT(d, c, key) \ +#define LFSR_TAG_ALT(c, d, key) \ (LFSR_TAG_ALT \ - | (0x2000 & (d)) \ - | (0x1000 & (c)) \ + | (0x2000 & (c)) \ + | (0x1000 & (d)) \ | (0x0fff & (lfsr_tag_t)(key))) #define LFSR_TAG_UATTR(attr) \ @@ -874,11 +874,11 @@ static inline bool lfsr_tag_isgt(lfsr_tag_t tag) { } static inline bool lfsr_tag_isa(lfsr_tag_t tag) { - return (tag & 0x2fff) == (LFSR_TAG_GT | 0); + return (tag & 0x1fff) == (LFSR_TAG_GT | 0); } static inline bool lfsr_tag_isn(lfsr_tag_t tag) { - return (tag & 0x2fff) == (LFSR_TAG_LE | 0); + return (tag & 0x1fff) == (LFSR_TAG_LE | 0); } static inline lfsr_tag_t lfsr_tag_isparallel(lfsr_tag_t a, lfsr_tag_t b) { @@ -3010,7 +3010,7 @@ trunk:; // stitch together both trunks err = lfsr_p_push(lfs, rbyd, p, - LFSR_TAG_ALT(LFSR_TAG_LE, LFSR_TAG_B, d_tag), + LFSR_TAG_ALT(LFSR_TAG_B, LFSR_TAG_LE, d_tag), d_rid - (lower_rid - weight), jump); if (err) { @@ -3105,7 +3105,7 @@ trunk:; // .-'| .--' // 3 4 3 4 x } else { - alt = LFSR_TAG_ALT(LFSR_TAG_LE, LFSR_TAG_B, 0); + alt = LFSR_TAG_ALT(LFSR_TAG_B, LFSR_TAG_LE, 0); weight = 0; jump = 0; } @@ -3278,12 +3278,12 @@ stem:; && lfsr_tag_key(tag_) < lfsr_tag_key(tag)))))) { if (lfsr_tag_isrm(tag) || !lfsr_tag_key(tag)) { // if removed, make our tag unreachable - alt = LFSR_TAG_ALT(LFSR_TAG_GT, LFSR_TAG_B, lower_tag); + alt = LFSR_TAG_ALT(LFSR_TAG_B, LFSR_TAG_GT, lower_tag); weight = upper_rid - lower_rid + delta; upper_rid -= weight; } else { // split less than - alt = LFSR_TAG_ALT(LFSR_TAG_LE, LFSR_TAG_B, tag_); + alt = LFSR_TAG_ALT(LFSR_TAG_B, LFSR_TAG_LE, tag_); weight = upper_rid - lower_rid; lower_rid += weight; } @@ -3299,12 +3299,12 @@ stem:; && lfsr_tag_key(tag_) > lfsr_tag_key(tag)))))) { if (lfsr_tag_isrm(tag) || !lfsr_tag_key(tag)) { // if removed, make our tag unreachable - alt = LFSR_TAG_ALT(LFSR_TAG_GT, LFSR_TAG_B, lower_tag); + alt = LFSR_TAG_ALT(LFSR_TAG_B, LFSR_TAG_GT, lower_tag); weight = upper_rid - lower_rid + delta; upper_rid -= weight; } else { // split greater than - alt = LFSR_TAG_ALT(LFSR_TAG_GT, LFSR_TAG_B, tag); + alt = LFSR_TAG_ALT(LFSR_TAG_B, LFSR_TAG_GT, tag); weight = upper_rid - (rid+1); upper_rid -= weight; } @@ -3770,10 +3770,10 @@ static int lfsr_rbyd_appendcompaction(lfs_t *lfs, lfsr_rbyd_t *rbyd, // connect with an altle err = lfsr_rbyd_appendtag(lfs, rbyd, LFSR_TAG_ALT( - LFSR_TAG_LE, (i == 0 && off < layer_) ? LFSR_TAG_R : LFSR_TAG_B, + LFSR_TAG_LE, tag), weight, rbyd->eoff - trunk); diff --git a/scripts/dbgbmap.py b/scripts/dbgbmap.py index 116369dd..f3bdf7f4 100755 --- a/scripts/dbgbmap.py +++ b/scripts/dbgbmap.py @@ -44,8 +44,8 @@ TAG_SHRUB = 0x1000 TAG_CKSUM = 0x3000 TAG_ECKSUM = 0x3100 TAG_ALT = 0x4000 -TAG_GT = 0x2000 -TAG_R = 0x1000 +TAG_R = 0x2000 +TAG_GT = 0x1000 CHARS = 'mbd-' diff --git a/scripts/dbgbtree.py b/scripts/dbgbtree.py index 5743d045..2ee3b909 100755 --- a/scripts/dbgbtree.py +++ b/scripts/dbgbtree.py @@ -42,8 +42,8 @@ TAG_SHRUB = 0x1000 TAG_CKSUM = 0x3000 TAG_ECKSUM = 0x3100 TAG_ALT = 0x4000 -TAG_GT = 0x2000 -TAG_R = 0x1000 +TAG_R = 0x2000 +TAG_GT = 0x1000 # some ways of block geometry representations diff --git a/scripts/dbglfs.py b/scripts/dbglfs.py index 12d8a261..4fc6bf6f 100755 --- a/scripts/dbglfs.py +++ b/scripts/dbglfs.py @@ -43,8 +43,8 @@ TAG_SHRUB = 0x1000 TAG_CKSUM = 0x3000 TAG_ECKSUM = 0x3100 TAG_ALT = 0x4000 -TAG_GT = 0x2000 -TAG_R = 0x1000 +TAG_R = 0x2000 +TAG_GT = 0x1000 # some ways of block geometry representations diff --git a/scripts/dbgmtree.py b/scripts/dbgmtree.py index a8177691..589288ac 100755 --- a/scripts/dbgmtree.py +++ b/scripts/dbgmtree.py @@ -42,8 +42,8 @@ TAG_SHRUB = 0x1000 TAG_CKSUM = 0x3000 TAG_ECKSUM = 0x3100 TAG_ALT = 0x4000 -TAG_GT = 0x2000 -TAG_R = 0x1000 +TAG_R = 0x2000 +TAG_GT = 0x1000 # some ways of block geometry representations diff --git a/scripts/dbgrbyd.py b/scripts/dbgrbyd.py index 8fb3414c..eb858ff2 100755 --- a/scripts/dbgrbyd.py +++ b/scripts/dbgrbyd.py @@ -51,8 +51,8 @@ TAG_SHRUB = 0x1000 TAG_CKSUM = 0x3000 TAG_ECKSUM = 0x3100 TAG_ALT = 0x4000 -TAG_GT = 0x2000 -TAG_R = 0x1000 +TAG_R = 0x2000 +TAG_GT = 0x1000 # some ways of block geometry representations @@ -371,7 +371,7 @@ def dbg_log(data, block_size, rev, eoff, weight, *, wastrunk = True lower_, upper_ = 0, 0 - if (tag & 0xf000) == TAG_ALT: + if tag & TAG_ALT and not tag & TAG_GT: lower_ += w else: upper_ += w @@ -548,7 +548,7 @@ def dbg_log(data, block_size, rev, eoff, weight, *, wastrunk = True lower_, upper_ = 0, 0 - if (tag & 0xe000) == TAG_ALT: + if tag & TAG_ALT and not tag & TAG_GT: lower_ += w else: upper_ += w diff --git a/scripts/dbgtag.py b/scripts/dbgtag.py index ca78c121..cb870bc1 100755 --- a/scripts/dbgtag.py +++ b/scripts/dbgtag.py @@ -40,8 +40,8 @@ TAG_SHRUB = 0x1000 TAG_CKSUM = 0x3000 TAG_ECKSUM = 0x3100 TAG_ALT = 0x4000 -TAG_GT = 0x2000 -TAG_R = 0x1000 +TAG_R = 0x2000 +TAG_GT = 0x1000 # some ways of block geometry representations