replace IFLOOR with util_ifloor
[mesa.git] / src / util / imports.h
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 "util/compiler.h"
43 #include "util/bitscan.h"
44 #include "util/u_memory.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 /*
52 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
53 * as offsets into buffer stores. Since the vertex array pointer and
54 * buffer store pointer are both pointers and we need to add them, we use
55 * this macro.
56 * Both pointers/offsets are expressed in bytes.
57 */
58 #define ADD_POINTERS(A, B) ( (uint8_t *) (A) + (uintptr_t) (B) )
59
60
61 /**
62 * Sometimes we treat floats as ints. On x86 systems, moving a float
63 * as an int (thereby using integer registers instead of FP registers) is
64 * a performance win. Typically, this can be done with ordinary casts.
65 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
66 * these casts generate warnings.
67 * The following union typedef is used to solve that.
68 */
69 typedef union { float f; int i; unsigned u; } fi_type;
70
71
72
73 /*@}*/
74
75
76 /**
77 * finite macro.
78 */
79 #if defined(_MSC_VER)
80 # define finite _finite
81 #endif
82
83
84 /***
85 *** IS_INF_OR_NAN: test if float is infinite or NaN
86 ***/
87 #if defined(isfinite)
88 #define IS_INF_OR_NAN(x) (!isfinite(x))
89 #elif defined(finite)
90 #define IS_INF_OR_NAN(x) (!finite(x))
91 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
92 #define IS_INF_OR_NAN(x) (!isfinite(x))
93 #else
94 #define IS_INF_OR_NAN(x) (!finite(x))
95 #endif
96
97
98 /**
99 * Convert float to int by rounding to nearest integer, away from zero.
100 */
101 static inline int IROUND(float f)
102 {
103 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
104 }
105
106 /**
107 * Convert double to int by rounding to nearest integer, away from zero.
108 */
109 static inline int IROUNDD(double d)
110 {
111 return (int) ((d >= 0.0) ? (d + 0.5) : (d - 0.5));
112 }
113
114 /**
115 * Convert float to int64 by rounding to nearest integer.
116 */
117 static inline int64_t IROUND64(float f)
118 {
119 return (int64_t) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
120 }
121
122
123 /**
124 * Convert positive float to int by rounding to nearest integer.
125 */
126 static inline int IROUND_POS(float f)
127 {
128 assert(f >= 0.0F);
129 return (int) (f + 0.5F);
130 }
131
132
133 /**********************************************************************
134 * Functions
135 */
136
137 extern void *
138 _mesa_align_malloc( size_t bytes, unsigned long alignment );
139
140 extern void *
141 _mesa_align_calloc( size_t bytes, unsigned long alignment );
142
143 extern void
144 _mesa_align_free( void *ptr );
145
146 extern void *
147 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
148 unsigned long alignment);
149
150 extern int
151 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
152
153 extern int
154 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
155
156
157 #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
158 #define strtok_r strtok_s
159 #endif
160
161 #ifdef __cplusplus
162 }
163 #endif
164
165
166 #endif /* IMPORTS_H */