| 1 |
/* |
| 2 |
# This file is part of the Astrometry.net suite. |
| 3 |
# Licensed under a 3-clause BSD style license - see LICENSE |
| 4 |
*/ |
| 5 |
#ifndef OS_FEATURES_H |
| 6 |
#define OS_FEATURES_H |
| 7 |
|
| 8 |
// This is actually in POSIX1b but may or may not be available. |
| 9 |
int fdatasync(int fd); |
| 10 |
|
| 11 |
// Not POSIX; doesn't exist in Solaris 10 |
| 12 |
//#include <sys/param.h> |
| 13 |
#ifndef MIN |
| 14 |
#define MIN(a,b) (((a)<(b))?(a):(b)) |
| 15 |
#endif |
| 16 |
#ifndef MAX |
| 17 |
#define MAX(a,b) (((a)>(b))?(a):(b)) |
| 18 |
#endif |
| 19 |
|
| 20 |
// isfinite() on Solaris; from |
| 21 |
// https://code.google.com/p/redis/issues/detail?id=20 |
| 22 |
#if defined(__sun) && defined(__GNUC__) |
| 23 |
|
| 24 |
#undef isnan |
| 25 |
#define isnan(x) \ |
| 26 |
__extension__({ __typeof (x) __x_a = (x); \ |
| 27 |
__builtin_expect(__x_a != __x_a, 0); }) |
| 28 |
|
| 29 |
#undef isfinite |
| 30 |
#define isfinite(x) \ |
| 31 |
__extension__ ({ __typeof (x) __x_f = (x); \ |
| 32 |
__builtin_expect(!isnan(__x_f - __x_f), 1); }) |
| 33 |
|
| 34 |
#undef isinf |
| 35 |
#define isinf(x) \ |
| 36 |
__extension__ ({ __typeof (x) __x_i = (x); \ |
| 37 |
__builtin_expect(!isnan(__x_i) && !isfinite(__x_i), 0); }) |
| 38 |
|
| 39 |
#undef isnormal |
| 40 |
#define isnormal(x) \ |
| 41 |
__extension__ ({ __typeof(x) __x_n = (x); \ |
| 42 |
if (__x_n < 0.0) __x_n = -__x_n; \ |
| 43 |
__builtin_expect(isfinite(__x_n) \ |
| 44 |
&& (sizeof(__x_n) == sizeof(float) \ |
| 45 |
? __x_n >= __FLT_MIN__ \ |
| 46 |
: sizeof(__x_n) == sizeof(long double) \ |
| 47 |
? __x_n >= __LDBL_MIN__ \ |
| 48 |
: __x_n >= __DBL_MIN__), 1); }) |
| 49 |
|
| 50 |
#undef HUGE_VALF |
| 51 |
#define HUGE_VALF (1e50f) |
| 52 |
|
| 53 |
#endif |
| 54 |
|
| 55 |
/** |
| 56 |
The qsort_r story: |
| 57 |
|
| 58 |
-qsort_r appears in BSD (including Mac OSX) |
| 59 |
void qsort_r(void *, size_t, size_t, |
| 60 |
void *, |
| 61 |
int (*)(void *, const void *, const void *)); |
| 62 |
|
| 63 |
-qsort_r appears in glibc 2.8, but with a different argument order: |
| 64 |
void qsort_r(void*, size_t, size_t, |
| 65 |
int (*)(const void*, const void*, void*), |
| 66 |
void*); |
| 67 |
|
| 68 |
Notice that the "thunk" and "comparison function" arguments to qsort_r are |
| 69 |
swapped, and the "thunk" appears either at the beginning or end of the comparison |
| 70 |
function. |
| 71 |
|
| 72 |
We check a few things: |
| 73 |
-is qsort_r declared? |
| 74 |
-does qsort_r exist? |
| 75 |
-do we need to swap the arguments? |
| 76 |
|
| 77 |
Those using qsort_r in Astrometry.net should instead use the macro QSORT_R() |
| 78 |
to take advantage of these tests. |
| 79 |
|
| 80 |
Its signature is: |
| 81 |
|
| 82 |
void QSORT_R(void* base, size_t nmembers, size_t member_size, |
| 83 |
void* token, comparison_function); |
| 84 |
|
| 85 |
You should define the "comparison" function like this: |
| 86 |
|
| 87 |
static int QSORT_COMPARISON_FUNCTION(my_comparison, void* token, const void* v1, const void* v2) { |
| 88 |
... |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
Distributions including glibc 2.8 include: |
| 93 |
-Mandriva 2009 |
| 94 |
-Ubuntu 8.10 |
| 95 |
*/ |
| 96 |
|
| 97 |
///// In this healpix distribution, we avoid all this mess by importing |
| 98 |
///// a stand-alone qsort_reentrant implementation. |
| 99 |
|
| 100 |
typedef int cmp_t(void *, const void *, const void *); |
| 101 |
void |
| 102 |
qsort_rex(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp); |
| 103 |
|
| 104 |
#define QSORT_R qsort_rex |
| 105 |
#define QSORT_COMPARISON_FUNCTION(func, thunk, v1, v2) func(thunk, v1, v2) |
| 106 |
|
| 107 |
// As suggested in http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Names.html |
| 108 |
#if __STDC_VERSION__ < 199901L |
| 109 |
# if __GNUC__ >= 2 |
| 110 |
# define __func__ __FUNCTION__ |
| 111 |
# else |
| 112 |
# define __func__ "<unknown>" |
| 113 |
# endif |
| 114 |
#endif |
| 115 |
|
| 116 |
#endif |