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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/* $NetBSD: graph2.c,v 1.6 2021/01/28 18:52:43 joerg Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Joerg Sonnenberger.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
__RCSID("$NetBSD: graph2.c,v 1.6 2021/01/28 18:52:43 joerg Exp $");
#include <err.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nbperf.h"
#include "graph2.h"
void
SIZED2(_setup)(struct SIZED(graph) *graph, uint32_t v, uint32_t e)
{
graph->v = v;
graph->e = e;
graph->verts = calloc(sizeof(*graph->verts), v);
graph->edges = calloc(sizeof(*graph->edges), e);
graph->output_order = calloc(sizeof(uint32_t), e);
if (graph->verts == NULL || graph->edges == NULL ||
graph->output_order == NULL)
err(1, "malloc failed");
}
void
SIZED2(_free)(struct SIZED(graph) *graph)
{
free(graph->verts);
free(graph->edges);
free(graph->output_order);
graph->verts = NULL;
graph->edges = NULL;
graph->output_order = NULL;
}
static struct nbperf *sorting_nbperf;
static struct SIZED(graph) *sorting_graph;
static int sorting_found;
static int sorting_cmp(const void *a_, const void *b_)
{
const uint32_t *a = a_, *b = b_;
int i;
const struct SIZED(edge) *ea = &sorting_graph->edges[*a],
*eb = &sorting_graph->edges[*b];
for (i = 0; i < GRAPH_SIZE; ++i) {
if (ea->vertices[i] < eb->vertices[i])
return -1;
if (ea->vertices[i] > eb->vertices[i])
return 1;
}
if (sorting_nbperf->keylens[*a] < sorting_nbperf->keylens[*b])
return -1;
if (sorting_nbperf->keylens[*a] > sorting_nbperf->keylens[*b])
return 1;
i = memcmp(sorting_nbperf->keys[*a], sorting_nbperf->keys[*b],
sorting_nbperf->keylens[*a]);
if (i == 0)
sorting_found = 1;
return i;
}
static int
SIZED2(_check_duplicates)(struct nbperf *nbperf, struct SIZED(graph) *graph)
{
size_t i;
uint32_t *key_index = calloc(sizeof(*key_index), graph->e);
if (key_index == NULL)
err(1, "malloc failed");
for (i = 0; i < graph->e; ++i)
key_index[i] = i;
sorting_nbperf = nbperf;
sorting_graph = graph;
sorting_found = 0;
qsort(key_index, graph->e, sizeof(*key_index), sorting_cmp);
if (sorting_found)
goto found_dups;
/*
* Any duplicate must have been found as part of the qsort,
* as it can't sort consecutive elements in the output without
* comparing them at least once.
*/
free(key_index);
return 0;
found_dups:
nbperf->has_duplicates = 1;
return -1;
}
static inline void
SIZED2(_add_edge)(struct SIZED(graph) *graph, uint32_t edge)
{
struct SIZED(edge) *e = graph->edges + edge;
struct SIZED(vertex) *v;
size_t i;
for (i = 0; i < GRAPH_SIZE; ++i) {
v = graph->verts + e->vertices[i];
v->edges ^= edge;
++v->degree;
}
}
static inline void
SIZED2(_remove_edge)(struct SIZED(graph) *graph, uint32_t edge)
{
struct SIZED(edge) *e = graph->edges + edge;
struct SIZED(vertex) *v;
size_t i;
for (i = 0; i < GRAPH_SIZE; ++i) {
v = graph->verts + e->vertices[i];
v->edges ^= edge;
--v->degree;
}
}
static inline void
SIZED2(_remove_vertex)(struct SIZED(graph) *graph, uint32_t vertex)
{
struct SIZED(vertex) *v = graph->verts + vertex;
uint32_t e;
if (v->degree == 1) {
e = v->edges;
graph->output_order[--graph->output_index] = e;
SIZED2(_remove_edge)(graph, e);
}
}
int
SIZED2(_hash)(struct nbperf *nbperf, struct SIZED(graph) *graph)
{
struct SIZED(edge) *e;
uint32_t hashes[NBPERF_MAX_HASH_SIZE];
size_t i, j;
#if GRAPH_SIZE == 2
if (nbperf->allow_hash_fudging && (graph->v & 1) != 0)
errx(1, "vertex count must have lowest bit clear");
#else
if (nbperf->allow_hash_fudging && (graph->v & 3) != 0)
errx(1, "vertex count must have lowest 2 bits clear");
#endif
memset(graph->verts, 0, sizeof(*graph->verts) * graph->v);
graph->hash_fudge = 0;
for (i = 0; i < graph->e; ++i) {
(*nbperf->compute_hash)(nbperf,
nbperf->keys[i], nbperf->keylens[i], hashes);
e = graph->edges + i;
for (j = 0; j < GRAPH_SIZE; ++j) {
e->vertices[j] = hashes[j] % graph->v;
if (j == 1 && e->vertices[0] == e->vertices[1]) {
if (!nbperf->allow_hash_fudging)
return -1;
e->vertices[1] ^= 1; /* toogle bit to differ */
graph->hash_fudge |= 1;
}
#if GRAPH_SIZE == 3
if (j == 2 && (e->vertices[0] == e->vertices[2] ||
e->vertices[1] == e->vertices[2])) {
if (!nbperf->allow_hash_fudging)
return -1;
graph->hash_fudge |= 2;
e->vertices[2] ^= 1;
e->vertices[2] ^= 2 * (e->vertices[0] == e->vertices[2] ||
e->vertices[1] == e->vertices[2]);
}
#endif
}
}
for (i = 0; i < graph->e; ++i)
SIZED2(_add_edge)(graph, i);
if (nbperf->check_duplicates) {
nbperf->check_duplicates = 0;
return SIZED2(_check_duplicates)(nbperf, graph);
}
return 0;
}
int
SIZED2(_output_order)(struct SIZED(graph) *graph)
{
size_t i, j;
struct SIZED(edge) *e2;
graph->output_index = graph->e;
for (i = 0; i < graph->v; ++i)
SIZED2(_remove_vertex)(graph, i);
for (i = graph->e; graph->output_index > 0 && i > graph->output_index;) {
e2 = graph->edges + graph->output_order[--i];
for (j = 0; j < GRAPH_SIZE; ++j)
SIZED2(_remove_vertex)(graph, e2->vertices[j]);
}
if (graph->output_index != 0) {
return -1;
}
return 0;
}
|