1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  *                         University Research and Technology
4  *                         Corporation.  All rights reserved.
5  * Copyright (c) 2004-2005 The University of Tennessee and The University
6  *                         of Tennessee Research Foundation.  All rights
7  *                         reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  *                         University of Stuttgart.  All rights reserved.
10  * Copyright (c) 2004-2005 The Regents of the University of California.
11  *                         All rights reserved.
12  * Copyright (c) 2014      Intel, Inc. All rights reserved.
13  * $COPYRIGHT$
14  *
15  * Additional copyrights may follow
16  *
17  * $HEADER$
18  */
19 
20 #ifndef OPAL_TYPES_H
21 #define OPAL_TYPES_H
22 
23 #include "opal_config.h"
24 
25 #include <stdint.h>
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
31 #endif
32 #ifdef HAVE_SYS_SELECT_H
33 #include <sys/select.h>
34 #endif
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
40 #endif
41 
42 #if OPAL_ENABLE_DEBUG
43 #include "opal/util/output.h"
44 #endif
45 
46 /*
47  * portable assignment of pointer to int
48  */
49 
50 typedef union {
51    uint64_t lval;
52    uint32_t ival;
53    void*    pval;
54    struct {
55        uint32_t uval;
56        uint32_t lval;
57    } sval;
58 } opal_ptr_t;
59 
60 /*
61  * handle differences in iovec
62  */
63 
64 #if defined(__APPLE__) || defined(__WINDOWS__)
65 typedef char* opal_iov_base_ptr_t;
66 #define OPAL_IOVBASE char
67 #else
68 #define OPAL_IOVBASE void
69 typedef void* opal_iov_base_ptr_t;
70 #endif
71 
72 /*
73  * handle differences in socklen_t
74  */
75 
76 #if defined(HAVE_SOCKLEN_T)
77 typedef socklen_t opal_socklen_t;
78 #else
79 typedef int opal_socklen_t;
80 #endif
81 
82 
83 /*
84  * Convert a 64 bit value to network byte order.
85  */
86 static inline uint64_t hton64(uint64_t val) __opal_attribute_const__;
hton64(uint64_t val)87 static inline uint64_t hton64(uint64_t val)
88 {
89 #ifdef HAVE_UNIX_BYTESWAP
90     union { uint64_t ll;
91             uint32_t l[2];
92     } w, r;
93 
94     /* platform already in network byte order? */
95     if(htonl(1) == 1L)
96         return val;
97     w.ll = val;
98     r.l[0] = htonl(w.l[1]);
99     r.l[1] = htonl(w.l[0]);
100     return r.ll;
101 #else
102     return val;
103 #endif
104 }
105 
106 /*
107  * Convert a 64 bit value from network to host byte order.
108  */
109 
110 static inline uint64_t ntoh64(uint64_t val) __opal_attribute_const__;
ntoh64(uint64_t val)111 static inline uint64_t ntoh64(uint64_t val)
112 {
113 #ifdef HAVE_UNIX_BYTESWAP
114     union { uint64_t ll;
115             uint32_t l[2];
116     } w, r;
117 
118     /* platform already in network byte order? */
119     if(htonl(1) == 1L)
120         return val;
121     w.ll = val;
122     r.l[0] = ntohl(w.l[1]);
123     r.l[1] = ntohl(w.l[0]);
124     return r.ll;
125 #else
126     return val;
127 #endif
128 }
129 
130 
131 /**
132  * Convert between a local representation of pointer and a 64 bits value.
133  */
134 static inline uint64_t opal_ptr_ptol( void* ptr ) __opal_attribute_const__;
opal_ptr_ptol(void * ptr)135 static inline uint64_t opal_ptr_ptol( void* ptr )
136 {
137     return (uint64_t)(uintptr_t) ptr;
138 }
139 
140 static inline void* opal_ptr_ltop( uint64_t value ) __opal_attribute_const__;
opal_ptr_ltop(uint64_t value)141 static inline void* opal_ptr_ltop( uint64_t value )
142 {
143 #if SIZEOF_VOID_P == 4 && OPAL_ENABLE_DEBUG
144     if (value > ((1ULL << 32) - 1ULL)) {
145         opal_output(0, "Warning: truncating value in opal_ptr_ltop");
146     }
147 #endif
148     return (void*)(uintptr_t) value;
149 }
150 
151 #if defined(WORDS_BIGENDIAN) || !defined(HAVE_UNIX_BYTESWAP)
152 static inline uint16_t opal_swap_bytes2(uint16_t val) __opal_attribute_const__;
opal_swap_bytes2(uint16_t val)153 static inline uint16_t opal_swap_bytes2(uint16_t val)
154 {
155     union { uint16_t bigval;
156             uint8_t  arrayval[2];
157     } w, r;
158 
159     w.bigval = val;
160     r.arrayval[0] = w.arrayval[1];
161     r.arrayval[1] = w.arrayval[0];
162 
163     return r.bigval;
164 }
165 
166 static inline uint32_t opal_swap_bytes4(uint32_t val) __opal_attribute_const__;
opal_swap_bytes4(uint32_t val)167 static inline uint32_t opal_swap_bytes4(uint32_t val)
168 {
169     union { uint32_t bigval;
170             uint8_t  arrayval[4];
171     } w, r;
172 
173     w.bigval = val;
174     r.arrayval[0] = w.arrayval[3];
175     r.arrayval[1] = w.arrayval[2];
176     r.arrayval[2] = w.arrayval[1];
177     r.arrayval[3] = w.arrayval[0];
178 
179     return r.bigval;
180 }
181 
182 static inline uint64_t opal_swap_bytes8(uint64_t val) __opal_attribute_const__;
opal_swap_bytes8(uint64_t val)183 static inline uint64_t opal_swap_bytes8(uint64_t val)
184 {
185     union { uint64_t bigval;
186             uint8_t  arrayval[8];
187     } w, r;
188 
189     w.bigval = val;
190     r.arrayval[0] = w.arrayval[7];
191     r.arrayval[1] = w.arrayval[6];
192     r.arrayval[2] = w.arrayval[5];
193     r.arrayval[3] = w.arrayval[4];
194     r.arrayval[4] = w.arrayval[3];
195     r.arrayval[5] = w.arrayval[2];
196     r.arrayval[6] = w.arrayval[1];
197     r.arrayval[7] = w.arrayval[0];
198 
199     return r.bigval;
200 }
201 
202 #else
203 #define opal_swap_bytes2 htons
204 #define opal_swap_bytes4 htonl
205 #define opal_swap_bytes8 hton64
206 #endif /* WORDS_BIGENDIAN || !HAVE_UNIX_BYTESWAP */
207 
208 #endif /* OPAL_TYPES_H */
209