Waltham  0.1.0
waltham-util.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
31 #ifndef WALTHAM_UTIL_H
32 #define WALTHAM_UTIL_H
33 
34 #include <math.h>
35 #include <stddef.h>
36 #include <inttypes.h>
37 #include <stdarg.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /* GCC visibility */
44 #if defined(__GNUC__) && __GNUC__ >= 4
45 #define WTH_EXPORT __attribute__ ((visibility("default")))
46 #else
47 #define WTH_EXPORT
48 #endif
49 
50 /* Deprecated attribute */
51 #if defined(__GNUC__) && __GNUC__ >= 4
52 #define WTH_DEPRECATED __attribute__ ((deprecated))
53 #else
54 #define WTH_DEPRECATED
55 #endif
56 
57 /* Printf annotation */
58 #if defined(__GNUC__) && __GNUC__ >= 4
59 #define WTH_PRINTF(x, y) __attribute__((__format__(__printf__, x, y)))
60 #else
61 #define WTH_PRINTF(x, y)
62 #endif
63 
64 struct wth_array {
65  size_t size;
66  size_t alloc;
67  void *data;
68 };
69 
70 #define wth_array_for_each(pos, array) \
71  for (pos = (array)->data; \
72  (const char *) pos < ((const char *) (array)->data + (array)->size); \
73  (pos)++)
74 
75 void
76 wth_array_init(struct wth_array *array);
77 
78 void
79 wth_array_release(struct wth_array *array);
80 
81 void *
82 wth_array_add(struct wth_array *array, size_t size);
83 
84 int
85 wth_array_copy(struct wth_array *array, struct wth_array *source);
86 
87 typedef int32_t wth_fixed_t;
88 
89 static inline double
90 wth_fixed_to_double (wth_fixed_t f)
91 {
92  union {
93  double d;
94  int64_t i;
95  } u;
96 
97  u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
98 
99  return u.d - (3LL << 43);
100 }
101 
102 static inline wth_fixed_t
103 wth_fixed_from_double(double d)
104 {
105  union {
106  double d;
107  int64_t i;
108  } u;
109 
110  u.d = d + (3LL << (51 - 8));
111 
112  return u.i;
113 }
114 
115 static inline int
116 wth_fixed_to_int(wth_fixed_t f)
117 {
118  return f / 256;
119 }
120 
121 static inline wth_fixed_t
122 wth_fixed_from_int(int i)
123 {
124  return i * 256;
125 }
126 
136 };
137 
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 #endif
Continue the iteration.
Definition: waltham-util.h:135
Definition: waltham-util.h:64
wth_iterator_result
This enum represents the return value of an iterator function.
Definition: waltham-util.h:131
Stop the iteration.
Definition: waltham-util.h:133