-
Notifications
You must be signed in to change notification settings - Fork 2
/
clean.c
152 lines (135 loc) · 4.11 KB
/
clean.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
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
/*
* =====================================================================================
*
* Filename: compare.c
*
* Description: Example code t
*
* Version: 1.0
* Created: 09/16/2015 09:23:33 AM CDT
* Revision: none
* Compiler: gcc
*
* Author: Leonardo A. Bautista Gomez ([email protected]),
* Company: Argonne National Laboratory
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>
#include <time.h>
#include "lz.h"
float reverseFloat( const float inFloat )
{
float retVal;
char *floatToConvert = ( char* ) & inFloat;
char *returnFloat = ( char* ) & retVal;
returnFloat[0] = floatToConvert[3];
returnFloat[1] = floatToConvert[2];
returnFloat[2] = floatToConvert[1];
returnFloat[3] = floatToConvert[0];
return retVal;
}
double reverseDouble( const float inDouble )
{
double retVal;
char *doubleToConvert = ( char* ) & inDouble;
char *returnDouble = ( char* ) & retVal;
returnDouble[0] = doubleToConvert[7];
returnDouble[1] = doubleToConvert[6];
returnDouble[2] = doubleToConvert[5];
returnDouble[3] = doubleToConvert[4];
returnDouble[4] = doubleToConvert[3];
returnDouble[5] = doubleToConvert[2];
returnDouble[6] = doubleToConvert[1];
returnDouble[7] = doubleToConvert[0];
return retVal;
}
int cleanFile(char *pSrc1Fn, double min, double max, int prec, int swap)
{
double mi = max, ma = min;
char pSrc2Fn[64];
ulong size1;
FILE *pFile1 = fopen(pSrc1Fn, "rb");
if (pFile1 == NULL)
{
printf("Failed to open input file.");
return EXIT_FAILURE;
}
fseek(pFile1, 0, SEEK_END);
size1 = ftell(pFile1)/prec;
fseek(pFile1, 0, SEEK_SET);
sprintf(pSrc2Fn, "%s.cln", pSrc1Fn);
FILE *pFile2 = fopen(pSrc2Fn, "w");
if (pFile2 == NULL)
{
printf("Failed to open input file.");
return EXIT_FAILURE;
}
while (size1 > 0)
{
if (prec == 4)
{
float buf;
fread(&buf, sizeof(float), 1, pFile1);
if (swap) buf = reverseFloat(buf);
if (buf >= (float)min && buf <= (float)max)
{
if (buf > ma) ma = buf;
if (buf < mi) mi = buf;
fwrite(&buf, sizeof(float), 1, pFile2);
//printf("%f \n", buf);
}
} else {
double buf;
fread(&buf, sizeof(double), 1, pFile1);
if (swap) buf = reverseDouble(buf);
if (buf >= min && buf <= max)
{
if (buf > ma) ma = buf;
if (buf < mi) mi = buf;
fwrite(&buf, sizeof(double), 1, pFile2);
//printf("%f \n", buf);
}
}
size1 = size1 - 1;
}
printf("All values between %f and %f\n", mi, ma);
fclose(pFile1);
fclose(pFile2);
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
int swap, res, prec = sizeof(double);
double min, max;
char pSrcFn[64];
if (argc == 6) {
sprintf(pSrcFn, "%s", argv[1]);
min = atoi(argv[2]);
max = atoi(argv[3]);
prec = atoi(argv[4]);
swap = atoi(argv[5]);
if ((prec != 32) && (prec != 64))
{
printf("Precision needs to be equal to 32 or 64\n");
return EXIT_FAILURE;
}
if ((swap != 0) && (swap != 1))
{
printf("Swap needs to be equal to 0 or 1\n");
return EXIT_FAILURE;
}
prec = prec/8;
} else {
printf("Usage: \n");
printf(" ./clean filename1 Min Max precision(32 or 64) swap_endianness(1 or 0))\n");
return EXIT_FAILURE;
}
printf("Files %s in %d bits precision.\n", pSrcFn, prec*8);
res = cleanFile(pSrcFn, min, max, prec, swap);
if (res == EXIT_FAILURE) return EXIT_FAILURE;
return EXIT_SUCCESS;
}