-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.c
168 lines (146 loc) · 3.43 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
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
/**
* Utility functions
*
* @author Connor Imes <[email protected]>
* @date 2019-07-15
*/
#include <complex.h>
#include <errno.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
float rand_flt(void)
{
// random number in range [-0.5, 0.5] - this is what FFTW's benchfft does
float d = rand();
return (d / (float) RAND_MAX) - 0.5;
}
double rand_dbl(void)
{
// random number in range [-0.5, 0.5] - this is what FFTW's benchfft does
double d = rand();
return (d / (double) RAND_MAX) - 0.5;
}
float complex rand_fcmplx(void)
{
return CMPLXF(rand_flt(), rand_flt());
}
double complex rand_dcmplx(void)
{
return CMPLX(rand_dbl(), rand_dbl());
}
#define FILL_RAND(a, len, fn_rand) \
{ \
size_t i; \
for (i = 0; i < len; i++) \
a[i] = fn_rand(); \
}
void fill_rand_flt(float *a, size_t len)
{
FILL_RAND(a, len, rand_flt);
}
void fill_rand_dbl(double *a, size_t len)
{
FILL_RAND(a, len, rand_dbl);
}
void fill_rand_fcmplx(float complex *a, size_t len)
{
FILL_RAND(a, len, rand_fcmplx);
}
void fill_rand_dcmplx(double complex *a, size_t len)
{
FILL_RAND(a, len, rand_dcmplx);
}
#define MATRIX_PRINT(A, nrows, ncols) \
{ \
size_t r, c; \
for (r = 0; r < nrows; r++) { \
for (c = 0; c < ncols; c++) { \
printf("%s%f", (c > 0 ? ", " : ""), A[r * ncols + c]); \
} \
printf("\n"); \
} \
}
#define MATRIX_PRINT_CMPLX(A, nrows, ncols) \
{ \
size_t r, c, i; \
for (r = 0; r < nrows; r++) { \
for (c = 0; c < ncols; c++) { \
i = r * ncols + c; \
/* TODO: should be crealf and cimagf for complex */ \
printf("%s(%f, %f)", (c > 0 ? ", " : ""), creal(A[i]), cimag(A[i])); \
} \
printf("\n"); \
} \
}
void matrix_print_flt(const float *A, size_t nrows, size_t ncols)
{
MATRIX_PRINT(A, nrows, ncols);
}
void matrix_print_dbl(const double *A, size_t nrows, size_t ncols)
{
MATRIX_PRINT(A, nrows, ncols);
}
void matrix_print_fcmplx(const float complex *A, size_t nrows, size_t ncols)
{
MATRIX_PRINT_CMPLX(A, nrows, ncols);
}
void matrix_print_dcmplx(const double complex *A, size_t nrows, size_t ncols)
{
MATRIX_PRINT_CMPLX(A, nrows, ncols);
}
int is_eq_flt(float a, float b)
{
float v = a - b;
return v >= 0.0 ? (v < FLT_EPSILON) : (v > -FLT_EPSILON);
}
int is_eq_dbl(double a, double b)
{
double v = a - b;
return v >= 0.0 ? (v < DBL_EPSILON) : (v > -DBL_EPSILON);
}
int is_eq_fcmplx(float complex a, float complex b)
{
return is_eq_flt(crealf(a), crealf(b)) && is_eq_flt(cimagf(a), cimagf(b));
}
int is_eq_dcmplx(double complex a, double complex b)
{
return is_eq_dbl(creal(a), creal(b)) && is_eq_dbl(cimag(a), cimag(b));
}
void *assert_malloc(size_t sz)
{
void *ptr = malloc(sz);
if (!ptr) {
perror("malloc");
exit(ENOMEM);
}
return ptr;
}
void *assert_malloc_al(size_t sz)
{
size_t align;
void *ptr;
if (sz % 64 == 0) {
align = 64;
} else if (sz % 32 == 0) {
align = 32;
} else {
fprintf(stderr, "assert_malloc: sz must be a multiple of 64 or 32\n");
exit(EINVAL);
}
#if defined(HAVE_ALIGNED_ALLOC)
ptr = aligned_alloc(align, sz);
if (!ptr) {
perror("aligned_alloc");
exit(ENOMEM);
}
#else
errno = posix_memalign(&ptr, align, sz);
if (errno) {
perror("posix_memalign");
exit(ENOMEM);
}
#endif
return ptr;
}