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 "errors.h" 45 #include "util/bitscan.h" 46 47 #ifdef __cplusplus 48 extern "C" { 49 #endif 50 51 52 /**********************************************************************/ 53 /** Memory macros */ 54 /*@{*/ 55 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 61 /*@}*/ 62 63 64 /* 65 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers 66 * as offsets into buffer stores. Since the vertex array pointer and 67 * buffer store pointer are both pointers and we need to add them, we use 68 * this macro. 69 * Both pointers/offsets are expressed in bytes. 70 */ 71 #define ADD_POINTERS(A, B) ( (GLubyte *) (A) + (uintptr_t) (B) ) 72 73 74 /** 75 * Sometimes we treat GLfloats as GLints. On x86 systems, moving a float 76 * as an int (thereby using integer registers instead of FP registers) is 77 * a performance win. Typically, this can be done with ordinary casts. 78 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0) 79 * these casts generate warnings. 80 * The following union typedef is used to solve that. 81 */ 82 typedef union { GLfloat f; GLint i; GLuint u; } fi_type; 83 84 85 86 #if defined(_MSC_VER) 87 #define strcasecmp(s1, s2) _stricmp(s1, s2) 88 #endif 89 /*@}*/ 90 91 92 /*** 93 *** LOG2: Log base 2 of float 94 ***/ 95 static inline GLfloat LOG2(GLfloat x) 96 { 97 #if 0 98 /* This is pretty fast, but not accurate enough (only 2 fractional bits). 99 * Based on code from http://www.stereopsis.com/log2.html 100 */ 101 const GLfloat y = x * x * x * x; 102 const GLuint ix = *((GLuint *) &y); 103 const GLuint exp = (ix >> 23) & 0xFF; 104 const GLint log2 = ((GLint) exp) - 127; 105 return (GLfloat) log2 * (1.0 / 4.0); /* 4, because of x^4 above */ 106 #endif 107 /* Pretty fast, and accurate. 108 * Based on code from http://www.flipcode.com/totd/ 109 */ 110 fi_type num; 111 GLint log_2; 112 num.f = x; 113 log_2 = ((num.i >> 23) & 255) - 128; 114 num.i &= ~(255 << 23); 115 num.i += 127 << 23; 116 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3; 117 return num.f + log_2; 118 } 119 120 121 122 /** 123 * finite macro. 124 */ 125 #if defined(_MSC_VER) 126 # define finite _finite 127 #endif 128 129 130 /*** 131 *** IS_INF_OR_NAN: test if float is infinite or NaN 132 ***/ 133 #if defined(isfinite) 134 #define IS_INF_OR_NAN(x) (!isfinite(x)) 135 #elif defined(finite) 136 #define IS_INF_OR_NAN(x) (!finite(x)) 137 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 138 #define IS_INF_OR_NAN(x) (!isfinite(x)) 139 #else 140 #define IS_INF_OR_NAN(x) (!finite(x)) 141 #endif 142 143 144 /** 145 * Convert float to int by rounding to nearest integer, away from zero. 146 */ 147 static inline int IROUND(float f) 148 { 149 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F)); 150 } 151 152 /** 153 * Convert double to int by rounding to nearest integer, away from zero. 154 */ 155 static inline int IROUNDD(double d) 156 { 157 return (int) ((d >= 0.0) ? (d + 0.5) : (d - 0.5)); 158 } 159 160 /** 161 * Convert float to int64 by rounding to nearest integer. 162 */ 163 static inline GLint64 IROUND64(float f) 164 { 165 return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F)); 166 } 167 168 169 /** 170 * Convert positive float to int by rounding to nearest integer. 171 */ 172 static inline int IROUND_POS(float f) 173 { 174 assert(f >= 0.0F); 175 return (int) (f + 0.5F); 176 } 177 178 /** Return (as an integer) floor of float */ 179 static inline int IFLOOR(float f) 180 { 181 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) 182 /* 183 * IEEE floor for computers that round to nearest or even. 184 * 'f' must be between -4194304 and 4194303. 185 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1", 186 * but uses some IEEE specific tricks for better speed. 187 * Contributed by Josh Vanderhoof 188 */ 189 int ai, bi; 190 double af, bf; 191 af = (3 << 22) + 0.5 + (double)f; 192 bf = (3 << 22) + 0.5 - (double)f; 193 /* GCC generates an extra fstp/fld without this. */ 194 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st"); 195 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st"); 196 return (ai - bi) >> 1; 197 #else 198 int ai, bi; 199 double af, bf; 200 fi_type u; 201 af = (3 << 22) + 0.5 + (double)f; 202 bf = (3 << 22) + 0.5 - (double)f; 203 u.f = (float) af; ai = u.i; 204 u.f = (float) bf; bi = u.i; 205 return (ai - bi) >> 1; 206 #endif 207 } 208 209 210 /** 211 * Is x a power of two? 212 */ 213 static inline int 214 _mesa_is_pow_two(int x) 215 { 216 return !(x & (x - 1)); 217 } 218 219 /** 220 * Round given integer to next higer power of two 221 * If X is zero result is undefined. 222 * 223 * Source for the fallback implementation is 224 * Sean Eron Anderson's webpage "Bit Twiddling Hacks" 225 * http://graphics.stanford.edu/~seander/bithacks.html 226 * 227 * When using builtin function have to do some work 228 * for case when passed values 1 to prevent hiting 229 * undefined result from __builtin_clz. Undefined 230 * results would be different depending on optimization 231 * level used for build. 232 */ 233 static inline int32_t 234 _mesa_next_pow_two_32(uint32_t x) 235 { 236 #ifdef HAVE___BUILTIN_CLZ 237 uint32_t y = (x != 1); 238 return (1 + y) << ((__builtin_clz(x - y) ^ 31) ); 239 #else 240 x--; 241 x |= x >> 1; 242 x |= x >> 2; 243 x |= x >> 4; 244 x |= x >> 8; 245 x |= x >> 16; 246 x++; 247 return x; 248 #endif 249 } 250 251 static inline int64_t 252 _mesa_next_pow_two_64(uint64_t x) 253 { 254 #ifdef HAVE___BUILTIN_CLZLL 255 uint64_t y = (x != 1); 256 STATIC_ASSERT(sizeof(x) == sizeof(long long)); 257 return (1 + y) << ((__builtin_clzll(x - y) ^ 63)); 258 #else 259 x--; 260 x |= x >> 1; 261 x |= x >> 2; 262 x |= x >> 4; 263 x |= x >> 8; 264 x |= x >> 16; 265 x |= x >> 32; 266 x++; 267 return x; 268 #endif 269 } 270 271 272 /* 273 * Returns the floor form of binary logarithm for a 32-bit integer. 274 */ 275 static inline GLuint 276 _mesa_logbase2(GLuint n) 277 { 278 #ifdef HAVE___BUILTIN_CLZ 279 return (31 - __builtin_clz(n | 1)); 280 #else 281 GLuint pos = 0; 282 if (n >= 1<<16) { n >>= 16; pos += 16; } 283 if (n >= 1<< 8) { n >>= 8; pos += 8; } 284 if (n >= 1<< 4) { n >>= 4; pos += 4; } 285 if (n >= 1<< 2) { n >>= 2; pos += 2; } 286 if (n >= 1<< 1) { pos += 1; } 287 return pos; 288 #endif 289 } 290 291 292 /** 293 * Return 1 if this is a little endian machine, 0 if big endian. 294 */ 295 static inline GLboolean 296 _mesa_little_endian(void) 297 { 298 const GLuint ui = 1; /* intentionally not static */ 299 return *((const GLubyte *) &ui); 300 } 301 302 303 304 /********************************************************************** 305 * Functions 306 */ 307 308 extern void * 309 _mesa_align_malloc( size_t bytes, unsigned long alignment ); 310 311 extern void * 312 _mesa_align_calloc( size_t bytes, unsigned long alignment ); 313 314 extern void 315 _mesa_align_free( void *ptr ); 316 317 extern void * 318 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize, 319 unsigned long alignment); 320 321 extern void * 322 _mesa_exec_malloc( GLuint size ); 323 324 extern void 325 _mesa_exec_free( void *addr ); 326 327 328 #ifdef HAVE___BUILTIN_POPCOUNT 329 #define _mesa_bitcount(i) __builtin_popcount(i) 330 #else 331 extern unsigned int 332 _mesa_bitcount(unsigned int n); 333 #endif 334 335 #ifdef HAVE___BUILTIN_POPCOUNTLL 336 #define _mesa_bitcount_64(i) __builtin_popcountll(i) 337 #else 338 extern unsigned int 339 _mesa_bitcount_64(uint64_t n); 340 #endif 341 342 343 static inline bool 344 _mesa_half_is_negative(GLhalfARB h) 345 { 346 return h & 0x8000; 347 } 348 349 extern int 350 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4); 351 352 extern int 353 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg); 354 355 356 #if defined(_MSC_VER) && !defined(snprintf) 357 #define snprintf _snprintf 358 #endif 359 360 #if defined(_WIN32) && !defined(strtok_r) 361 #define strtok_r strtok_s 362 #endif 363 364 #ifdef __cplusplus 365 } 366 #endif 367 368 369 #endif /* IMPORTS_H */ 370