-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc_gates.c
103 lines (91 loc) · 1.23 KB
/
func_gates.c
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
/**
* func_gates.c
* Copyright (C) 2012 Jan Viktorin
*/
#include "func.h"
#include "rndgen.h"
#include <stdlib.h>
size_t func_inputs_max(void)
{
return 2;
}
size_t func_outputs_max(void)
{
return 1;
}
size_t func_inputs(func_t f)
{
return 2;
}
size_t func_outputs(func_t f)
{
return 1;
}
/**
* Available functions.
*/
enum func_enum_t {
F_AND,
F_OR,
F_XOR,
F_NOR,
F_NAND,
F_NXOR
};
#define FUNC_COUNT 6
size_t func_count(void)
{
return FUNC_COUNT;
}
const char *func_to_str(func_t f)
{
switch((enum func_enum_t) f) {
case F_AND:
return "AND";
case F_OR:
return "OR";
case F_XOR:
return "XOR";
case F_NOR:
return "NOR";
case F_NAND:
return "NAND";
case F_NXOR:
return "NXOR";
default:
return "<?>";
}
}
void func_gen(func_t *f)
{
*f = rndgen_range(FUNC_COUNT - 1);
}
void func_mut(func_t *f)
{
func_gen(f);
}
void func_eval64(func_t f, uint64_t *op, uint64_t *dst)
{
switch((enum func_enum_t) f) {
case F_AND:
*dst = op[0] & op[1];
break;
case F_OR:
*dst = op[0] | op[1];
break;
case F_XOR:
*dst = op[0] ^ op[1];
break;
case F_NOR:
*dst = ~(op[0] | op[1]);
break;
case F_NAND:
*dst = ~(op[0] & op[1]);
break;
case F_NXOR:
*dst = ~(op[0] ^ op[1]);
break;
default:
abort();
}
}