From 10b9bb95111b90ec08dc4182341b5cc6c9065b22 Mon Sep 17 00:00:00 2001 From: d_m Date: Thu, 9 Nov 2023 21:51:44 -0500 Subject: [PATCH] add more bc15 functions --- src/bcb.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/bcb.h | 4 ++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/bcb.c b/src/bcb.c index 822b8df..ee84398 100644 --- a/src/bcb.c +++ b/src/bcb.c @@ -142,18 +142,73 @@ bc15int(bc15 t) 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 && (carry != 0 || (a >> i) != 0 || (b >> i) != 0); i += 2) { + 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; } diff --git a/src/bcb.h b/src/bcb.h index 0360985..fea1b32 100644 --- a/src/bcb.h +++ b/src/bcb.h @@ -47,6 +47,10 @@ 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); -- 2.45.2