-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathternary.h
111 lines (96 loc) · 4.62 KB
/
ternary.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/****************************************************************************
* Copyright (C) 2020 by Ted Kotz *
* *
* This file is part of TernCpuEmu. *
* *
* TernCpuEmu is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* TernCpuEmu is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with TernCpuEmu. If not, see *
* <http://www.gnu.org/licenses/>. *
****************************************************************************/
/**
* @file ternary.h
* @author Ted Kotz <[email protected]>
* @version 0.1
*
* [Description]
*
*/
#ifndef TERNARY_MATH_H
#define TERNARY_MATH_H
/* Includes ******************************************************************/
#include <inttypes.h>
/* Defines *******************************************************************/
#define BITS_PER_TRIT 2
#define N 3
#define TRITS_PER_TRINT32_T 32
#define TRITS_PER_TRINT16_T 16
#define BITS_PER_TRINT32_T ( TRITS_PER_TRINT32_T * BITS_PER_TRIT)
#define BITS_PER_TRINT16_T ( TRITS_PER_TRINT16_T * BITS_PER_TRIT)
#define TRINT32_MASK ((1ULL << BITS_PER_TRINT32_T) - 1)
#define TRINT16_MASK ((1ULL << BITS_PER_TRINT16_T) - 1)
#define TRIT_MASK (3)
/* Types *********************************************************************/
typedef uint64_t trint32_t;
typedef uint32_t trint16_t;
typedef uint_fast8_t trit_t;
/* Interfaces ****************************************************************/
/* Data **********************************************************************/
/* Functions *****************************************************************/
/**
* [Description]
*
* @param
* @return
*/
extern int trit2int( trit_t x );
extern trit_t int2trit( int x );
extern trit_t char2trit ( char c );
extern int64_t ternary2int( trint32_t x );
//extern trint32_t int2ternary( int64_t x, int lvl );
/**
* Prints a ternary number
*
* @param x The trinary value to print
* @param width the number of least significant trits to print, 0 drops leading 0s
*/
extern void ternaryPrint( trint32_t x, int_fast8_t width);
extern void ternaryScan( trint32_t* x );
// BITWISE 1 OP
extern trint32_t apply_tritwise_gate1 ( uint_fast8_t gate, trint32_t op1 );
extern trint32_t ternaryINCB( trint32_t op1 );
extern trint32_t ternaryDECB( trint32_t op1 );
extern trint32_t ternaryNEGB( trint32_t op1 );
extern trint32_t ternaryFLTB( trint32_t op1 );
extern trint32_t ternaryABSB( trint32_t op1 );
// BITWISE 2 OPS
extern trint32_t ternaryADDB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryMULB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryANDB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryORRB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryMINB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryMAXB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternarySETB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryIMPB ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryUNMB ( trint32_t op1, trint32_t op2 );
// ARITHMETIC 1 OP
extern trint32_t ternarySGN ( trint32_t op1 );
// ARITHMETIC 2 OPS
extern trint32_t ternarySHL ( trint32_t op1, trint32_t op2 );
extern void ternaryROL ( trint32_t* op1, trint32_t* op2, trint32_t rotate, int triwordsize );
//extern trint32_t ternaryRCL ( trint32_t op1, trint32_t op2, trit_t* carry );
extern trint32_t ternaryADD ( trint32_t op1, trint32_t op2 );
extern trint32_t ternaryADC ( trint32_t op1, trint32_t op2, trit_t* carry );
extern trint32_t ternaryMUL ( trint32_t op1, trint32_t op2 );
//extern trint32_t ternaryDIVMOD ( trint32_t op1, trint32_t op2 );
#endif /* TERNARY_MATH_H */
/*****************************************************************************/