From 3dca27810e4f3b59ed83b46cf5bfe40f9c1523fb Mon Sep 17 00:00:00 2001 From: neauoire Date: Thu, 9 Nov 2023 19:28:09 -0800 Subject: [PATCH] Migrating functions from bcb to tern --- src/tern.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tern.h | 18 ++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/tern.c b/src/tern.c index 02b2bc6..4d056de 100644 --- a/src/tern.c +++ b/src/tern.c @@ -22,6 +22,13 @@ pbin(uc15 t, int len) } } +void +pbal(bc15 t, int len) +{ + for(int i = (len << 1) - 2; i > -1; i -= 2) + putchar(bc1tri(t >> i)); +} + void phep(uc15 t, int len) { @@ -32,6 +39,79 @@ phep(uc15 t, int len) } } +// 1 Trit + +char +bc1tri(bc3 t) +{ + return "0+?-"[t & 0x3]; +} + +bc3 +tribc1(char c) +{ + switch(c) { + case '+': return 0x1; + case '0': return 0x0; + case '-': return 0x3; + } + return 0x2; +} + +int +bc1uns(bc3 t) +{ + return (t + 1) & 0x3; +} + +bc3 +unsbc1(int n) +{ + return (n - 1) & 0x3; +} + +void +pbc1(bc3 t) +{ + pbin(t, 1); +} + +// 3 Trits + +bc3 +intbc3(int n) +{ + n += 13; + return (unsbc1((n / 9) % 3) << 4) | (unsbc1((n / 3) % 3) << 2) | unsbc1(n % 3); +} + +int +bc3int(bc3 t) +{ + return (bc1uns(t >> 4) * 9 + bc1uns(t >> 2) * 3 + bc1uns(t)) - 13; +} + +int +triint(char c) +{ + switch(c) { + case '+': return 1; + case '0': return 0; + case '-': return -1; + } + return 0x2; +} + +char +inttri(int n) +{ + return "-0+?"[(n + 1) & 0x3]; +} + + + + + uc15 binuc15(int n) { diff --git a/src/tern.h b/src/tern.h index 30d2069..a1262c0 100644 --- a/src/tern.h +++ b/src/tern.h @@ -11,12 +11,30 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ +typedef uint32_t bc15; typedef uint32_t uc15; +typedef uint_fast8_t bc3; typedef uint_fast8_t uc3; +// Printing void pbin(uc15 n, int len); +void pbal(bc15 t, int len); void phep(uc15 n, int len); +// 1 Trit +char bc1tri(bc3 t); +bc3 tribc1(char c); +int bc1uns(bc3 t); +bc3 unsbc1(int n); +void pbc1(bc3 t); + +// 3 Trits +bc3 intbc3(int n); +int bc3int(bc3 t); +int triint(char c); +char inttri(int n); +void pbc3(bc3 t); + int uc15bin(uc15 t); uc15 binuc15(int n); uc15 hepuc15(char *str); -- 2.45.2