CFByteOrder.h
1 /* CFByteOrder.h 2 Copyright (c) 1995-2019, Apple Inc. and the Swift project authors 3 4 Portions Copyright (c) 2014-2019, Apple Inc. and the Swift project authors 5 Licensed under Apache License v2.0 with Runtime Library Exception 6 See http://swift.org/LICENSE.txt for license information 7 See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 8 */ 9 10 #if !defined(__COREFOUNDATION_CFBYTEORDER__) 11 #define __COREFOUNDATION_CFBYTEORDER__ 1 12 13 #include <CoreFoundation/CFBase.h> 14 #if (TARGET_OS_OSX || TARGET_OS_IPHONE) && !defined(CF_USE_OSBYTEORDER_H) 15 #include <libkern/OSByteOrder.h> 16 #define CF_USE_OSBYTEORDER_H 1 17 #endif 18 19 CF_EXTERN_C_BEGIN 20 21 enum __CFByteOrder { 22 CFByteOrderUnknown, 23 CFByteOrderLittleEndian, 24 CFByteOrderBigEndian 25 }; 26 typedef CFIndex CFByteOrder; 27 28 CF_INLINE CFByteOrder CFByteOrderGetCurrent(void) { 29 #if CF_USE_OSBYTEORDER_H 30 int32_t byteOrder = OSHostByteOrder(); 31 switch (byteOrder) { 32 case OSLittleEndian: return CFByteOrderLittleEndian; 33 case OSBigEndian: return CFByteOrderBigEndian; 34 default: break; 35 } 36 return CFByteOrderUnknown; 37 #else 38 #if __LITTLE_ENDIAN__ 39 return CFByteOrderLittleEndian; 40 #elif __BIG_ENDIAN__ 41 return CFByteOrderBigEndian; 42 #else 43 return CFByteOrderUnknown; 44 #endif 45 #endif 46 } 47 48 CF_INLINE uint16_t CFSwapInt16(uint16_t arg) { 49 #if CF_USE_OSBYTEORDER_H 50 return OSSwapInt16(arg); 51 #else 52 uint16_t result; 53 result = (uint16_t)(((arg << 8) & 0xFF00) | ((arg >> 8) & 0xFF)); 54 return result; 55 #endif 56 } 57 58 CF_INLINE uint32_t CFSwapInt32(uint32_t arg) { 59 #if CF_USE_OSBYTEORDER_H 60 return OSSwapInt32(arg); 61 #else 62 uint32_t result; 63 result = ((arg & 0xFF) << 24) | ((arg & 0xFF00) << 8) | ((arg >> 8) & 0xFF00) | ((arg >> 24) & 0xFF); 64 return result; 65 #endif 66 } 67 68 CF_INLINE uint64_t CFSwapInt64(uint64_t arg) { 69 #if CF_USE_OSBYTEORDER_H 70 return OSSwapInt64(arg); 71 #else 72 union CFSwap { 73 uint64_t sv; 74 uint32_t ul[2]; 75 } tmp, result; 76 tmp.sv = arg; 77 result.ul[0] = CFSwapInt32(tmp.ul[1]); 78 result.ul[1] = CFSwapInt32(tmp.ul[0]); 79 return result.sv; 80 #endif 81 } 82 83 CF_INLINE uint16_t CFSwapInt16BigToHost(uint16_t arg) { 84 #if CF_USE_OSBYTEORDER_H 85 return OSSwapBigToHostInt16(arg); 86 #elif __BIG_ENDIAN__ 87 return arg; 88 #else 89 return CFSwapInt16(arg); 90 #endif 91 } 92 93 CF_INLINE uint32_t CFSwapInt32BigToHost(uint32_t arg) { 94 #if CF_USE_OSBYTEORDER_H 95 return OSSwapBigToHostInt32(arg); 96 #elif __BIG_ENDIAN__ 97 return arg; 98 #else 99 return CFSwapInt32(arg); 100 #endif 101 } 102 103 CF_INLINE uint64_t CFSwapInt64BigToHost(uint64_t arg) { 104 #if CF_USE_OSBYTEORDER_H 105 return OSSwapBigToHostInt64(arg); 106 #elif __BIG_ENDIAN__ 107 return arg; 108 #else 109 return CFSwapInt64(arg); 110 #endif 111 } 112 113 CF_INLINE uint16_t CFSwapInt16HostToBig(uint16_t arg) { 114 #if CF_USE_OSBYTEORDER_H 115 return OSSwapHostToBigInt16(arg); 116 #elif __BIG_ENDIAN__ 117 return arg; 118 #else 119 return CFSwapInt16(arg); 120 #endif 121 } 122 123 CF_INLINE uint32_t CFSwapInt32HostToBig(uint32_t arg) { 124 #if CF_USE_OSBYTEORDER_H 125 return OSSwapHostToBigInt32(arg); 126 #elif __BIG_ENDIAN__ 127 return arg; 128 #else 129 return CFSwapInt32(arg); 130 #endif 131 } 132 133 CF_INLINE uint64_t CFSwapInt64HostToBig(uint64_t arg) { 134 #if CF_USE_OSBYTEORDER_H 135 return OSSwapHostToBigInt64(arg); 136 #elif __BIG_ENDIAN__ 137 return arg; 138 #else 139 return CFSwapInt64(arg); 140 #endif 141 } 142 143 CF_INLINE uint16_t CFSwapInt16LittleToHost(uint16_t arg) { 144 #if CF_USE_OSBYTEORDER_H 145 return OSSwapLittleToHostInt16(arg); 146 #elif __LITTLE_ENDIAN__ 147 return arg; 148 #else 149 return CFSwapInt16(arg); 150 #endif 151 } 152 153 CF_INLINE uint32_t CFSwapInt32LittleToHost(uint32_t arg) { 154 #if CF_USE_OSBYTEORDER_H 155 return OSSwapLittleToHostInt32(arg); 156 #elif __LITTLE_ENDIAN__ 157 return arg; 158 #else 159 return CFSwapInt32(arg); 160 #endif 161 } 162 163 CF_INLINE uint64_t CFSwapInt64LittleToHost(uint64_t arg) { 164 #if CF_USE_OSBYTEORDER_H 165 return OSSwapLittleToHostInt64(arg); 166 #elif __LITTLE_ENDIAN__ 167 return arg; 168 #else 169 return CFSwapInt64(arg); 170 #endif 171 } 172 173 CF_INLINE uint16_t CFSwapInt16HostToLittle(uint16_t arg) { 174 #if CF_USE_OSBYTEORDER_H 175 return OSSwapHostToLittleInt16(arg); 176 #elif __LITTLE_ENDIAN__ 177 return arg; 178 #else 179 return CFSwapInt16(arg); 180 #endif 181 } 182 183 CF_INLINE uint32_t CFSwapInt32HostToLittle(uint32_t arg) { 184 #if CF_USE_OSBYTEORDER_H 185 return OSSwapHostToLittleInt32(arg); 186 #elif __LITTLE_ENDIAN__ 187 return arg; 188 #else 189 return CFSwapInt32(arg); 190 #endif 191 } 192 193 CF_INLINE uint64_t CFSwapInt64HostToLittle(uint64_t arg) { 194 #if CF_USE_OSBYTEORDER_H 195 return OSSwapHostToLittleInt64(arg); 196 #elif __LITTLE_ENDIAN__ 197 return arg; 198 #else 199 return CFSwapInt64(arg); 200 #endif 201 } 202 203 typedef struct {uint32_t v;} CFSwappedFloat32; 204 typedef struct {uint64_t v;} CFSwappedFloat64; 205 206 CF_INLINE CFSwappedFloat32 CFConvertFloat32HostToSwapped(Float32 arg) { 207 union CFSwap { 208 Float32 v; 209 CFSwappedFloat32 sv; 210 } result; 211 result.v = arg; 212 #if __LITTLE_ENDIAN__ 213 result.sv.v = CFSwapInt32(result.sv.v); 214 #endif 215 return result.sv; 216 } 217 218 CF_INLINE Float32 CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg) { 219 union CFSwap { 220 Float32 v; 221 CFSwappedFloat32 sv; 222 } result; 223 result.sv = arg; 224 #if __LITTLE_ENDIAN__ 225 result.sv.v = CFSwapInt32(result.sv.v); 226 #endif 227 return result.v; 228 } 229 230 CF_INLINE CFSwappedFloat64 CFConvertFloat64HostToSwapped(Float64 arg) { 231 union CFSwap { 232 Float64 v; 233 CFSwappedFloat64 sv; 234 } result; 235 result.v = arg; 236 #if __LITTLE_ENDIAN__ 237 result.sv.v = CFSwapInt64(result.sv.v); 238 #endif 239 return result.sv; 240 } 241 242 CF_INLINE Float64 CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg) { 243 union CFSwap { 244 Float64 v; 245 CFSwappedFloat64 sv; 246 } result; 247 result.sv = arg; 248 #if __LITTLE_ENDIAN__ 249 result.sv.v = CFSwapInt64(result.sv.v); 250 #endif 251 return result.v; 252 } 253 254 CF_INLINE CFSwappedFloat32 CFConvertFloatHostToSwapped(float arg) { 255 union CFSwap { 256 float v; 257 CFSwappedFloat32 sv; 258 } result; 259 result.v = arg; 260 #if __LITTLE_ENDIAN__ 261 result.sv.v = CFSwapInt32(result.sv.v); 262 #endif 263 return result.sv; 264 } 265 266 CF_INLINE float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg) { 267 union CFSwap { 268 float v; 269 CFSwappedFloat32 sv; 270 } result; 271 result.sv = arg; 272 #if __LITTLE_ENDIAN__ 273 result.sv.v = CFSwapInt32(result.sv.v); 274 #endif 275 return result.v; 276 } 277 278 CF_INLINE CFSwappedFloat64 CFConvertDoubleHostToSwapped(double arg) { 279 union CFSwap { 280 double v; 281 CFSwappedFloat64 sv; 282 } result; 283 result.v = arg; 284 #if __LITTLE_ENDIAN__ 285 result.sv.v = CFSwapInt64(result.sv.v); 286 #endif 287 return result.sv; 288 } 289 290 CF_INLINE double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg) { 291 union CFSwap { 292 double v; 293 CFSwappedFloat64 sv; 294 } result; 295 result.sv = arg; 296 #if __LITTLE_ENDIAN__ 297 result.sv.v = CFSwapInt64(result.sv.v); 298 #endif 299 return result.v; 300 } 301 302 CF_EXTERN_C_END 303 304 #endif /* ! __COREFOUNDATION_CFBYTEORDER__ */ 305