H2Lib  3.0
basic.h
Go to the documentation of this file.
1 
2 /* ------------------------------------------------------------
3  * This is the file "basic.h" of the H2Lib package.
4  * All rights reserved, Steffen Boerm 2009
5  * ------------------------------------------------------------ */
6 
10 #ifndef BASIC_H
11 #define BASIC_H
12 
18 typedef struct _stopwatch stopwatch;
19 
22 
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <stdarg.h>
26 #ifdef USE_CAIRO
27 #include <cairo/cairo.h>
28 #endif
29 
30 #include "settings.h"
31 
32 #ifdef USE_SIMD
33 #include "simd.h"
34 #endif
35 
36 /* ------------------------------------------------------------
37  * Miscellaneous
38  * ------------------------------------------------------------ */
39 
41 #ifndef M_PI
42 #define M_PI 3.141592653589793238462643383
43 #endif
44 
46 #define NORM_STEPS 20
47 
51 #define NC_DEFLATE_LEVEL 9
52 
54 extern int max_pardepth;
55 
57 #define H2_MACH_EPS 1e-13
58 
59 /* ------------------------------------------------------------
60  Set up the library
61  ------------------------------------------------------------ */
62 
72 HEADER_PREFIX void
73 init_h2lib(int *argc, char ***argv);
74 
79 HEADER_PREFIX void
80 uninit_h2lib();
81 
82 /* ------------------------------------------------------------
83  * General utility macros and functions
84  * ------------------------------------------------------------ */
85 
90 #ifdef USE_SIMD
91 #define h2_malloc(sz) _mm_malloc(sz, VALIGN)
92 #else
93 #define h2_malloc(sz) malloc(sz)
94 #endif
95 
100 #ifdef USE_SIMD
101 #define h2_free(p) _mm_free(p)
102 #else
103 #define h2_free(p) free(p)
104 #endif
105 
106 
111 HEADER_PREFIX size_t
113 
120 #define ROUNDUP(x, N) ((((x) + (N) - 1) / (N)) * (N))
121 
122 #ifdef ABS
123 #undef ABS
124 #endif
125 
126 #ifdef UINT_MAX
127 #undef UINT_MAX
128 #endif
129 
130 /* Macros for the type "field" */
131 
133 #ifdef USE_COMPLEX
134 #ifdef USE_FLOAT
135 #define REAL(x) crealf(x)
136 #else
137 #define REAL(x) creal(x)
138 #endif
139 #else
140 #define REAL(x) (x)
141 #endif
142 
144 #ifdef USE_COMPLEX
145 #ifdef USE_FLOAT
146 #define IMAG(x) cimagf(x)
147 #else
148 #define IMAG(x) cimag(x)
149 #endif
150 #else
151 #define IMAG(x) 0.0
152 #endif
153 
155 #ifdef USE_COMPLEX
156 #ifdef USE_FLOAT
157 #define CONJ(x) conjf(x)
158 #else
159 #define CONJ(x) conj(x)
160 #endif
161 #else
162 #define CONJ(x) (x)
163 #endif
164 
166 #define ABSSQR(x) _h2_abssqr(x)
167 
169 #define ABS(x) REAL_SQRT(ABSSQR(x))
170 
173 #define SIGN1(x) _h2_sgn1(x)
174 
176 #define FIELD_RAND() _h2_fieldrand()
177 
178 /* Macros for the type "real" */
179 
181 #define REAL_ABS(x) fabs(x)
182 
184 #define REAL_SQR(x) _h2_real_sqr(x)
185 
187 #ifdef USE_FLOAT
188 #define REAL_SIN(x) sinf(x)
189 #else
190 #define REAL_SIN(x) sin(x)
191 #endif
192 
194 #ifdef USE_FLOAT
195 #define REAL_COS(x) cosf(x)
196 #else
197 #define REAL_COS(x) cos(x)
198 #endif
199 
201 #ifdef USE_FLOAT
202 #define REAL_ASIN(x) asinf(x)
203 #else
204 #define REAL_ASIN(x) asin(x)
205 #endif
206 
208 #ifdef USE_FLOAT
209 #define REAL_ACOS(x) acosf(x)
210 #else
211 #define REAL_ACOS(x) acos(x)
212 #endif
213 
215 #ifdef USE_FLOAT
216 #define REAL_TAN(x) tanf(x)
217 #else
218 #define REAL_TAN(x) tan(x)
219 #endif
220 
222 #ifdef USE_FLOAT
223 #define REAL_SQRT(x) sqrtf(x)
224 #else
225 #define REAL_SQRT(x) sqrt(x)
226 #endif
227 
230 #define REAL_RSQRT(x) _h2_rsqrt(x)
231 
233 #ifdef USE_FLOAT
234 #define REAL_POW(x, y) powf(x, y)
235 #else
236 #define REAL_POW(x, y) pow(x, y)
237 #endif
238 
240 #ifdef USE_FLOAT
241 #define REAL_LOG(x) logf(x)
242 #else
243 #define REAL_LOG(x) log(x)
244 #endif
245 
247 #ifdef USE_FLOAT
248 #define REAL_EXP(x) expf(x)
249 #else
250 #define REAL_EXP(x) exp(x)
251 #endif
252 
254 #define REAL_RAND() _h2_realrand()
255 
258 #define DOT2(x,y) (CONJ((x)[0]) * (y)[0] + CONJ((x)[1]) * (y)[1])
259 
262 #define DOT3(x,y) (CONJ((x)[0]) * (y)[0] + CONJ((x)[1]) * (y)[1] + CONJ((x)[2]) * (y)[2])
263 
266 #define NORMSQR2(x,y) (ABSSQR(x) + ABSSQR(y))
267 
270 #define NORMSQR3(x,y,z) (ABSSQR(x) + ABSSQR(y) + ABSSQR(z))
271 
274 #define NORM2(x,y) REAL_SQRT(ABSSQR(x) + ABSSQR(y))
275 
278 #define NORM3(x,y,z) REAL_SQRT(ABSSQR(x) + ABSSQR(y) + ABSSQR(z))
279 
282 #define REAL_DOT2(x,y) ((x)[0] * (y)[0] + (x)[1] * (y)[1])
283 
286 #define REAL_DOT3(x,y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2])
287 
290 #define REAL_NORMSQR2(x,y) (REAL_SQR(x) + REAL_SQR(y))
291 
294 #define REAL_NORMSQR3(x,y,z) (REAL_SQR(x) + REAL_SQR(y) + REAL_SQR(z))
295 
298 #define REAL_NORM2(x,y) REAL_SQRT(REAL_SQR(x) + REAL_SQR(y))
299 
302 #define REAL_NORM3(x,y,z) REAL_SQRT(REAL_SQR(x) + REAL_SQR(y) + REAL_SQR(z))
303 
305 #define REAL_MAX(x, y) _h2_realmax(x, y)
306 
308 #define REAL_MAX3(x, y, z) _h2_realmax3(x, y, z)
309 
311 #define REAL_MIN(x, y) _h2_realmin(x, y)
312 
314 #define REAL_MIN3(x, y, z) _h2_realmin3(x, y, z)
315 
316 /* Macros for the type "uint" */
317 
319 #define UINT_MAX(x, y) _h2_uintmax(x, y)
320 
322 #define UINT_MAX3(x, y, z) _h2_uintmax3(x, y, z)
323 
325 #define UINT_MIN(x, y) _h2_uintmin(x, y)
326 
328 #define UINT_MIN3(x, y, z) _h2_uintmin3(x, y, z)
329 
330 #ifdef __GNUC__
331 INLINE_PREFIX real _h2_rsqrt(real x) __attribute__((unused,const));
332 INLINE_PREFIX real _h2_abssqr(field x) __attribute__((unused,const));
333 INLINE_PREFIX field _h2_sgn1(field x) __attribute__((unused,const));
334 INLINE_PREFIX field _h2_fieldrand() __attribute__((unused,const));
335 INLINE_PREFIX real _h2_real_sqr(real x) __attribute__((unused,const));
336 INLINE_PREFIX real _h2_realmax(real a, real b) __attribute__((unused,const));
337 INLINE_PREFIX real _h2_realmax3(real a, real b, real c) __attribute__((unused,const));
338 INLINE_PREFIX real _h2_realmin(real a, real b) __attribute__((unused,const));
339 INLINE_PREFIX real _h2_realmin3(real a, real b, real c) __attribute__((unused,const));
340 INLINE_PREFIX real _h2_realrand() __attribute__((unused,const));
341 INLINE_PREFIX uint _h2_uintmax(uint a, uint b) __attribute__((unused,const));
342 INLINE_PREFIX uint _h2_uintmax3(uint a, uint b, uint c) __attribute__((unused,const));
343 INLINE_PREFIX uint _h2_uintmin(uint a, uint b) __attribute__((unused,const));
344 INLINE_PREFIX uint _h2_uintmin3(uint a, uint b, uint c) __attribute__((unused,const));
345 #else
346 INLINE_PREFIX real _h2_realrand();
347 #endif
348 
353 INLINE_PREFIX real _h2_rsqrt(real x) {
354  return r_one / REAL_SQRT(x);
355 }
356 
361 #ifdef USE_COMPLEX
362 INLINE_PREFIX real _h2_abssqr(field x) {
363  real rx = REAL(x);
364  real ix = IMAG(x);
365 
366  return rx * rx + ix * ix;
367 }
368 #else
369 INLINE_PREFIX real _h2_abssqr(field x) {
370  return x * x;
371 }
372 #endif
373 
379 #ifdef USE_COMPLEX
380 INLINE_PREFIX field _h2_sgn1(field x) {
381  real norm, rx, ix;
382 
383  if (ABSSQR(x) <= H2_ALMOST_ZERO) {
384  return 1.0;
385  } else {
386  rx = REAL(x);
387  ix = IMAG(x);
388  norm = REAL_RSQRT(rx * rx + ix * ix);
389  return rx * norm + ix * norm * I;
390  }
391 }
392 #else
393 INLINE_PREFIX field _h2_sgn1(field x) {
394  return (x < f_zero ? f_minusone : f_one);
395 }
396 #endif
397 
402 INLINE_PREFIX field _h2_fieldrand() {
403  field x;
404 
405  x = _h2_realrand();
406 #ifdef USE_COMPLEX
407  x += f_i * _h2_realrand();
408 #endif
409 
410  return x;
411 }
412 
417 INLINE_PREFIX real _h2_real_sqr(real x) {
418  return x * x;
419 }
420 
426 INLINE_PREFIX real _h2_realmax(real x, real y) {
427  return (x < y ? y : x);
428 }
429 
436 INLINE_PREFIX real _h2_realmax3(real x, real y, real z) {
437  return (x < y ? (y < z ? z : y) : (x < z ? z : x));
438 }
439 
445 INLINE_PREFIX real _h2_realmin(real a, real b) {
446  return (a < b ? a : b);
447 }
448 
455 INLINE_PREFIX real _h2_realmin3(real x, real y, real z) {
456  return (x < y ? (z < x ? z : x) : (z < y ? z : y));
457 }
458 
462 INLINE_PREFIX real _h2_realrand() {
463  return 2.0 * rand() / RAND_MAX - 1.0;
464 }
465 
471 INLINE_PREFIX uint _h2_uintmax(uint x, uint y) {
472  return (x < y ? y : x);
473 }
474 
481 INLINE_PREFIX uint _h2_uintmax3(uint x, uint y, uint z) {
482  return (x < y ? (y < z ? z : y) : (x < z ? z : x));
483 }
484 
490 INLINE_PREFIX uint _h2_uintmin(uint x, uint y) {
491  return (x < y ? x : y);
492 }
493 
500 INLINE_PREFIX uint _h2_uintmin3(uint x, uint y, uint z) {
501  return (x < y ? (z < x ? z : x) : (z < y ? z : y));
502 }
503 
504 /* ------------------------------------------------------------
505  * Memory management
506  * ------------------------------------------------------------ */
507 
512 #define allocmem(sz) _h2_allocmem(sz,__FILE__,__LINE__)
513 
519 void *
520 _h2_allocmem(size_t sz, const char *filename, int line);
521 
526 #define allocuint(sz) _h2_allocuint(sz,__FILE__,__LINE__)
527 
533 uint *
534 _h2_allocuint(size_t sz, const char *filename, int line);
535 
540 #define allocreal(sz) _h2_allocreal(sz,__FILE__,__LINE__)
541 
547 real *
548 _h2_allocreal(size_t sz, const char *filename, int line);
549 
554 #define allocfield(sz) _h2_allocfield(sz,__FILE__,__LINE__)
555 
561 field *
562 _h2_allocfield(size_t sz, const char *filename, int line);
563 
571 #define allocmatrix(rows,cols) _h2_allocmatrix(rows,cols,__FILE__,__LINE__)
572 
582 field *
583 _h2_allocmatrix(size_t rows, size_t cols, const char *filename, int line);
584 
588 void
589 freemem(void *ptr);
590 
591 /* ------------------------------------------------------------
592  * Sorting
593  * ------------------------------------------------------------ */
594 
607 #define heapsort(n,leq,swap,data) _h2_heapsort(n,leq,swap,data)
608 
621 HEADER_PREFIX void
622 _h2_heapsort(uint n, bool leq(uint, uint, void *),
623  void swap(uint, uint, void *), void *data);
624 
625 /* ------------------------------------------------------------
626  * Timing
627  * ------------------------------------------------------------ */
628 
633 new_stopwatch();
634 
638 HEADER_PREFIX void
640 
647 HEADER_PREFIX void
649 
661 
662 /* ------------------------------------------------------------
663  * Drawing
664  * ------------------------------------------------------------ */
665 
666 #ifdef USE_CAIRO
667 
677 HEADER_PREFIX cairo_t *
678 new_cairopdf(const char *filename, double width, double height);
679 
691 HEADER_PREFIX cairo_t *
692 new_cairopng(uint width, uint height);
693 
699 HEADER_PREFIX bool
700 write_cairopng(cairo_t *cr, const char *filename);
701 #endif
702 
739 #endif
#define ABSSQR(x)
Compute the square of the absolute value of a field element .
Definition: basic.h:166
const field f_zero
field constant zero
cairo_t * new_cairopng(uint width, uint height)
Create a PNG canvas for Cairo drawing.
#define REAL_SQRT(x)
Compute the square root of a non-negative real nunber .
Definition: basic.h:225
void uninit_h2lib()
Uninitialize the library.
unsigned uint
Unsigned integer type.
Definition: settings.h:70
stopwatch * pstopwatch
Pointer to a stopwatch object.
Definition: basic.h:21
#define H2_ALMOST_ZERO
Definition: settings.h:132
double _Complex field
Field type.
Definition: settings.h:171
pstopwatch new_stopwatch()
Create a stopwatch object.
const field f_i
field constant for the imaginary number.
#define INLINE_PREFIX
Prefix for inline functions.
Definition: settings.h:36
#define IMAG(x)
Get the imaginary part of a field element .
Definition: basic.h:148
cairo_t * new_cairopdf(const char *filename, double width, double height)
Create a PDF canvas for Cairo drawing.
real stop_stopwatch(pstopwatch sw)
Stop a stopwatch.
size_t get_current_memory()
Current amount of allocated memory (bytes)
void freemem(void *ptr)
Release allocated storage.
#define HEADER_PREFIX
Prefix for function declarations.
Definition: settings.h:43
const field f_minusone
field constant minus one.
double real
real floating point type.
Definition: settings.h:97
const field f_one
field constant one.
void start_stopwatch(pstopwatch sw)
Start a stopwatch.
bool write_cairopng(cairo_t *cr, const char *filename)
Write Cairo image to PNG file.
const real r_one
real constant one.
#define REAL(x)
Get the real part of a field element .
Definition: basic.h:137
void del_stopwatch(pstopwatch sw)
Delete a stopwatch object.
#define REAL_RSQRT(x)
Compute the reciprocal square root of a non-negative real nunber .
Definition: basic.h:230
int max_pardepth
Reasonable cut-off depth for parallelization.
struct _stopwatch stopwatch
Stop watch for run-time measurements.
Definition: basic.h:18
void init_h2lib(int *argc, char ***argv)
Initialize the library.