From bfe58ba0b94542754e997d7c35fac411f035608d Mon Sep 17 00:00:00 2001 From: neauoire Date: Thu, 9 Nov 2023 19:37:49 -0800 Subject: [PATCH] Merged BCB into tern --- build.sh | 4 +- src/base3.c | 3 +- src/bcb.c | 285 ---------------------------------------------------- src/bcb.h | 65 ------------ src/tern.c | 166 ++++++++++++++++++++++++++++++ src/tern.h | 19 ++++ 6 files changed, 188 insertions(+), 354 deletions(-) delete mode 100644 src/bcb.c delete mode 100644 src/bcb.h diff --git a/build.sh b/build.sh index e471b6b..a3486b7 100755 --- a/build.sh +++ b/build.sh @@ -10,8 +10,8 @@ fi rm -f bin/* mkdir -p bin -# cc ${DEBUG_FLAGS} src/base3.c src/bcb.c -o bin/base3 -# ./bin/base3 +cc ${DEBUG_FLAGS} src/base3.c src/tern.c -o bin/base3 +./bin/base3 cc ${DEBUG_FLAGS} src/gyoasm.c src/tern.c -o bin/gyoasm cc ${DEBUG_FLAGS} src/gyodis.c src/tern.c -o bin/gyodis diff --git a/src/base3.c b/src/base3.c index 794a00c..f19663e 100644 --- a/src/base3.c +++ b/src/base3.c @@ -1,5 +1,5 @@ #include -#include "bcb.h" +#include "tern.h" /* Copyright (c) 2023 Devine Lu Linvega @@ -92,6 +92,5 @@ main(int argc, char **argv) } } printf(" %d of %d test cases passed\n", passed, total); - return 0; } diff --git a/src/bcb.c b/src/bcb.c deleted file mode 100644 index fdb8ecd..0000000 --- a/src/bcb.c +++ /dev/null @@ -1,285 +0,0 @@ -#include -#include -#include "bcb.h" - -/* -Copyright (c) 2023 Devine Lu Linvega - -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE. -*/ - -void -pbin(bc15 n, int len) -{ - for(int i = (len << 1) - 1; i > -1; --i) { - putchar('0' + ((n >> i) & 0x1)); - if(!(i % 2) && i) putchar(' '); - } -} - -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) -{ - for(int i = (len - 1); i > -1; --i) { - int x = uc15int((t >> (i * 6)) & 0x3f); - putchar(x ? '@' + x : '0'); - } -} - -// 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]; -} - -// 15 Trits - -bc15 -intbc15(int n) -{ - int i, divisor = 4782969, acc = 0; - for(i = 0; i < 15; i++) { - acc = acc << 2; - int limit = (divisor - 1) >> 1; - if(n > limit) { - acc |= 0x1; - n -= divisor; - } else if(n < -limit) { - acc |= 0x3; - n += divisor; - } - divisor /= 3; - } - return acc; -} - -int -bc15int(bc15 t) -{ - int digit = 1; - int acc = 0; - for(int i = 0; i < 30; i += 2) { - int p = (t >> i) & 0x1; - int n = (t >> (i + 1)) & 0x1; - acc += digit * (p - 2 * n); - digit *= 3; - } - return acc; -} - -bc3 -bc15sign(bc15 a) -{ - int i; - for(i = 28; i >= 0; i -= 2) { - bc3 x = (a >> i) & 0x3; - if (x != 0) return x; - } - return 0; -} - -bc15 -bc15tnand(bc15 a, bc15 b) -{ - bc15 acc = 0; - int i; - for(i = 0; i < 30; i += 2) { - int x = (a >> i) & 0x3; - int y = (b >> i) & 0x3; - int digit = x == y ? (4 - x) & 0x3 : (x | y) >> 1; - acc |= (digit << i); - } - return acc; -} - -bc15 -bc15tnor(bc15 a, bc15 b) -{ - bc15 acc = 0; - int i, digit; - for(i = 0; i < 30; i += 2) { - int x = (a >> i) & 0x3; - int y = (b >> i) & 0x3; - if (x == y) { - digit = (4 - x) & 0x3; - } else { - int t = ((x ^ y) + 1) & 0x3; - digit = t | (t>>1); - } - acc |= (digit << i); - } - return acc; -} - -bc15 -bc15negate(bc15 a) -{ - bc15 acc = 0; - for(int i = 0; i < 30; i += 2) { - int x = (a >> i) & 0x3; - int nx = (4 - digit) & 0x3; - acc |= (nx << i); - } - return acc; -} - -bc15 -bc15add(bc15 a, bc15 b) -{ - bc15 acc = 0; - int i, carry = 0; - for(i = 0; i < 30; i += 2) { - int digit = carry; - carry = 0; - bc15carry((a >> i) & 0x3, &digit, &carry); - bc15carry((b >> i) & 0x3, &digit, &carry); - acc |= (digit << i); - } - return acc; -} - -void -bc15carry(int next, int *digit, int *carry) -{ - int sum = *digit + next; - int part = sum & 0x3; - if(part == 2) { - *carry = (*carry + (sum >> 1)) & 0x3; - *digit = (sum ^ 4) >> 1; - } else { - *digit = part; - } -} - -bc3 -bc15cmp(bc15 a, bc15 b) -{ - int i; - for(i = 28; i >= 0; i -= 2) { - /* altered repr: 0 (-) 1 (zero) 2 (+) */ - bc3 x = ((a >> i) + 1) & 0x3; - bc3 y = ((b >> i) + 1) & 0x3; - if (x != y) - return x < y ? 3 : 1; - } - return 0; -} - -// Unsigned(TODO: migrate to 11-00-01 encoding) - -int -uc15int(uc15 t) -{ - int sft = 1, acc = 0; - while(t) - acc += sft * (t & 0x3), t >>= 2, sft *= 3; - return acc; -} - -uc15 -intuc15(int t) -{ - uc15 sft = 0, acc = 0; - while(t > 0) - acc |= (t % 3) << sft, t /= 3, sft += 2; - return acc; -} - -// Heptavintimal - -char -bc3hep(bc3 t) -{ - return t == 0x3f ? '0' : 'A' + bc3int(t) + 12; -} - -bc3 -hepbc3(char c) -{ - return c == '0' ? 0x3f : intbc3(c - 'A' - 12); -} - -uc15 -hepuc15(char *str) -{ - char c; - uc15 acc = 0; - while((c = *str++)) - acc <<= 6, acc |= intuc15(c ? (c - 'A') + 1 : 0); - return acc; -} diff --git a/src/bcb.h b/src/bcb.h deleted file mode 100644 index 46c4555..0000000 --- a/src/bcb.h +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include - -/* -Copyright (c) 2023 Devine Lu Linvega - -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE. -*/ - -typedef uint_fast8_t bc3; -typedef uint32_t bc15; -typedef uint32_t uc15; - -/* - tri '+' '0' '-' - bc1 0b01 0b00 0b11 - int 1 0 -1 - - try "+++" "000" "---" - bc3 0b010101 0b000000 0b111111 - int 13 0 -13 -*/ - -void pbin(bc15 t, int len); -void pbal(bc15 t, int len); -void phep(uc15 t, 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); - -// 15 Trits -bc15 intbc15(int n); -int bc15int(bc15 t); -bc3 bc15sign(bc15 a); -bc15 bc15tnand(bc15 a); -bc15 bc15tnor(bc15 a); -bc15 bc15negate(bc15 a); -bc15 bc15add(bc15 a, bc15 b); -void bc15carry(int sum, int *digit, int *carry); -bc15 bc15cmp(bc15 a, bc15 b); - -// Unsigned -int uc15int(uc15 t); -uc15 intuc15(int n); - -// Heptavintimal -char bc3hep(bc3 t); -bc3 hepbc3(char c); -uc15 hepuc15(char *str); diff --git a/src/tern.c b/src/tern.c index 4d056de..20b0f43 100644 --- a/src/tern.c +++ b/src/tern.c @@ -108,6 +108,172 @@ inttri(int n) return "-0+?"[(n + 1) & 0x3]; } +// 15 Trits + +bc15 +intbc15(int n) +{ + int i, divisor = 4782969, acc = 0; + for(i = 0; i < 15; i++) { + acc = acc << 2; + int limit = (divisor - 1) >> 1; + if(n > limit) { + acc |= 0x1; + n -= divisor; + } else if(n < -limit) { + acc |= 0x3; + n += divisor; + } + divisor /= 3; + } + return acc; +} + +int +bc15int(bc15 t) +{ + int digit = 1; + int acc = 0; + for(int i = 0; i < 30; i += 2) { + int p = (t >> i) & 0x1; + int n = (t >> (i + 1)) & 0x1; + acc += digit * (p - 2 * n); + digit *= 3; + } + return acc; +} + +bc3 +bc15sign(bc15 a) +{ + int i; + for(i = 28; i >= 0; i -= 2) { + bc3 x = (a >> i) & 0x3; + if (x != 0) return x; + } + return 0; +} + +bc15 +bc15tnand(bc15 a, bc15 b) +{ + bc15 acc = 0; + int i; + for(i = 0; i < 30; i += 2) { + int x = (a >> i) & 0x3; + int y = (b >> i) & 0x3; + int digit = x == y ? (4 - x) & 0x3 : (x | y) >> 1; + acc |= (digit << i); + } + return acc; +} + +bc15 +bc15tnor(bc15 a, bc15 b) +{ + bc15 acc = 0; + int i, digit; + for(i = 0; i < 30; i += 2) { + int x = (a >> i) & 0x3; + int y = (b >> i) & 0x3; + if (x == y) { + digit = (4 - x) & 0x3; + } else { + int t = ((x ^ y) + 1) & 0x3; + digit = t | (t>>1); + } + acc |= (digit << i); + } + return acc; +} + +bc15 +bc15negate(bc15 a) +{ + bc15 acc = 0; + for(int i = 0; i < 30; i += 2) { + int x = (a >> i) & 0x3; + int nx = (4 - a) & 0x3; + acc |= (nx << i); + } + return acc; +} + +bc15 +bc15add(bc15 a, bc15 b) +{ + bc15 acc = 0; + int i, carry = 0; + for(i = 0; i < 30; i += 2) { + int digit = carry; + carry = 0; + bc15carry((a >> i) & 0x3, &digit, &carry); + bc15carry((b >> i) & 0x3, &digit, &carry); + acc |= (digit << i); + } + return acc; +} + +void +bc15carry(int next, int *digit, int *carry) +{ + int sum = *digit + next; + int part = sum & 0x3; + if(part == 2) { + *carry = (*carry + (sum >> 1)) & 0x3; + *digit = (sum ^ 4) >> 1; + } else { + *digit = part; + } +} + +bc3 +bc15cmp(bc15 a, bc15 b) +{ + int i; + for(i = 28; i >= 0; i -= 2) { + /* altered repr: 0 (-) 1 (zero) 2 (+) */ + bc3 x = ((a >> i) + 1) & 0x3; + bc3 y = ((b >> i) + 1) & 0x3; + if (x != y) + return x < y ? 3 : 1; + } + return 0; +} +// Unsigned + +int +uc15int(uc15 t) +{ + int sft = 1, acc = 0; + while(t) + acc += sft * (t & 0x3), t >>= 2, sft *= 3; + return acc; +} + +uc15 +intuc15(int t) +{ + uc15 sft = 0, acc = 0; + while(t > 0) + acc |= (t % 3) << sft, t /= 3, sft += 2; + return acc; +} + +// Heptavintimal + +char +bc3hep(bc3 t) +{ + return t == 0x3f ? '0' : 'A' + bc3int(t) + 12; +} + +bc3 +hepbc3(char c) +{ + return c == '0' ? 0x3f : intbc3(c - 'A' - 12); +} + diff --git a/src/tern.h b/src/tern.h index a1262c0..c687e9c 100644 --- a/src/tern.h +++ b/src/tern.h @@ -35,6 +35,25 @@ int triint(char c); char inttri(int n); void pbc3(bc3 t); +// 15 Trits +bc15 intbc15(int n); +int bc15int(bc15 t); +bc3 bc15sign(bc15 a); +bc15 bc15tnand(bc15 a, bc15 b); +bc15 bc15tnor(bc15 a, bc15 b); +bc15 bc15negate(bc15 a); +bc15 bc15add(bc15 a, bc15 b); +void bc15carry(int sum, int *digit, int *carry); +bc3 bc15cmp(bc15 a, bc15 b); + +// Unsigned +int uc15int(uc15 t); +uc15 intuc15(int n); + +// Heptavintimal +char bc3hep(bc3 t); +bc3 hepbc3(char c); + int uc15bin(uc15 t); uc15 binuc15(int n); uc15 hepuc15(char *str); -- 2.45.2