Skip to content

Commit 0dd0569

Browse files
committed
Infrastructure for future runtime library integration
1 parent 455c19b commit 0dd0569

10 files changed

Lines changed: 689 additions & 111 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ Optional arguments:
139139
`__builtin_ternary_tb2t`, `__builtin_ternary_tt2b`, `__builtin_ternary_t2f`, and `__builtin_ternary_f2t`.
140140
- `-fplugin-arg-ternary_plugin-types` enables builtin ternary integer types `t32_t`, `t64_t`,
141141
`t128_t` with packed 2-bit trit storage.
142-
- `-fplugin-arg-ternary_plugin-prefix=<name>` sets the function name prefix used by `-lower`
143-
(default: `__ternary_select`).
142+
- `-fplugin-arg-ternary_plugin-prefix=<name>` sets the base helper prefix used by lowering
143+
(default: `__ternary`). For example, select helpers become `<prefix>_select_i32` and arithmetic
144+
helpers become `<prefix>_add`, `<prefix>_sub`, etc.
144145

145146
Example with trace/dumps enabled:
146147

include/ternary.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ extern int __builtin_ternary_tt2b(t32_t v);
4343
extern float __builtin_ternary_t2f(t32_t v);
4444
extern t32_t __builtin_ternary_f2t(float v);
4545

46+
// Ternary-specific comparison builtins (return ternary results)
47+
extern t32_t __builtin_ternary_cmplt(t32_t a, t32_t b);
48+
extern t32_t __builtin_ternary_cmpeq(t32_t a, t32_t b);
49+
extern t32_t __builtin_ternary_cmpgt(t32_t a, t32_t b);
50+
extern t32_t __builtin_ternary_cmpneq(t32_t a, t32_t b);
51+
extern t64_t __builtin_ternary_cmplt_t64(t64_t a, t64_t b);
52+
extern t64_t __builtin_ternary_cmpeq_t64(t64_t a, t64_t b);
53+
extern t64_t __builtin_ternary_cmpgt_t64(t64_t a, t64_t b);
54+
extern t64_t __builtin_ternary_cmpneq_t64(t64_t a, t64_t b);
55+
4656
// Vector builtins
4757
extern v2t32_t __builtin_ternary_add_v2t32(v2t32_t a, v2t32_t b);
4858
extern v4t64_t __builtin_ternary_mul_v4t64(v4t64_t a, v4t64_t b);

include/ternary_runtime.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ t32_t __ternary_f2t64_t32(double v);
8282
int __ternary_cmp_t32(t32_t a, t32_t b);
8383
t32_t __ternary_bt_str_t32(const char *s);
8484

85+
/* Ternary-specific comparison operations (return ternary results) */
86+
t32_t __ternary_cmplt_t32(t32_t a, t32_t b);
87+
t32_t __ternary_cmpeq_t32(t32_t a, t32_t b);
88+
t32_t __ternary_cmpgt_t32(t32_t a, t32_t b);
89+
t32_t __ternary_cmpneq_t32(t32_t a, t32_t b);
90+
8591
t64_t __ternary_add_t64(t64_t a, t64_t b);
8692
t64_t __ternary_mul_t64(t64_t a, t64_t b);
8793
t64_t __ternary_not_t64(t64_t a);
@@ -105,6 +111,12 @@ t64_t __ternary_f2t64_t64(double v);
105111
int __ternary_cmp_t64(t64_t a, t64_t b);
106112
t64_t __ternary_bt_str_t64(const char *s);
107113

114+
/* Ternary-specific comparison operations for t64 (return ternary results) */
115+
t64_t __ternary_cmplt_t64(t64_t a, t64_t b);
116+
t64_t __ternary_cmpeq_t64(t64_t a, t64_t b);
117+
t64_t __ternary_cmpgt_t64(t64_t a, t64_t b);
118+
t64_t __ternary_cmpneq_t64(t64_t a, t64_t b);
119+
108120
#ifdef __cplusplus
109121
}
110122
#endif

runtime/ternary_runtime.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ static int ternary_trit_max(int a, int b)
113113

114114
static int ternary_trit_xor(int a, int b)
115115
{
116-
int mn = ternary_trit_min(a, b);
117-
int mx = ternary_trit_max(a, b);
118-
return a + b - 2 * mn - 2 * mx;
116+
int sum = a + b;
117+
int mod = ((sum % 3) + 3) % 3;
118+
if (mod == 0) return 0;
119+
if (mod == 1) return 1;
120+
return -1;
119121
}
120122

121123
static int ternary_get_trit(uint64_t packed, unsigned idx)
@@ -660,6 +662,27 @@ int __ternary_ge(int a, int b)
660662
{ \
661663
int cmp = __ternary_cmp_t##SUFFIX(a, b); \
662664
return cmp == 1 || cmp == 0 ? 1 : 0; \
665+
} \
666+
/* Ternary-specific comparison operations returning ternary results */ \
667+
TYPE __ternary_cmplt_t##SUFFIX(TYPE a, TYPE b) \
668+
{ \
669+
int cmp = __ternary_cmp_t##SUFFIX(a, b); \
670+
return (TYPE)ENCODE(cmp == -1 ? -1 : 0, TRITS); \
671+
} \
672+
TYPE __ternary_cmpeq_t##SUFFIX(TYPE a, TYPE b) \
673+
{ \
674+
int cmp = __ternary_cmp_t##SUFFIX(a, b); \
675+
return (TYPE)ENCODE(cmp == 0 ? 1 : 0, TRITS); \
676+
} \
677+
TYPE __ternary_cmpgt_t##SUFFIX(TYPE a, TYPE b) \
678+
{ \
679+
int cmp = __ternary_cmp_t##SUFFIX(a, b); \
680+
return (TYPE)ENCODE(cmp == 1 ? 1 : 0, TRITS); \
681+
} \
682+
TYPE __ternary_cmpneq_t##SUFFIX(TYPE a, TYPE b) \
683+
{ \
684+
int cmp = __ternary_cmp_t##SUFFIX(a, b); \
685+
return (TYPE)ENCODE(cmp != 0 ? 1 : 0, TRITS); \
663686
}
664687

665688
DEFINE_TERNARY_TYPE_OPS(32, t32_t, 32, uint64_t, ternary_decode, ternary_encode,

runtime_skeleton/src/ternary_runtime_skeleton.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ static int ternary_trit_max(int a, int b)
195195

196196
static int ternary_trit_xor(int a, int b)
197197
{
198-
int mn = ternary_trit_min(a, b);
199-
int mx = ternary_trit_max(a, b);
200-
return a + b - 2 * mn - 2 * mx;
198+
int sum = a + b;
199+
int mod = ((sum % 3) + 3) % 3;
200+
if (mod == 0) return 0;
201+
if (mod == 1) return 1;
202+
return -1;
201203
}
202204

203205
static uint64_t ternary_tritwise_op_u64(uint64_t a, uint64_t b, unsigned trit_count, int op)

0 commit comments

Comments
 (0)