@@ -113,9 +113,11 @@ static int ternary_trit_max(int a, int b)
113113
114114static 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
121123static 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
665688DEFINE_TERNARY_TYPE_OPS (32 , t32_t , 32 , uint64_t , ternary_decode , ternary_encode ,
0 commit comments