File: tuning-common.c

package info (click to toggle)
gf2x 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 2,612 kB
  • ctags: 725
  • sloc: sh: 11,112; ansic: 7,549; cpp: 1,369; makefile: 490; perl: 151
file content (144 lines) | stat: -rw-r--r-- 3,557 bytes parent folder | download
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
/* This file is part of the gf2x library.

   Copyright 2007, 2008, 2009
   Richard Brent, Pierrick Gaudry, Emmanuel Thome', Paul Zimmermann

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   This program 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 General Public License for
   more details.

   You should have received a copy of the GNU General Public License along
   with this program; see the file COPYING.  If not, write to the Free
   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   02111-1307, USA.
*/

#define _BSD_SOURCE
#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "tuning-common.h"

double mulstep = 1;
FILE * rp;
const char * outfile = NULL;

void random_wordstring(unsigned long *a, long n)
{
    long i;
    for (i = 0; i < n; i++)
	a[i] = random();
}

void dump(const unsigned long *a, long m, const unsigned long *b, long n,
	  const unsigned long *c, const unsigned long *d)
{
    printf("failed:=[");
    printf("[");
    int j;
    for (j = 0; j < m; j++) {
	if (j)
	    printf(", ");
	printf("%lu", a[j]);
    }
    printf("],");
    printf("[");
    for (j = 0; j < n; j++) {
	if (j)
	    printf(", ");
	printf("%lu", b[j]);
    }
    printf("],");
    printf("[");
    for (j = 0; j < m + n; j++) {
	if (j)
	    printf(", ");
	printf("%lu", c[j]);
    }
    printf("],");
    printf("[");
    for (j = 0; j < m + n; j++) {
	if (j)
	    printf(", ");
	printf("%lu", d[j]);
    }
    printf("]");
    printf("];\n");
}

void check(const unsigned long *a, long m,
	   const unsigned long *b, long n,
	   const char * cname, const unsigned long *c,
           const char * dname, const unsigned long *d)
{
    long i = 0;
    for(i = 0 ; i < m + n ; i++) {
        if (c[i] != d[i]) {
            fprintf(stderr,
                    "Error: %s and %s differ for %ldx%ld at word %ld\n",
                    cname, dname, m, n, i);
            if (m + n < 1000) {
                dump(a, m, b, n, c, d);
            }
            abort();
        }
    }
}


void set_tuning_output()
{
    if (outfile) {
        if ((rp = fopen(outfile, "w")) == NULL) {
            fprintf(stderr, "fopen(%s): %s\n", outfile, strerror(errno));
            exit(1);
        }
    } else {
#if 0
        /* Not really useful since there is -o anyway */
        /* If file descriptor # 3 is open for writing, use it. */
        if ((rp = fdopen(3, "w")) == NULL) {
            rp = stdout;
        }
#else
        rp = stdout;
#endif
    }

    setbuf(rp, NULL);
    setbuf(stdout, NULL);
}

int handle_tuning_mulstep(int * p_argc, char *** p_argv)
{
    if (strcmp((*p_argv)[0], "--step") == 0 || strcmp((*p_argv)[0], "-s") == 0) {
        (*p_argc)--,(*p_argv)++;
        if (! (*p_argc)) {
            return -1;
        }
        mulstep = atof((*p_argv)[0]);
        return 1;
    }
    return 0;
}

int handle_tuning_outfile(int * p_argc, char *** p_argv)
{
    if (strcmp((*p_argv)[0], "--output") == 0 || strcmp((*p_argv)[0], "-o") == 0) {
        (*p_argc)--,(*p_argv)++;
        if (! (*p_argc)) {
            return -1;
        }
        outfile = (*p_argv)[0];
        return 1;
    }
    return 0;
}