1/*
2 * Mesa 3-D graphics library
3 * Version:  7.5
4 *
5 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN 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 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 "compiler.h"
40#include "glheader.h"
41
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47
48/**********************************************************************/
49/** Memory macros */
50/*@{*/
51
52/** Allocate \p BYTES bytes */
53#define MALLOC(BYTES)      malloc(BYTES)
54/** Allocate and zero \p BYTES bytes */
55#define CALLOC(BYTES)      calloc(1, BYTES)
56/** Allocate a structure of type \p T */
57#define MALLOC_STRUCT(T)   (struct T *) malloc(sizeof(struct T))
58/** Allocate and zero a structure of type \p T */
59#define CALLOC_STRUCT(T)   (struct T *) calloc(1, sizeof(struct T))
60/** Free memory */
61#define FREE(PTR)          free(PTR)
62
63/*@}*/
64
65
66/*
67 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
68 * as offsets into buffer stores.  Since the vertex array pointer and
69 * buffer store pointer are both pointers and we need to add them, we use
70 * this macro.
71 * Both pointers/offsets are expressed in bytes.
72 */
73#define ADD_POINTERS(A, B)  ( (GLubyte *) (A) + (uintptr_t) (B) )
74
75
76/**
77 * Sometimes we treat GLfloats as GLints.  On x86 systems, moving a float
78 * as a int (thereby using integer registers instead of FP registers) is
79 * a performance win.  Typically, this can be done with ordinary casts.
80 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
81 * these casts generate warnings.
82 * The following union typedef is used to solve that.
83 */
84typedef union { GLfloat f; GLint i; } fi_type;
85
86
87
88/**********************************************************************
89 * Math macros
90 */
91
92#define MAX_GLUSHORT	0xffff
93#define MAX_GLUINT	0xffffffff
94
95/* Degrees to radians conversion: */
96#define DEG2RAD (M_PI/180.0)
97
98
99/***
100 *** SQRTF: single-precision square root
101 ***/
102#if 0 /* _mesa_sqrtf() not accurate enough - temporarily disabled */
103#  define SQRTF(X)  _mesa_sqrtf(X)
104#else
105#  define SQRTF(X)  (float) sqrt((float) (X))
106#endif
107
108
109/***
110 *** INV_SQRTF: single-precision inverse square root
111 ***/
112#if 0
113#define INV_SQRTF(X) _mesa_inv_sqrt(X)
114#else
115#define INV_SQRTF(X) (1.0F / SQRTF(X))  /* this is faster on a P4 */
116#endif
117
118
119/**
120 * \name Work-arounds for platforms that lack C99 math functions
121 */
122/*@{*/
123#if (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600)) && !defined(_ISOC99_SOURCE) \
124   && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)) \
125   && (!defined(_MSC_VER) || (_MSC_VER < 1400))
126/*#define acosf(f) ((float) acos(f))
127#define asinf(f) ((float) asin(f))
128#define atan2f(x,y) ((float) atan2(x,y))
129#define atanf(f) ((float) atan(f))
130#define cielf(f) ((float) ciel(f))
131#define cosf(f) ((float) cos(f))
132#define coshf(f) ((float) cosh(f))
133#define expf(f) ((float) exp(f))
134#define exp2f(f) ((float) exp2(f))
135#define floorf(f) ((float) floor(f))
136#define logf(f) ((float) log(f))
137#define log2f(f) ((float) log2(f))
138#define powf(x,y) ((float) pow(x,y))
139#define sinf(f) ((float) sin(f))
140#define sinhf(f) ((float) sinh(f))
141#define sqrtf(f) ((float) sqrt(f))
142#define tanf(f) ((float) tan(f))
143#define tanhf(f) ((float) tanh(f))
144#define acoshf(f) ((float) acosh(f))
145#define asinhf(f) ((float) asinh(f))
146#define atanhf(f) ((float) atanh(f))*/
147#endif
148
149#if defined(_MSC_VER)
150static INLINE float truncf(float x) { return x < 0.0f ? ceilf(x) : floorf(x); }
151static INLINE float exp2f(float x) { return powf(2.0f, x); }
152static INLINE float log2f(float x) { return logf(x) * 1.442695041f; }
153static INLINE float asinhf(float x) { return logf(x + sqrtf(x * x + 1.0f)); }
154static INLINE float acoshf(float x) { return logf(x + sqrtf(x * x - 1.0f)); }
155static INLINE float atanhf(float x) { return (logf(1.0f + x) - logf(1.0f - x)) / 2.0f; }
156static INLINE int isblank(int ch) { return ch == ' ' || ch == '\t'; }
157#define strtoll(p, e, b) _strtoi64(p, e, b)
158#endif
159/*@}*/
160
161/***
162 *** LOG2: Log base 2 of float
163 ***/
164#ifdef USE_IEEE
165#if 0
166/* This is pretty fast, but not accurate enough (only 2 fractional bits).
167 * Based on code from http://www.stereopsis.com/log2.html
168 */
169static INLINE GLfloat LOG2(GLfloat x)
170{
171   const GLfloat y = x * x * x * x;
172   const GLuint ix = *((GLuint *) &y);
173   const GLuint exp = (ix >> 23) & 0xFF;
174   const GLint log2 = ((GLint) exp) - 127;
175   return (GLfloat) log2 * (1.0 / 4.0);  /* 4, because of x^4 above */
176}
177#endif
178/* Pretty fast, and accurate.
179 * Based on code from http://www.flipcode.com/totd/
180 */
181static INLINE GLfloat LOG2(GLfloat val)
182{
183   fi_type num;
184   GLint log_2;
185   num.f = val;
186   log_2 = ((num.i >> 23) & 255) - 128;
187   num.i &= ~(255 << 23);
188   num.i += 127 << 23;
189   num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
190   return num.f + log_2;
191}
192#else
193/*
194 * NOTE: log_base_2(x) = log(x) / log(2)
195 * NOTE: 1.442695 = 1/log(2).
196 */
197#define LOG2(x)  ((GLfloat) (log(x) * 1.442695F))
198#endif
199
200
201/***
202 *** IS_INF_OR_NAN: test if float is infinite or NaN
203 ***/
204#ifdef USE_IEEE
205static INLINE int IS_INF_OR_NAN( float x )
206{
207   fi_type tmp;
208   tmp.f = x;
209   return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
210}
211#elif defined(isfinite)
212#define IS_INF_OR_NAN(x)        (!isfinite(x))
213#elif defined(finite)
214#define IS_INF_OR_NAN(x)        (!finite(x))
215#elif defined(__VMS)
216#define IS_INF_OR_NAN(x)        (!finite(x))
217#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
218#define IS_INF_OR_NAN(x)        (!isfinite(x))
219#else
220#define IS_INF_OR_NAN(x)        (!finite(x))
221#endif
222
223
224/***
225 *** IS_NEGATIVE: test if float is negative
226 ***/
227#if defined(USE_IEEE)
228static INLINE int GET_FLOAT_BITS( float x )
229{
230   fi_type fi;
231   fi.f = x;
232   return fi.i;
233}
234#define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) < 0)
235#else
236#define IS_NEGATIVE(x) (x < 0.0F)
237#endif
238
239
240/***
241 *** DIFFERENT_SIGNS: test if two floats have opposite signs
242 ***/
243#if defined(USE_IEEE)
244#define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
245#else
246/* Could just use (x*y<0) except for the flatshading requirements.
247 * Maybe there's a better way?
248 */
249#define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
250#endif
251
252
253/***
254 *** CEILF: ceiling of float
255 *** FLOORF: floor of float
256 *** FABSF: absolute value of float
257 *** LOGF: the natural logarithm (base e) of the value
258 *** EXPF: raise e to the value
259 *** LDEXPF: multiply value by an integral power of two
260 *** FREXPF: extract mantissa and exponent from value
261 ***/
262#if defined(__gnu_linux__)
263/* C99 functions */
264#define CEILF(x)   ceilf(x)
265#define FLOORF(x)  floorf(x)
266#define FABSF(x)   fabsf(x)
267#define LOGF(x)    logf(x)
268#define EXPF(x)    expf(x)
269#define LDEXPF(x,y)  ldexpf(x,y)
270#define FREXPF(x,y)  frexpf(x,y)
271#else
272#define CEILF(x)   ((GLfloat) ceil(x))
273#define FLOORF(x)  ((GLfloat) floor(x))
274#define FABSF(x)   ((GLfloat) fabs(x))
275#define LOGF(x)    ((GLfloat) log(x))
276#define EXPF(x)    ((GLfloat) exp(x))
277#define LDEXPF(x,y)  ((GLfloat) ldexp(x,y))
278#define FREXPF(x,y)  ((GLfloat) frexp(x,y))
279#endif
280
281
282/***
283 *** IROUND: return (as an integer) float rounded to nearest integer
284 ***/
285#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
286static INLINE int iround(float f)
287{
288   int r;
289   __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
290   return r;
291}
292#define IROUND(x)  iround(x)
293#elif defined(USE_X86_ASM) && defined(_MSC_VER)
294static INLINE int iround(float f)
295{
296   int r;
297   _asm {
298	 fld f
299	 fistp r
300	}
301   return r;
302}
303#define IROUND(x)  iround(x)
304#elif defined(__WATCOMC__) && defined(__386__)
305long iround(float f);
306#pragma aux iround =                    \
307	"push   eax"                        \
308	"fistp  dword ptr [esp]"            \
309	"pop    eax"                        \
310	parm [8087]                         \
311	value [eax]                         \
312	modify exact [eax];
313#define IROUND(x)  iround(x)
314#else
315#define IROUND(f)  ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
316#endif
317
318#define IROUND64(f)  ((GLint64) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
319
320/***
321 *** IROUND_POS: return (as an integer) positive float rounded to nearest int
322 ***/
323#ifdef DEBUG
324#define IROUND_POS(f) (assert((f) >= 0.0F), IROUND(f))
325#else
326#define IROUND_POS(f) (IROUND(f))
327#endif
328
329
330/***
331 *** IFLOOR: return (as an integer) floor of float
332 ***/
333#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
334/*
335 * IEEE floor for computers that round to nearest or even.
336 * 'f' must be between -4194304 and 4194303.
337 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
338 * but uses some IEEE specific tricks for better speed.
339 * Contributed by Josh Vanderhoof
340 */
341static INLINE int ifloor(float f)
342{
343   int ai, bi;
344   double af, bf;
345   af = (3 << 22) + 0.5 + (double)f;
346   bf = (3 << 22) + 0.5 - (double)f;
347   /* GCC generates an extra fstp/fld without this. */
348   __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
349   __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
350   return (ai - bi) >> 1;
351}
352#define IFLOOR(x)  ifloor(x)
353#elif defined(USE_IEEE)
354static INLINE int ifloor(float f)
355{
356   int ai, bi;
357   double af, bf;
358   fi_type u;
359
360   af = (3 << 22) + 0.5 + (double)f;
361   bf = (3 << 22) + 0.5 - (double)f;
362   u.f = (float) af;  ai = u.i;
363   u.f = (float) bf;  bi = u.i;
364   return (ai - bi) >> 1;
365}
366#define IFLOOR(x)  ifloor(x)
367#else
368static INLINE int ifloor(float f)
369{
370   int i = IROUND(f);
371   return (i > f) ? i - 1 : i;
372}
373#define IFLOOR(x)  ifloor(x)
374#endif
375
376
377/***
378 *** ICEIL: return (as an integer) ceiling of float
379 ***/
380#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
381/*
382 * IEEE ceil for computers that round to nearest or even.
383 * 'f' must be between -4194304 and 4194303.
384 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
385 * but uses some IEEE specific tricks for better speed.
386 * Contributed by Josh Vanderhoof
387 */
388static INLINE int iceil(float f)
389{
390   int ai, bi;
391   double af, bf;
392   af = (3 << 22) + 0.5 + (double)f;
393   bf = (3 << 22) + 0.5 - (double)f;
394   /* GCC generates an extra fstp/fld without this. */
395   __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
396   __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
397   return (ai - bi + 1) >> 1;
398}
399#define ICEIL(x)  iceil(x)
400#elif defined(USE_IEEE)
401static INLINE int iceil(float f)
402{
403   int ai, bi;
404   double af, bf;
405   fi_type u;
406   af = (3 << 22) + 0.5 + (double)f;
407   bf = (3 << 22) + 0.5 - (double)f;
408   u.f = (float) af; ai = u.i;
409   u.f = (float) bf; bi = u.i;
410   return (ai - bi + 1) >> 1;
411}
412#define ICEIL(x)  iceil(x)
413#else
414static INLINE int iceil(float f)
415{
416   int i = IROUND(f);
417   return (i < f) ? i + 1 : i;
418}
419#define ICEIL(x)  iceil(x)
420#endif
421
422
423/**
424 * Is x a power of two?
425 */
426static INLINE int
427_mesa_is_pow_two(int x)
428{
429   return !(x & (x - 1));
430}
431
432/**
433 * Round given integer to next higer power of two
434 * If X is zero result is undefined.
435 *
436 * Source for the fallback implementation is
437 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
438 * http://graphics.stanford.edu/~seander/bithacks.html
439 *
440 * When using builtin function have to do some work
441 * for case when passed values 1 to prevent hiting
442 * undefined result from __builtin_clz. Undefined
443 * results would be different depending on optimization
444 * level used for build.
445 */
446static INLINE int32_t
447_mesa_next_pow_two_32(uint32_t x)
448{
449#if defined(__GNUC__) && \
450	((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
451	uint32_t y = (x != 1);
452	return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
453#else
454	x--;
455	x |= x >> 1;
456	x |= x >> 2;
457	x |= x >> 4;
458	x |= x >> 8;
459	x |= x >> 16;
460	x++;
461	return x;
462#endif
463}
464
465static INLINE int64_t
466_mesa_next_pow_two_64(uint64_t x)
467{
468#if defined(__GNUC__) && \
469	((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
470	uint64_t y = (x != 1);
471	if (sizeof(x) == sizeof(long))
472		return (1 + y) << ((__builtin_clzl(x - y) ^ 63));
473	else
474		return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
475#else
476	x--;
477	x |= x >> 1;
478	x |= x >> 2;
479	x |= x >> 4;
480	x |= x >> 8;
481	x |= x >> 16;
482	x |= x >> 32;
483	x++;
484	return x;
485#endif
486}
487
488
489/**
490 * Return 1 if this is a little endian machine, 0 if big endian.
491 */
492static INLINE GLboolean
493_mesa_little_endian(void)
494{
495   const GLuint ui = 1; /* intentionally not static */
496   return *((const GLubyte *) &ui);
497}
498
499
500
501/**********************************************************************
502 * Functions
503 */
504
505extern void *
506_mesa_align_malloc( size_t bytes, unsigned long alignment );
507
508extern void *
509_mesa_align_calloc( size_t bytes, unsigned long alignment );
510
511extern void
512_mesa_align_free( void *ptr );
513
514extern void *
515_mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
516                    unsigned long alignment);
517
518extern void *
519_mesa_exec_malloc( GLuint size );
520
521extern void
522_mesa_exec_free( void *addr );
523
524extern void *
525_mesa_realloc( void *oldBuffer, size_t oldSize, size_t newSize );
526
527extern void
528_mesa_memset16( unsigned short *dst, unsigned short val, size_t n );
529
530extern double
531_mesa_sqrtd(double x);
532
533extern float
534_mesa_sqrtf(float x);
535
536extern float
537_mesa_inv_sqrtf(float x);
538
539extern void
540_mesa_init_sqrt_table(void);
541
542extern int
543_mesa_ffs(int32_t i);
544
545extern int
546_mesa_ffsll(int64_t i);
547
548extern unsigned int
549_mesa_bitcount(unsigned int n);
550
551extern GLhalfARB
552_mesa_float_to_half(float f);
553
554extern float
555_mesa_half_to_float(GLhalfARB h);
556
557
558extern void *
559_mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
560               int (*compar)(const void *, const void *) );
561
562extern char *
563_mesa_getenv( const char *var );
564
565extern char *
566_mesa_strdup( const char *s );
567
568extern float
569_mesa_strtof( const char *s, char **end );
570
571extern unsigned int
572_mesa_str_checksum(const char *str);
573
574extern int
575_mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
576
577struct gl_context;
578
579extern void
580_mesa_warning( struct gl_context *gc, const char *fmtString, ... ) PRINTFLIKE(2, 3);
581
582extern void
583_mesa_problem( const struct gl_context *ctx, const char *fmtString, ... ) PRINTFLIKE(2, 3);
584
585extern void
586_mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... ) PRINTFLIKE(3, 4);
587
588extern void
589_mesa_debug( const struct gl_context *ctx, const char *fmtString, ... ) PRINTFLIKE(2, 3);
590
591
592#if defined(_MSC_VER) && !defined(snprintf)
593#define snprintf _snprintf
594#endif
595
596
597#ifdef __cplusplus
598}
599#endif
600
601
602#endif /* IMPORTS_H */
603