1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg/**
27848b8605Smrg * \file imports.h
28848b8605Smrg * Standard C library function wrappers.
29848b8605Smrg *
30848b8605Smrg * This file provides wrappers for all the standard C library functions
31848b8605Smrg * like malloc(), free(), printf(), getenv(), etc.
32848b8605Smrg */
33848b8605Smrg
34848b8605Smrg
35848b8605Smrg#ifndef IMPORTS_H
36848b8605Smrg#define IMPORTS_H
37848b8605Smrg
38848b8605Smrg
39b8e80941Smrg#include <stdlib.h>
40b8e80941Smrg#include <stdarg.h>
41b8e80941Smrg#include <string.h>
42848b8605Smrg#include "compiler.h"
43848b8605Smrg#include "glheader.h"
44b8e80941Smrg#include "util/bitscan.h"
45848b8605Smrg
46848b8605Smrg#ifdef __cplusplus
47848b8605Smrgextern "C" {
48848b8605Smrg#endif
49848b8605Smrg
50848b8605Smrg
51848b8605Smrg/**********************************************************************/
52848b8605Smrg/** Memory macros */
53848b8605Smrg/*@{*/
54848b8605Smrg
55848b8605Smrg/** Allocate a structure of type \p T */
56848b8605Smrg#define MALLOC_STRUCT(T)   (struct T *) malloc(sizeof(struct T))
57848b8605Smrg/** Allocate and zero a structure of type \p T */
58848b8605Smrg#define CALLOC_STRUCT(T)   (struct T *) calloc(1, sizeof(struct T))
59848b8605Smrg
60848b8605Smrg/*@}*/
61848b8605Smrg
62848b8605Smrg
63848b8605Smrg/*
64848b8605Smrg * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
65848b8605Smrg * as offsets into buffer stores.  Since the vertex array pointer and
66848b8605Smrg * buffer store pointer are both pointers and we need to add them, we use
67848b8605Smrg * this macro.
68848b8605Smrg * Both pointers/offsets are expressed in bytes.
69848b8605Smrg */
70848b8605Smrg#define ADD_POINTERS(A, B)  ( (GLubyte *) (A) + (uintptr_t) (B) )
71848b8605Smrg
72848b8605Smrg
73848b8605Smrg/**
74848b8605Smrg * Sometimes we treat GLfloats as GLints.  On x86 systems, moving a float
75b8e80941Smrg * as an int (thereby using integer registers instead of FP registers) is
76848b8605Smrg * a performance win.  Typically, this can be done with ordinary casts.
77848b8605Smrg * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
78848b8605Smrg * these casts generate warnings.
79848b8605Smrg * The following union typedef is used to solve that.
80848b8605Smrg */
81848b8605Smrgtypedef union { GLfloat f; GLint i; GLuint u; } fi_type;
82848b8605Smrg
83848b8605Smrg
84848b8605Smrg
85848b8605Smrg#if defined(_MSC_VER)
86848b8605Smrg#define strcasecmp(s1, s2) _stricmp(s1, s2)
87848b8605Smrg#endif
88848b8605Smrg/*@}*/
89848b8605Smrg
90848b8605Smrg
91848b8605Smrg/***
92848b8605Smrg *** LOG2: Log base 2 of float
93848b8605Smrg ***/
94848b8605Smrgstatic inline GLfloat LOG2(GLfloat x)
95848b8605Smrg{
96848b8605Smrg#if 0
97848b8605Smrg   /* This is pretty fast, but not accurate enough (only 2 fractional bits).
98848b8605Smrg    * Based on code from http://www.stereopsis.com/log2.html
99848b8605Smrg    */
100848b8605Smrg   const GLfloat y = x * x * x * x;
101848b8605Smrg   const GLuint ix = *((GLuint *) &y);
102848b8605Smrg   const GLuint exp = (ix >> 23) & 0xFF;
103848b8605Smrg   const GLint log2 = ((GLint) exp) - 127;
104848b8605Smrg   return (GLfloat) log2 * (1.0 / 4.0);  /* 4, because of x^4 above */
105848b8605Smrg#endif
106848b8605Smrg   /* Pretty fast, and accurate.
107848b8605Smrg    * Based on code from http://www.flipcode.com/totd/
108848b8605Smrg    */
109848b8605Smrg   fi_type num;
110848b8605Smrg   GLint log_2;
111848b8605Smrg   num.f = x;
112848b8605Smrg   log_2 = ((num.i >> 23) & 255) - 128;
113848b8605Smrg   num.i &= ~(255 << 23);
114848b8605Smrg   num.i += 127 << 23;
115848b8605Smrg   num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
116848b8605Smrg   return num.f + log_2;
117848b8605Smrg}
118848b8605Smrg
119848b8605Smrg
120848b8605Smrg
121b8e80941Smrg/**
122b8e80941Smrg * finite macro.
123b8e80941Smrg */
124b8e80941Smrg#if defined(_MSC_VER)
125b8e80941Smrg#  define finite _finite
126b8e80941Smrg#endif
127b8e80941Smrg
128b8e80941Smrg
129848b8605Smrg/***
130848b8605Smrg *** IS_INF_OR_NAN: test if float is infinite or NaN
131848b8605Smrg ***/
132848b8605Smrg#if defined(isfinite)
133848b8605Smrg#define IS_INF_OR_NAN(x)        (!isfinite(x))
134848b8605Smrg#elif defined(finite)
135848b8605Smrg#define IS_INF_OR_NAN(x)        (!finite(x))
136848b8605Smrg#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
137848b8605Smrg#define IS_INF_OR_NAN(x)        (!isfinite(x))
138848b8605Smrg#else
139848b8605Smrg#define IS_INF_OR_NAN(x)        (!finite(x))
140848b8605Smrg#endif
141848b8605Smrg
142848b8605Smrg
143848b8605Smrg/**
144848b8605Smrg * Convert float to int by rounding to nearest integer, away from zero.
145848b8605Smrg */
146848b8605Smrgstatic inline int IROUND(float f)
147848b8605Smrg{
148848b8605Smrg   return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
149848b8605Smrg}
150848b8605Smrg
151b8e80941Smrg/**
152b8e80941Smrg * Convert double to int by rounding to nearest integer, away from zero.
153b8e80941Smrg */
154b8e80941Smrgstatic inline int IROUNDD(double d)
155b8e80941Smrg{
156b8e80941Smrg   return (int) ((d >= 0.0) ? (d + 0.5) : (d - 0.5));
157b8e80941Smrg}
158848b8605Smrg
159848b8605Smrg/**
160848b8605Smrg * Convert float to int64 by rounding to nearest integer.
161848b8605Smrg */
162848b8605Smrgstatic inline GLint64 IROUND64(float f)
163848b8605Smrg{
164848b8605Smrg   return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
165848b8605Smrg}
166848b8605Smrg
167848b8605Smrg
168848b8605Smrg/**
169848b8605Smrg * Convert positive float to int by rounding to nearest integer.
170848b8605Smrg */
171848b8605Smrgstatic inline int IROUND_POS(float f)
172848b8605Smrg{
173848b8605Smrg   assert(f >= 0.0F);
174848b8605Smrg   return (int) (f + 0.5F);
175848b8605Smrg}
176848b8605Smrg
177848b8605Smrg/** Return (as an integer) floor of float */
178848b8605Smrgstatic inline int IFLOOR(float f)
179848b8605Smrg{
180848b8605Smrg#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
181848b8605Smrg   /*
182848b8605Smrg    * IEEE floor for computers that round to nearest or even.
183848b8605Smrg    * 'f' must be between -4194304 and 4194303.
184848b8605Smrg    * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
185848b8605Smrg    * but uses some IEEE specific tricks for better speed.
186848b8605Smrg    * Contributed by Josh Vanderhoof
187848b8605Smrg    */
188848b8605Smrg   int ai, bi;
189848b8605Smrg   double af, bf;
190848b8605Smrg   af = (3 << 22) + 0.5 + (double)f;
191848b8605Smrg   bf = (3 << 22) + 0.5 - (double)f;
192848b8605Smrg   /* GCC generates an extra fstp/fld without this. */
193848b8605Smrg   __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
194848b8605Smrg   __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
195848b8605Smrg   return (ai - bi) >> 1;
196848b8605Smrg#else
197848b8605Smrg   int ai, bi;
198848b8605Smrg   double af, bf;
199848b8605Smrg   fi_type u;
200848b8605Smrg   af = (3 << 22) + 0.5 + (double)f;
201848b8605Smrg   bf = (3 << 22) + 0.5 - (double)f;
202848b8605Smrg   u.f = (float) af;  ai = u.i;
203848b8605Smrg   u.f = (float) bf;  bi = u.i;
204848b8605Smrg   return (ai - bi) >> 1;
205848b8605Smrg#endif
206848b8605Smrg}
207848b8605Smrg
208848b8605Smrg
209848b8605Smrg/**
210848b8605Smrg * Is x a power of two?
211848b8605Smrg */
212848b8605Smrgstatic inline int
213848b8605Smrg_mesa_is_pow_two(int x)
214848b8605Smrg{
215848b8605Smrg   return !(x & (x - 1));
216848b8605Smrg}
217848b8605Smrg
218848b8605Smrg/**
219848b8605Smrg * Round given integer to next higer power of two
220848b8605Smrg * If X is zero result is undefined.
221848b8605Smrg *
222848b8605Smrg * Source for the fallback implementation is
223848b8605Smrg * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
224848b8605Smrg * http://graphics.stanford.edu/~seander/bithacks.html
225848b8605Smrg *
226848b8605Smrg * When using builtin function have to do some work
227848b8605Smrg * for case when passed values 1 to prevent hiting
228848b8605Smrg * undefined result from __builtin_clz. Undefined
229848b8605Smrg * results would be different depending on optimization
230848b8605Smrg * level used for build.
231848b8605Smrg */
232848b8605Smrgstatic inline int32_t
233848b8605Smrg_mesa_next_pow_two_32(uint32_t x)
234848b8605Smrg{
235b8e80941Smrg#ifdef HAVE___BUILTIN_CLZ
236848b8605Smrg	uint32_t y = (x != 1);
237848b8605Smrg	return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
238848b8605Smrg#else
239848b8605Smrg	x--;
240848b8605Smrg	x |= x >> 1;
241848b8605Smrg	x |= x >> 2;
242848b8605Smrg	x |= x >> 4;
243848b8605Smrg	x |= x >> 8;
244848b8605Smrg	x |= x >> 16;
245848b8605Smrg	x++;
246848b8605Smrg	return x;
247848b8605Smrg#endif
248848b8605Smrg}
249848b8605Smrg
250848b8605Smrgstatic inline int64_t
251848b8605Smrg_mesa_next_pow_two_64(uint64_t x)
252848b8605Smrg{
253b8e80941Smrg#ifdef HAVE___BUILTIN_CLZLL
254848b8605Smrg	uint64_t y = (x != 1);
255b8e80941Smrg	STATIC_ASSERT(sizeof(x) == sizeof(long long));
256b8e80941Smrg	return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
257848b8605Smrg#else
258848b8605Smrg	x--;
259848b8605Smrg	x |= x >> 1;
260848b8605Smrg	x |= x >> 2;
261848b8605Smrg	x |= x >> 4;
262848b8605Smrg	x |= x >> 8;
263848b8605Smrg	x |= x >> 16;
264848b8605Smrg	x |= x >> 32;
265848b8605Smrg	x++;
266848b8605Smrg	return x;
267848b8605Smrg#endif
268848b8605Smrg}
269848b8605Smrg
270848b8605Smrg
271848b8605Smrg/*
272848b8605Smrg * Returns the floor form of binary logarithm for a 32-bit integer.
273848b8605Smrg */
274848b8605Smrgstatic inline GLuint
275848b8605Smrg_mesa_logbase2(GLuint n)
276848b8605Smrg{
277b8e80941Smrg#ifdef HAVE___BUILTIN_CLZ
278848b8605Smrg   return (31 - __builtin_clz(n | 1));
279848b8605Smrg#else
280848b8605Smrg   GLuint pos = 0;
281848b8605Smrg   if (n >= 1<<16) { n >>= 16; pos += 16; }
282848b8605Smrg   if (n >= 1<< 8) { n >>=  8; pos +=  8; }
283848b8605Smrg   if (n >= 1<< 4) { n >>=  4; pos +=  4; }
284848b8605Smrg   if (n >= 1<< 2) { n >>=  2; pos +=  2; }
285848b8605Smrg   if (n >= 1<< 1) {           pos +=  1; }
286848b8605Smrg   return pos;
287848b8605Smrg#endif
288848b8605Smrg}
289848b8605Smrg
290848b8605Smrg
291848b8605Smrg/**
292848b8605Smrg * Return 1 if this is a little endian machine, 0 if big endian.
293848b8605Smrg */
294848b8605Smrgstatic inline GLboolean
295848b8605Smrg_mesa_little_endian(void)
296848b8605Smrg{
297848b8605Smrg   const GLuint ui = 1; /* intentionally not static */
298848b8605Smrg   return *((const GLubyte *) &ui);
299848b8605Smrg}
300848b8605Smrg
301848b8605Smrg
302848b8605Smrg
303848b8605Smrg/**********************************************************************
304848b8605Smrg * Functions
305848b8605Smrg */
306848b8605Smrg
307848b8605Smrgextern void *
308848b8605Smrg_mesa_align_malloc( size_t bytes, unsigned long alignment );
309848b8605Smrg
310848b8605Smrgextern void *
311848b8605Smrg_mesa_align_calloc( size_t bytes, unsigned long alignment );
312848b8605Smrg
313848b8605Smrgextern void
314848b8605Smrg_mesa_align_free( void *ptr );
315848b8605Smrg
316848b8605Smrgextern void *
317848b8605Smrg_mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
318848b8605Smrg                    unsigned long alignment);
319848b8605Smrg
320848b8605Smrgextern int
321848b8605Smrg_mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
322848b8605Smrg
323848b8605Smrgextern int
324848b8605Smrg_mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
325848b8605Smrg
326848b8605Smrg
327b8e80941Smrg#if defined(_WIN32) && !defined(strtok_r)
328b8e80941Smrg#define strtok_r strtok_s
329848b8605Smrg#endif
330848b8605Smrg
331848b8605Smrg#ifdef __cplusplus
332848b8605Smrg}
333848b8605Smrg#endif
334848b8605Smrg
335848b8605Smrg
336848b8605Smrg#endif /* IMPORTS_H */
337