summaryrefslogtreecommitdiff
path: root/include/util.h
diff options
context:
space:
mode:
authorWai-Hong Tam <waihong@google.com>2021-06-15 15:43:06 -0700
committerCommit Bot <commit-bot@chromium.org>2021-06-16 04:30:15 +0000
commit504216c9b3df0af440ce86571136bb7dc005fc02 (patch)
treec0e1400644b9005a4593ac73f42dff99ad8fee3b /include/util.h
parentce149d30f144c952fe4ebf617b1e30ed0b0b3c8e (diff)
downloadchrome-ec-504216c9b3df0af440ce86571136bb7dc005fc02.tar.gz
util: Add function to convert binary first base3 number
Add an util function to convert a ternary bit array (each element is either 0, 1, or 2) to a non-standard ternary number system where the first 2^n natural numbers are represented as they would be in a binary system (without any Z digits) and the following 3^n-2^n numbers use the remaining ternary representations in the normal ternary system order (skipping the values that were already used up). This function is useful for converting BOARd ID, which is initially used a binary and later decided to switch to tri-state after some revisions have already been built. BRANCH=Trogdor BUG=b:190250108 TEST=make runhosttests Change-Id: I853a04f3b28eb54c61855251dc5232c7e6994fef Signed-off-by: Wai-Hong Tam <waihong@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2964389 Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'include/util.h')
-rw-r--r--include/util.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/include/util.h b/include/util.h
index a25bbbd77a..21603484bb 100644
--- a/include/util.h
+++ b/include/util.h
@@ -328,6 +328,37 @@ static inline uint64_t mulaa32(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
*/
void wait_for_ready(volatile uint32_t *reg, uint32_t enable, uint32_t ready);
+/**
+ * Convert the ternary bit array (each element is either 0, 1, or 2) to a
+ * non-standard ternary number system where the first 2^n natural numbers are
+ * represented as they would be in a binary system (without any Z digits) and
+ * the following 3^n-2^n numbers use the remaining ternary representations in
+ * the normal ternary system order (skipping the values that were already used
+ * up).
+ *
+ * This function is useful for converting BOARd ID, which is initially used a
+ * binary and later decided to switch to tri-state after some revisions have
+ * already been built.
+ *
+ * Example: For nbits = 2 we get the following representation:
+ *
+ * Number X1 X0
+ * 0 0 0
+ * 1 0 1
+ * 2 1 0
+ * 3 1 1 // Start counting ternaries back at 0 after this
+ * 4 0 2 // Skipping 00 and 01 which are already used up
+ * 5 1 2 // Skipping 10 and 11 which are already used up
+ * 6 2 0
+ * 7 2 1
+ * 8 2 2
+ *
+ * @param bits Array of ternary bits (LSB first).
+ * @param nbits Total number of bits.
+ * @return Number in the binary-first ternary number system.
+ */
+int binary_first_base3_from_bits(int *bits, int nbits);
+
#ifdef __cplusplus
}
#endif