• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * 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
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef U_POINTER_H
29 #define U_POINTER_H
30 
31 #include "pipe/p_compiler.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 static INLINE intptr_t
pointer_to_intptr(const void * p)38 pointer_to_intptr( const void *p )
39 {
40    union {
41       const void *p;
42       intptr_t i;
43    } pi;
44    pi.p = p;
45    return pi.i;
46 }
47 
48 static INLINE void *
intptr_to_pointer(intptr_t i)49 intptr_to_pointer( intptr_t i )
50 {
51    union {
52       void *p;
53       intptr_t i;
54    } pi;
55    pi.i = i;
56    return pi.p;
57 }
58 
59 static INLINE uintptr_t
pointer_to_uintptr(const void * ptr)60 pointer_to_uintptr( const void *ptr )
61 {
62    union {
63       const void *p;
64       uintptr_t u;
65    } pu;
66    pu.p = ptr;
67    return pu.u;
68 }
69 
70 static INLINE void *
uintptr_to_pointer(uintptr_t u)71 uintptr_to_pointer( uintptr_t u )
72 {
73    union {
74       void *p;
75       uintptr_t u;
76    } pu;
77    pu.u = u;
78    return pu.p;
79 }
80 
81 /**
82  * Return a pointer aligned to next multiple of N bytes.
83  */
84 static INLINE void *
align_pointer(const void * unaligned,uintptr_t alignment)85 align_pointer( const void *unaligned, uintptr_t alignment )
86 {
87    uintptr_t aligned = (pointer_to_uintptr( unaligned ) + alignment - 1) & ~(alignment - 1);
88    return uintptr_to_pointer( aligned );
89 }
90 
91 
92 /**
93  * Return a pointer aligned to next multiple of 16 bytes.
94  */
95 static INLINE void *
align16(void * unaligned)96 align16( void *unaligned )
97 {
98    return align_pointer( unaligned, 16 );
99 }
100 
101 typedef void (*func_pointer)(void);
102 
103 static INLINE func_pointer
pointer_to_func(void * p)104 pointer_to_func( void *p )
105 {
106    union {
107       void *p;
108       func_pointer f;
109    } pf;
110    pf.p = p;
111    return pf.f;
112 }
113 
114 static INLINE void *
func_to_pointer(func_pointer f)115 func_to_pointer( func_pointer f )
116 {
117    union {
118       void *p;
119       func_pointer f;
120    } pf;
121    pf.f = f;
122    return pf.p;
123 }
124 
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #endif /* U_POINTER_H */
131