summaryrefslogtreecommitdiff
path: root/include/libHX/misc.h
blob: 1d49c7ffb87b8af7674e6bf707dfb9fac5ef1ce6 (plain)
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
#ifndef _LIBHX_MISC_H
#define _LIBHX_MISC_H 1

#ifndef __cplusplus
#	include <limits.h>
#	include <stdarg.h>
#	include <stdbool.h>
#	include <stdio.h>
#else
#	include <climits>
#	include <cstdarg>
#	include <cstdio>
#endif

#ifdef __cplusplus
extern "C" {
#endif

/*
 *	BITMAP.H
 */
#define __HXbitmap_bpq(type) \
	(sizeof(type) * CHAR_BIT)
#define HXbitmap_size(type, bits) \
	(((bits) + __HXbitmap_bpq(type) - 1) / __HXbitmap_bpq(type))
#define __HXbitmap_quant(map, bit) \
	((map)[(bit) / __HXbitmap_bpq(*(map))])
#define HXbitmap_set(map, bit) \
	((void)(__HXbitmap_quant((map), (bit)) |= (1ULL << ((bit) % __HXbitmap_bpq(*(map))))))
#define HXbitmap_clear(map, bit) \
	((void)(__HXbitmap_quant((map), (bit)) &= ~(1ULL << ((bit) % __HXbitmap_bpq(*(map))))))
#define HXbitmap_test(map, bit) \
	((bool)(__HXbitmap_quant((map), (bit)) & (1ULL << ((bit) % __HXbitmap_bpq(*(map))))))

#define HX_TIMESPEC_FMT "%ld.%09ld"
#define HX_TIMEVAL_FMT "%ld.%06ld"
#ifdef __cplusplus
#	define HX_TIMESPEC_EXP(p) static_cast<long>((p)->tv_sec), static_cast<long>((p)->tv_nsec)
#	define HX_TIMEVAL_EXP(p) static_cast<long>((p)->tv_sec), static_cast<long>((p)->tv_usec)
#else
#	define HX_TIMESPEC_EXP(p) static_cast(long, (p)->tv_sec), static_cast(long, (p)->tv_nsec)
#	define HX_TIMEVAL_EXP(p) static_cast(long, (p)->tv_sec), static_cast(long, (p)->tv_usec)
#endif

struct stat;
struct timespec;
struct timeval;

/*
 *	DL.C
 */
extern void *HX_dlopen(const char *);
extern void *HX_dlsym(void *, const char *);
extern void HX_dlclose(void *);
extern const char *HX_dlerror(void);

/*
 *	MISC.C
 */
extern int HX_ffs(unsigned long);
extern int HX_fls(unsigned long);
extern float HX_flprf(float, float);
extern double HX_flpr(double, double);
#ifndef HX_HEXDUMP_DECLARATION
#define HX_HEXDUMP_DECLARATION 1
extern void HX_hexdump(FILE *, const void *, unsigned int);
#endif
extern bool HX_timespec_isneg(const struct timespec *);
extern struct timespec *HX_timespec_neg(struct timespec *,
	const struct timespec *);
extern struct timespec *HX_timespec_add(struct timespec *,
	const struct timespec *, const struct timespec *);
extern struct timespec *HX_timespec_sub(struct timespec *,
	const struct timespec *, const struct timespec *);
extern struct timespec *HX_timespec_mul(struct timespec *,
	const struct timespec *, int);
extern struct timespec *HX_timespec_mulf(struct timespec *,
	const struct timespec *, double);
extern struct timeval *HX_timeval_sub(struct timeval *,
	const struct timeval *, const struct timeval *);
extern long HX_time_compare(const struct stat *, const struct stat *, char);
extern void HX_zvecfree(char **);

/*
 *	RAND.C
 */
extern int HX_rand(void);
extern unsigned int HX_irand(unsigned int, unsigned int);
extern double HX_drand(double, double);

/*
 *	INLINE FUNCTIONS
 */
static __inline__ unsigned int HX_zveclen(const char *const *args)
{
	unsigned int argk = 0;
	while (*args++ != NULL)
		++argk;
	return argk;
}

#ifdef __cplusplus
} /* extern "C" */

extern "C++" {

template<typename type> static __inline__ type
HX_dlsym(void *handle, const char *symbol)
{
	return reinterpret_cast<type>(HX_dlsym(handle, symbol));
}

} /* extern "C++" */
#endif

#endif /* _LIBHX_MISC_H */