forked from JackHack96/logic-synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
118 lines (96 loc) · 2.09 KB
/
util.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Bill Lin
* University of California, Berkeley
* Comments to [email protected]
*
* Copyright (c) 1989 Bill Lin, UC Berkeley CAD Group
* Permission is granted to do anything with this
* code except sell it or remove this message.
*/
#include "jedi.h"
int distance(); /* forward declaration */
char *int_to_binary(); /* forward declaration */
int binary_to_int(); /* forward declaration */
int parse_line(); /* forward declaration */
FILE *my_fopen(); /* forward declaration */
int distance(code1, code2, width)
char *code1, *code2;
int width;
{
int dist;
int pdist;
int i;
dist = 0; pdist = 0;
for (i=0; i<width; i++) {
if (code1[i] != code2[i] && code1[i] != '-' && code2[i] != '-') {
dist++;
} else if ((code1[i] == '-' && code2[i] != '-') ||
(code1[i] != '-' && code2[i] == '-')) {
pdist++;
}
}
return (dist + pdist/2);
} /* end of distance */
char *int_to_binary(num, width)
int num, width;
{
char *buffer;
int i;
int j;
buffer = ALLOC(char, width+1);
j = num;
buffer[width] = '\0';
for (i=width-1; i>=0; i--) {
if (j & 1) {
buffer[i] = '1';
} else {
buffer[i] = '0';
}
j = j >> 1;
}
return buffer;
} /* end of int_to_binary */
int binary_to_int(binary, width)
char *binary;
int width;
{
int i, total;
int mask;
total = 0;
mask = 1;
for (i=width-1; i>=0; i--) {
if (binary[i] == '1') {
total = total | mask;
}
mask = mask << 1;
}
return total;
} /* end of binary_to_int */
parse_line(line)
register char *line;
{
register char **carg = targv;
register char ch;
targc = 0;
while (ch = *line++) {
if (ch <= ' ') continue;
targc++;
*carg++ = line-1;
while ((ch = *line) && ch > ' ' ) line++;
if (ch) *line++ = '\0';
}
*carg = 0;
} /* end of parse_line */
FILE *my_fopen(fname, mode)
char *fname;
char *mode;
{
FILE *fp;
if ((fp = fopen(fname, mode)) == NULL) {
(void) fprintf(stderr,
"error: couldn't open file %s under mode (%s)\n",
fname, mode);
exit(1);
}
return fp;
} /* end of my_fopen */