imports.h revision b8e80941
1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26/**
27 * \file imports.h
28 * Standard C library function wrappers.
29 *
30 * This file provides wrappers for all the standard C library functions
31 * like malloc(), free(), printf(), getenv(), etc.
32 */
33
34
35#ifndef IMPORTS_H
36#define IMPORTS_H
37
38
39#include <stdlib.h>
40#include <stdarg.h>
41#include <string.h>
42#include "compiler.h"
43#include "glheader.h"
44#include "util/bitscan.h"
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50
51/**********************************************************************/
52/** Memory macros */
53/*@{*/
54
55/** Allocate a structure of type \p T */
56#define MALLOC_STRUCT(T)   (struct T *) malloc(sizeof(struct T))
57/** Allocate and zero a structure of type \p T */
58#define CALLOC_STRUCT(T)   (struct T *) calloc(1, sizeof(struct T))
59
60/*@}*/
61
62
63/*
64 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
65 * as offsets into buffer stores.  Since the vertex array pointer and
66 * buffer store pointer are both pointers and we need to add them, we use
67 * this macro.
68 * Both pointers/offsets are expressed in bytes.
69 */
70#define ADD_POINTERS(A, B)  ( (GLubyte *) (A) + (uintptr_t) (B) )
71
72
73/**
74 * Sometimes we treat GLfloats as GLints.  On x86 systems, moving a float
75 * as an int (thereby using integer registers instead of FP registers) is
76 * a performance win.  Typically, this can be done with ordinary casts.
77 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
78 * these casts generate warnings.
79 * The following union typedef is used to solve that.
80 */
81typedef union { GLfloat f; GLint i; GLuint u; } fi_type;
82
83
84
85#if defined(_MSC_VER)
86#define strcasecmp(s1, s2) _stricmp(s1, s2)
87#endif
88/*@}*/
89
90
91/***
92 *** LOG2: Log base 2 of float
93 ***/
94static inline GLfloat LOG2(GLfloat x)
95{
96#if 0
97   /* This is pretty fast, but not accurate enough (only 2 fractional bits).
98    * Based on code from http://www.stereopsis.com/log2.html
99    */
100   const GLfloat y = x * x * x * x;
101   const GLuint ix = *((GLuint *) &y);
102   const GLuint exp = (ix >> 23) & 0xFF;
103   const GLint log2 = ((GLint) exp) - 127;
104   return (GLfloat) log2 * (1.0 / 4.0);  /* 4, because of x^4 above */
105#endif
106   /* Pretty fast, and accurate.
107    * Based on code from http://www.flipcode.com/totd/
108    */
109   fi_type num;
110   GLint log_2;
111   num.f = x;
112   log_2 = ((num.i >> 23) & 255) - 128;
113   num.i &= ~(255 << 23);
114   num.i += 127 << 23;
115   num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
116   return num.f + log_2;
117}
118
119
120
121/**
122 * finite macro.
123 */
124#if defined(_MSC_VER)
125#  define finite _finite
126#endif
127
128
129/***
130 *** IS_INF_OR_NAN: test if float is infinite or NaN
131 ***/
132#if defined(isfinite)
133#define IS_INF_OR_NAN(x)        (!isfinite(x))
134#elif defined(finite)
135#define IS_INF_OR_NAN(x)        (!finite(x))
136#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
137#define IS_INF_OR_NAN(x)        (!isfinite(x))
138#else
139#define IS_INF_OR_NAN(x)        (!finite(x))
140#endif
141
142
143/**
144 * Convert float to int by rounding to nearest integer, away from zero.
145 */
146static inline int IROUND(float f)
147{
148   return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
149}
150
151/**
152 * Convert double to int by rounding to nearest integer, away from zero.
153 */
154static inline int IROUNDD(double d)
155{
156   return (int) ((d >= 0.0) ? (d + 0.5) : (d - 0.5));
157}
158
159/**
160 * Convert float to int64 by rounding to nearest integer.
161 */
162static inline GLint64 IROUND64(float f)
163{
164   return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
165}
166
167
168/**
169 * Convert positive float to int by rounding to nearest integer.
170 */
171static inline int IROUND_POS(float f)
172{
173   assert(f >= 0.0F);
174   return (int) (f + 0.5F);
175}
176
177/** Return (as an integer) floor of float */
178static inline int IFLOOR(float f)
179{
180#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
181   /*
182    * IEEE floor for computers that round to nearest or even.
183    * 'f' must be between -4194304 and 4194303.
184    * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
185    * but uses some IEEE specific tricks for better speed.
186    * Contributed by Josh Vanderhoof
187    */
188   int ai, bi;
189   double af, bf;
190   af = (3 << 22) + 0.5 + (double)f;
191   bf = (3 << 22) + 0.5 - (double)f;
192   /* GCC generates an extra fstp/fld without this. */
193   __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
194   __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
195   return (ai - bi) >> 1;
196#else
197   int ai, bi;
198   double af, bf;
199   fi_type u;
200   af = (3 << 22) + 0.5 + (double)f;
201   bf = (3 << 22) + 0.5 - (double)f;
202   u.f = (float) af;  ai = u.i;
203   u.f = (float) bf;  bi = u.i;
204   return (ai - bi) >> 1;
205#endif
206}
207
208
209/**
210 * Is x a power of two?
211 */
212static inline int
213_mesa_is_pow_two(int x)
214{
215   return !(x & (x - 1));
216}
217
218/**
219 * Round given integer to next higer power of two
220 * If X is zero result is undefined.
221 *
222 * Source for the fallback implementation is
223 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
224 * http://graphics.stanford.edu/~seander/bithacks.html
225 *
226 * When using builtin function have to do some work
227 * for case when passed values 1 to prevent hiting
228 * undefined result from __builtin_clz. Undefined
229 * results would be different depending on optimization
230 * level used for build.
231 */
232static inline int32_t
233_mesa_next_pow_two_32(uint32_t x)
234{
235#ifdef HAVE___BUILTIN_CLZ
236	uint32_t y = (x != 1);
237	return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
238#else
239	x--;
240	x |= x >> 1;
241	x |= x >> 2;
242	x |= x >> 4;
243	x |= x >> 8;
244	x |= x >> 16;
245	x++;
246	return x;
247#endif
248}
249
250static inline int64_t
251_mesa_next_pow_two_64(uint64_t x)
252{
253#ifdef HAVE___BUILTIN_CLZLL
254	uint64_t y = (x != 1);
255	STATIC_ASSERT(sizeof(x) == sizeof(long long));
256	return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
257#else
258	x--;
259	x |= x >> 1;
260	x |= x >> 2;
261	x |= x >> 4;
262	x |= x >> 8;
263	x |= x >> 16;
264	x |= x >> 32;
265	x++;
266	return x;
267#endif
268}
269
270
271/*
272 * Returns the floor form of binary logarithm for a 32-bit integer.
273 */
274static inline GLuint
275_mesa_logbase2(GLuint n)
276{
277#ifdef HAVE___BUILTIN_CLZ
278   return (31 - __builtin_clz(n | 1));
279#else
280   GLuint pos = 0;
281   if (n >= 1<<16) { n >>= 16; pos += 16; }
282   if (n >= 1<< 8) { n >>=  8; pos +=  8; }
283   if (n >= 1<< 4) { n >>=  4; pos +=  4; }
284   if (n >= 1<< 2) { n >>=  2; pos +=  2; }
285   if (n >= 1<< 1) {           pos +=  1; }
286   return pos;
287#endif
288}
289
290
291/**
292 * Return 1 if this is a little endian machine, 0 if big endian.
293 */
294static inline GLboolean
295_mesa_little_endian(void)
296{
297   const GLuint ui = 1; /* intentionally not static */
298   return *((const GLubyte *) &ui);
299}
300
301
302
303/**********************************************************************
304 * Functions
305 */
306
307extern void *
308_mesa_align_malloc( size_t bytes, unsigned long alignment );
309
310extern void *
311_mesa_align_calloc( size_t bytes, unsigned long alignment );
312
313extern void
314_mesa_align_free( void *ptr );
315
316extern void *
317_mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
318                    unsigned long alignment);
319
320extern int
321_mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
322
323extern int
324_mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
325
326
327#if defined(_WIN32) && !defined(strtok_r)
328#define strtok_r strtok_s
329#endif
330
331#ifdef __cplusplus
332}
333#endif
334
335
336#endif /* IMPORTS_H */
337