Build system improvements
[ustl.git] / uutility.h
blobe17ba713811b8a96c2b50c85324b7b110eb83f46
1 // This file is part of the ustl library, an STL implementation.
2 //
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 /// \file uutility.h
7 ///
8 /// \brief Utility templates.
9 ///
10 /// Everything in here except min(), max(), distance(), and advance()
11 /// are uSTL extensions and are absent from other STL implementations.
12 ///
14 #ifndef UUTILITY_H_6A58BD296269A82A4AAAA4FD19FDB3AC
15 #define UUTILITY_H_6A58BD296269A82A4AAAA4FD19FDB3AC
17 #include "utypes.h"
18 #include "traits.h"
19 #include <assert.h>
21 namespace ustl {
23 #ifdef __GNUC__
24 /// Returns the number of elements in a static vector
25 #define VectorSize(v) (sizeof(v) / sizeof(*v))
26 #else
27 // Old compilers will not be able to evaluate *v on an empty vector.
28 // The tradeoff here is that VectorSize will not be able to measure arrays of local structs.
29 #define VectorSize(v) (sizeof(v) / ustl::size_of_elements(1, v))
30 #endif
32 /// Expands into a ptr,size expression for the given static vector; useful as link arguments.
33 #define VectorBlock(v) (v)+0, VectorSize(v) // +0 makes it work under gcc 2.95
34 /// Expands into a begin,end expression for the given static vector; useful for algorithm arguments.
35 #define VectorRange(v) VectorBlock(v)+(v)
37 /// Indexes into a static array with bounds limit
38 #define VectorElement(v,i) v[min(uoff_t(i),uoff_t(VectorSize(v)-1))]
40 /// Returns the number of bits in the given type
41 #define BitsInType(t) (sizeof(t) * CHAR_BIT)
43 /// Returns the mask of type \p t with the lowest \p n bits set.
44 #define BitMask(t,n) (t(~t(0)) >> ((sizeof(t) * CHAR_BIT) - (n)))
46 /// Argument that is used only in debug builds (as in an assert)
47 #ifndef NDEBUG
48 #define DebugArg(x) x
49 #else
50 #define DebugArg(x)
51 #endif
53 /// Shorthand for container iteration.
54 #define foreach(type,i,ctr) for (type i = (ctr).begin(); i != (ctr).end(); ++ i)
55 /// Shorthand for container reverse iteration.
56 #define eachfor(type,i,ctr) for (type i = (ctr).rbegin(); i != (ctr).rend(); ++ i)
58 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59 // Macro for passing template types as macro arguments.
60 // These are deprecated. Use metamac macros instead. Will remove by next release.
61 #define TEMPLATE_FULL_DECL1(d1,t1) template <d1 t1>
62 #define TEMPLATE_FULL_DECL2(d1,t1,d2,t2) template <d1 t1, d2 t2>
63 #define TEMPLATE_FULL_DECL3(d1,t1,d2,t2,d3,t3) template <d1 t1, d2 t2, d3 t3>
64 #define TEMPLATE_DECL1(t1) TEMPLATE_FULL_DECL1(typename,t1)
65 #define TEMPLATE_DECL2(t1,t2) TEMPLATE_FULL_DECL2(typename,t1,typename,t2)
66 #define TEMPLATE_DECL3(t1,t2,t3) TEMPLATE_FULL_DECL3(typename,t1,typename,t2,typename,t3)
67 #define TEMPLATE_TYPE1(type,a1) type<a1>
68 #define TEMPLATE_TYPE2(type,a1,a2) type<a1,a2>
69 #define TEMPLATE_TYPE3(type,a1,a2,a3) type<a1,a2,a3>
70 #endif
72 /// Returns the minimum of \p a and \p b
73 template <typename T1, typename T2>
74 inline T1 min (const T1& a, const T2& b)
76 return (a < b ? a : b);
79 /// Returns the maximum of \p a and \p b
80 template <typename T1, typename T2>
81 inline T1 max (const T1& a, const T2& b)
83 return (b < a ? a : b);
86 /// The alignment performed by default.
87 const size_t c_DefaultAlignment = __alignof__(void*);
89 /// \brief Rounds \p n up to be divisible by \p grain
90 template <typename T>
91 inline T Align (T n, size_t grain = c_DefaultAlignment)
93 T r = n % grain;
94 T a = n + (grain - r);
95 return (r ? a : n);
98 /// Returns a NULL pointer cast to T.
99 template <typename T>
100 inline T* NullPointer (void)
101 { return ((T*) NULL); }
103 /// \brief Returns a non-dereferentiable value reference.
104 /// This is useful for passing to alignof or the like which need a value but
105 /// don't need to actually use it.
106 template <typename T>
107 inline T& NullValue (void)
108 { return (*NullPointer<T>()); }
110 /// Offsets an iterator
111 template <typename T>
112 inline T advance (T i, ssize_t offset)
114 return (i + offset);
117 #ifndef DOXYGEN_SHOULD_SKIP_THIS
118 /// Offsets a void pointer
119 template <>
120 inline const void* advance (const void* p, ssize_t offset)
122 assert (p || !offset);
123 return (reinterpret_cast<const uint8_t*>(p) + offset);
126 /// Offsets a void pointer
127 template <>
128 inline void* advance (void* p, ssize_t offset)
130 assert (p || !offset);
131 return (reinterpret_cast<uint8_t*>(p) + offset);
133 #endif
135 /// Returns the difference \p p1 - \p p2
136 template <typename T1, typename T2>
137 inline ptrdiff_t distance (T1 i1, T2 i2)
139 return (i2 - i1);
142 #ifndef DOXYGEN_SHOULD_SKIP_THIS
143 #define UNVOID_DISTANCE(T1const,T2const) \
144 template <> inline ptrdiff_t distance (T1const void* p1, T2const void* p2) \
145 { return ((T2const uint8_t*)(p2) - (T1const uint8_t*)(p1)); }
146 UNVOID_DISTANCE(,)
147 UNVOID_DISTANCE(const,const)
148 UNVOID_DISTANCE(,const)
149 UNVOID_DISTANCE(const,)
150 #undef UNVOID_DISTANCE
151 #endif
153 /// \brief Returns the absolute value of \p v
154 /// Unlike the stdlib functions, this is inline and works with all types.
155 template <typename T>
156 inline T absv (T v)
158 return (v < 0 ? -v : v);
161 /// \brief Returns -1 for negative values, 1 for positive, and 0 for 0
162 template <typename T>
163 inline T sign (T v)
165 return ((0 < v) - (v < 0));
168 /// Returns the absolute value of the distance i1 and i2
169 template <typename T1, typename T2>
170 inline size_t abs_distance (T1 i1, T2 i2)
172 return (absv (distance(i1, i2)));
175 /// Returns the size of \p n elements of size \p T
176 template <typename T>
177 inline size_t size_of_elements (size_t n, const T*)
179 return (n * sizeof(T));
182 // Defined in byteswap.h, which is usually unusable.
183 #undef bswap_16
184 #undef bswap_32
185 #undef bswap_64
187 #if CPU_HAS_CMPXCHG8 // If it has that, it has bswap.
188 inline uint16_t bswap_16 (uint16_t v) { asm ("rorw $8, %w0" : "=r"(v) : "0"(v) : "cc"); return (v); }
189 inline uint32_t bswap_32 (uint32_t v) { asm ("bswap %0" : "=r"(v) : "0"(v)); return (v); }
190 #else
191 inline uint16_t bswap_16 (uint16_t v) { return (v << 8 | v >> 8); }
192 inline uint32_t bswap_32 (uint32_t v) { return (v << 24 | (v & 0xFF00) << 8 | (v >> 8) & 0xFF00 | v >> 24); }
193 #endif
194 #if HAVE_INT64_T
195 inline uint64_t bswap_64 (uint64_t v) { return ((uint64_t(bswap_32(v)) << 32) | bswap_32(v >> 32)); }
196 #endif
198 /// \brief Swaps the byteorder of \p v.
199 template <typename T>
200 inline T bswap (const T& v)
202 switch (BitsInType(T)) {
203 default: return (v);
204 case 16: return (T (bswap_16 (uint16_t (v))));
205 case 32: return (T (bswap_32 (uint32_t (v))));
206 #if HAVE_INT64_T
207 case 64: return (T (bswap_64 (uint64_t (v))));
208 #endif
212 #if BYTE_ORDER == BIG_ENDIAN
213 template <typename T> inline T le_to_native (const T& v) { return (bswap (v)); }
214 template <typename T> inline T be_to_native (const T& v) { return (v); }
215 template <typename T> inline T native_to_le (const T& v) { return (bswap (v)); }
216 template <typename T> inline T native_to_be (const T& v) { return (v); }
217 #elif BYTE_ORDER == LITTLE_ENDIAN
218 template <typename T> inline T le_to_native (const T& v) { return (v); }
219 template <typename T> inline T be_to_native (const T& v) { return (bswap (v)); }
220 template <typename T> inline T native_to_le (const T& v) { return (v); }
221 template <typename T> inline T native_to_be (const T& v) { return (bswap (v)); }
222 #endif // BYTE_ORDER
224 /// Deletes \p p and sets it to NULL
225 template <typename T>
226 inline void Delete (T*& p)
228 delete p;
229 p = NULL;
232 /// Deletes \p p as an array and sets it to NULL
233 template <typename T>
234 inline void DeleteVector (T*& p)
236 delete [] p;
237 p = NULL;
240 /// Template of making != from ! and ==
241 template <typename T>
242 inline bool operator!= (const T& x, const T& y)
244 return (!(x == y));
247 /// Template of making > from <
248 template <typename T>
249 inline bool operator> (const T& x, const T& y)
251 return (y < x);
254 /// Template of making <= from < and ==
255 template <typename T>
256 inline bool operator<= (const T& x, const T& y)
258 return (!(y < x));
261 /// Template of making >= from < and ==
262 template <typename T>
263 inline bool operator>= (const T& x, const T& y)
265 return (!(x < y));
268 /// Packs \p s multiple times into \p b. Useful for loop unrolling.
269 template <typename TSmall, typename TBig>
270 inline void pack_type (TSmall s, TBig& b)
272 const size_t n = sizeof(TBig) / sizeof(TSmall);
273 b = s;
274 // Calls to min are here to avoid warnings for shifts bigger than the type. min will be gone when optimized.
275 if (n < 2) return;
276 b = (b << min (BitsInType(TSmall), BitsInType(TBig))) | b;
277 if (n < 4) return;
278 b = (b << min (BitsInType(TSmall) * 2, BitsInType(TBig))) | b;
279 if (n < 8) return;
280 b = (b << min (BitsInType(TSmall) * 4, BitsInType(TBig))) | b;
283 /// \brief Divides \p n1 by \p n2 and rounds the result up.
284 /// This is in contrast to regular division, which rounds down.
285 template <typename T1, typename T2>
286 inline T1 DivRU (T1 n1, T2 n2)
288 T2 adj = n2 - 1;
289 if (n1 < 0)
290 adj = -adj;
291 return ((n1 + adj) / n2);
294 #if __GNUC__ >= 3
295 inline bool TestAndSet (int* pm) __attribute__((always_inline));
296 #endif
297 /// Sets the contents of \p pm to 1 and returns true if the previous value was 0.
298 inline bool TestAndSet (int* pm)
300 #if CPU_HAS_CMPXCHG8
301 bool rv;
302 int oldVal (1);
303 asm volatile ( // cmpxchg compares to %eax and swaps if equal
304 "cmpxchgl %3, %1\n\t"
305 "sete %0"
306 : "=a" (rv), "=m" (*pm), "=r" (oldVal)
307 : "2" (oldVal), "a" (0)
308 : "memory");
309 return (rv);
310 #elif __i386__ || __x86_64__
311 int oldVal (1);
312 asm volatile ("xchgl %0, %1" : "=r"(oldVal), "=m"(*pm) : "0"(oldVal), "m"(*pm) : "memory");
313 return (!oldVal);
314 #elif __sparc32__ // This has not been tested
315 int rv;
316 asm volatile ("ldstub %1, %0" : "=r"(rv), "=m"(*pm) : "m"(pm));
317 return (!rv);
318 #else
319 const int oldVal (*pm);
320 *pm = 1;
321 return (!oldVal);
322 #endif
325 /// \brief This template is to be used for dereferencing a type-punned pointer without a warning.
327 /// When casting a local variable to an unrelated type through a pointer (for
328 /// example, casting a float to a uint32_t without conversion), the resulting
329 /// memory location can be accessed through either pointer, which violates the
330 /// strict aliasing rule. While -fno-strict-aliasing option can be given to
331 /// the compiler, eliminating this warning, inefficient code may result in
332 /// some instances, because aliasing inhibits some optimizations. By using
333 /// this template, and by ensuring the memory is accessed in one way only,
334 /// efficient code can be produced without the warning. For gcc 4.1.0+.
336 template <typename DEST, typename SRC>
337 inline DEST noalias (const DEST&, SRC* s)
339 asm("":"+g"(s)::"memory");
340 union UPun { SRC s; DEST d; };
341 return (((UPun*)(s))->d);
344 template <typename DEST, typename SRC>
345 inline DEST noalias_cast (SRC s)
347 asm("":"+g"(s)::"memory");
348 union { SRC s; DEST d; } u = {s};
349 return (u.d);
352 namespace simd {
353 /// Call after you are done using SIMD algorithms for 64 bit tuples.
354 #if CPU_HAS_MMX
355 inline void reset_mmx (void) __attribute__((always_inline));
356 #define ALL_MMX_REGS_CHANGELIST "mm0","mm1","mm2","mm3","mm4","mm5","mm6","mm7","st","st(1)","st(2)","st(3)","st(4)","st(5)","st(6)","st(7)"
357 #if CPU_HAS_3DNOW
358 inline void reset_mmx (void) { asm ("femms":::ALL_MMX_REGS_CHANGELIST); }
359 #else
360 inline void reset_mmx (void) { asm ("emms":::ALL_MMX_REGS_CHANGELIST); }
361 #endif
362 #else
363 inline void reset_mmx (void) {}
364 #endif
365 } // namespace simd
367 } // namespace ustl
369 #endif