/usr/include/crystalspace-2.0/csutil/csendian.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | /*
Copyright (C) 1998 by Jorrit Tyberghein
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CS_CSENDIAN_H__
#define __CS_CSENDIAN_H__
/**\file
* Helpers for dealing with endian conversions.
*/
/**\addtogroup util
* @{
*/
#include <math.h>
#include "cstypes.h"
#include "csgeom/math.h"
#include "csutil/bitops.h"
#if defined(CS_HAVE_BYTESWAP_H)
#include <byteswap.h>
#endif
#define csQroundSure(x) (int ((x) + ((x < 0) ? -0.5 : +0.5)))
/**
* Methods to unconditionally swap the byte order of specifically sized types.
*/
struct csSwapBytes
{
public:
//@{
/// Swap byte order
static CS_FORCEINLINE uint16 Swap (uint16 s)
{
#if defined(CS_COMPILER_MSVC) && (_MSC_VER >= 1300)
return _byteswap_ushort (s);
#elif defined(CS_HAVE_BYTESWAP_H)
return bswap_16 (s);
#else
return (s >> 8) | (s << 8);
#endif
}
static CS_FORCEINLINE int16 Swap (int16 s)
{ return (int16)Swap ((uint16)s); }
static CS_FORCEINLINE uint32 Swap (uint32 l)
{
#if defined(CS_COMPILER_MSVC) && (_MSC_VER >= 1300)
return _byteswap_ulong (l);
#elif defined(CS_HAVE_BYTESWAP_H)
return bswap_32 (l);
#else
return (l >> 24) | ((l >> 8) & 0xff00) | ((l << 8) & 0xff0000) | (l << 24);
#endif
}
static CS_FORCEINLINE int32 Swap (int32 l)
{ return (int32)Swap ((uint32)l); }
static CS_FORCEINLINE uint64 Swap (uint64 l)
{
#if defined(CS_COMPILER_MSVC) && (_MSC_VER >= 1300)
return _byteswap_uint64 (l);
#elif defined(CS_HAVE_BYTESWAP_H) && !defined(__STRICT_ANSI__)
return bswap_64 (l);
#else
union
{
uint64 ui64;
uint32 ui32[2];
} u1, u2;
u1.ui64 = l;
u2.ui32[0] = Swap (u1.ui32[1]);
u2.ui32[1] = Swap (u1.ui32[0]);
return u2.ui64;
#endif
}
static CS_FORCEINLINE int64 Swap (int64 l)
{ return (int64)Swap ((uint64)l); }
static CS_FORCEINLINE uint16 UInt16 (uint16 x) { return Swap (x); }
static CS_FORCEINLINE int16 Int16 (int16 x) { return Swap (x); }
static CS_FORCEINLINE uint32 UInt32 (uint32 x) { return Swap (x); }
static CS_FORCEINLINE int32 Int32 (int32 x) { return Swap (x); }
static CS_FORCEINLINE uint64 UInt64 (uint64 x) { return Swap (x); }
static CS_FORCEINLINE int64 Int64 (int64 x) { return Swap (x); }
//@}
};
#ifdef CS_BIG_ENDIAN
struct csBigEndian
#else
/**
* Little endian to native conversion routines.
* \remarks Since conversion from and to native representation is the same
* operation, all methods can be used for either direction.
*/
struct csLittleEndian
#endif
{
//@{
/// Convert specifically sized type from or to little endian.
static CS_FORCEINLINE uint16 Convert (uint16 x) { return x; }
static CS_FORCEINLINE int16 Convert (int16 x) { return x; }
static CS_FORCEINLINE uint32 Convert (uint32 x) { return x; }
static CS_FORCEINLINE int32 Convert (int32 x) { return x; }
static CS_FORCEINLINE uint64 Convert (uint64 x) { return x; }
static CS_FORCEINLINE int64 Convert (int64 x) { return x; }
static CS_FORCEINLINE uint16 UInt16 (uint16 x) { return Convert (x); }
static CS_FORCEINLINE int16 Int16 (int16 x) { return Convert (x); }
static CS_FORCEINLINE uint32 UInt32 (uint32 x) { return Convert (x); }
static CS_FORCEINLINE int32 Int32 (int32 x) { return Convert (x); }
static CS_FORCEINLINE uint64 UInt64 (uint64 x) { return Convert (x); }
static CS_FORCEINLINE int64 Int64 (int64 x) { return Convert (x); }
//@}
};
#ifdef CS_LITTLE_ENDIAN
/**
* Big endian to native conversion routines.
* \remarks Since conversion from and to native representation is the same
* operation, all methods can be used for either direction.
*/
struct csBigEndian
#else
struct csLittleEndian
#endif
{
public:
//@{
/// Convert specifically sized type from or to big endian.
static CS_FORCEINLINE uint16 Convert (uint16 s)
{ return csSwapBytes::Swap (s); }
static CS_FORCEINLINE int16 Convert (int16 s)
{ return csSwapBytes::Swap (s); }
static CS_FORCEINLINE uint32 Convert (uint32 l)
{ return csSwapBytes::Swap (l); }
static CS_FORCEINLINE int32 Convert (int32 l)
{ return csSwapBytes::Swap (l); }
static CS_FORCEINLINE uint64 Convert (uint64 l)
{ return csSwapBytes::Swap (l); }
static CS_FORCEINLINE int64 Convert (int64 l)
{ return csSwapBytes::Swap (l); }
static CS_FORCEINLINE uint16 UInt16 (uint16 x) { return Convert (x); }
static CS_FORCEINLINE int16 Int16 (int16 x) { return Convert (x); }
static CS_FORCEINLINE uint32 UInt32 (uint32 x) { return Convert (x); }
static CS_FORCEINLINE int32 Int32 (int32 x) { return Convert (x); }
static CS_FORCEINLINE uint64 UInt64 (uint64 x) { return Convert (x); }
static CS_FORCEINLINE int64 Int64 (int64 x) { return Convert (x); }
//@}
};
/**
* Convert IEEE 32-bit floats from or to native machine floats.
*/
struct csIEEEfloat
{
/* \todo It would be even better if we also check for sizeof (float)
* in configure or so. */
#ifdef CS_IEEE_DOUBLE_FORMAT
//@{
/// Convert native to IEEE
static CS_FORCEINLINE uint32 FromNative (float f)
{
union
{
float f;
uint32 ui32;
} u;
u.f = f;
return u.ui32;
}
static CS_FORCEINLINE uint64 FromNative (double f)
{
union
{
double f;
uint64 ui64;
} u;
u.f = f;
return u.ui64;
}
//@}
//@{
/// Convert IEEE to native
static CS_FORCEINLINE float ToNative (uint32 f)
{
union
{
float f;
uint32 ui32;
} u;
u.ui32 = f;
return u.f;
}
static CS_FORCEINLINE double ToNative (uint64 f)
{
union
{
double f;
uint64 ui64;
} u;
u.ui64 = f;
return u.f;
}
//@}
#else
#error Do not know how to convert to IEEE floats
#endif
/// Convert IEEE half-precision float number to native 'float' type value
static CS_FORCEINLINE float ToNative (uint16 half)
{
union
{
uint32 u;
float f;
} u2f;
uint32 sign = (half & 0x8000) << 16;
int32 exponent = (half & 0x7C00) >> 10;
uint32 mantissa = (half & 0x03ff) << 13;
// Check for INF or NaN.
if (exponent == 0x1F)
{
u2f.u = sign | mantissa;
if (mantissa != 0)
{
// NaN
u2f.u |= 0x7FC00000;
}
else
{
// INF
u2f.u |= 0x7f800000;
}
return u2f.f;
}
// Check for a denorm.
if(exponent == 0)
{
unsigned long index;
CS::Utility::BitOps::ScanBitReverse (mantissa, index);
exponent -= (index - 9);
mantissa <<= (index - 8);
mantissa &= 0x007FFFFF;
}
// Convert the exponent...
exponent += 112;
exponent <<= 23;
// And create the float...
u2f.u = sign | exponent | mantissa;
return u2f.f;
}
/**
* Convert native 'float' type value to IEEE half-precision float number.
* Rounds towards zero.
*/
static CS_FORCEINLINE uint16 FromNativeRTZ (float f)
{
union
{
float f;
unsigned int u;
} f2u;
f2u.f = f;
unsigned short sign = 0x8000 & (f2u.u >> 16);
// Get the absolute value.
f2u.u &= 0x7FFFFFFF;
// Check for a NaN
if(CS::IsNaN (f2u.f))
{
// Construct a silent NaN.
f2u.u >>= 13;
f2u.u &= 0x7fff;
f2u.u |= 0x0200;
return sign | f2u.u;
}
// Check for overflow.
if(f2u.u >= 0x47800000)
{
// Check for INF.
if(f2u.u == 0x7F800000)
return sign | 0x7C00;
return sign | 0x7BFF;
}
// Check for underflow and denorms (flush to zero).
if(f2u.u < 0x38800000)
return sign;
// Convert the float to a half (rounding to zero).
f2u.u &= 0xFFFFE000U;
f2u.u -= 0x38000000U;
return sign | (f2u.u >> 13);
}
};
/**
* Sized data type access helpers.
* On some platforms, certain data types can only be accessed when correctly
* aligned (e.g. uint32 can only be read from addresses aligned to 4 bytes).
* This routines assist accessing sized types from arbitrary memory positions
* (e.g. when parsing files from memory) by working around the alignment
* requirements on platforms that have such.
*/
struct csGetFromAddress
{
//@{
/// Get specifically sized type from unaligned memory address
static CS_FORCEINLINE uint16 UInt16 (const void *buff)
{
#ifdef CS_STRICT_ALIGNMENT
uint16 s; memcpy (&s, buff, sizeof (s));
return s;
#else
return *(uint16 *)buff;
#endif
}
static CS_FORCEINLINE int16 Int16 (const void *buff)
{ return (int16)UInt16 (buff); }
static CS_FORCEINLINE uint32 UInt32 (const void *buff)
{
#ifdef CS_STRICT_ALIGNMENT
uint32 s; memcpy (&s, buff, sizeof (s));
return s;
#else
return *(uint32 *)buff;
#endif
}
static CS_FORCEINLINE int32 Int32 (const void *buff)
{ return (int32)UInt32 (buff); }
static CS_FORCEINLINE uint64 UInt64 (const void *buff)
{
#ifdef CS_STRICT_ALIGNMENT
uint64 s; memcpy (&s, buff, sizeof (s));
return s;
#else
return *(uint64 *)buff;
#endif
}
static CS_FORCEINLINE int64 Int64 (const void *buff)
{ return (int64)UInt64 (buff); }
//@}
};
/**
* Sized data type access helpers.
* On some platforms, certain data types can only be manipulated when correctly
* aligned (e.g. uint32 can only be Written to addresses aligned to 4 bytes).
* This routines assist manipulating sized types at arbitrary memory positions
* (e.g. when constructing files in memory) by working around the alignment
* requirements on platforms that have such.
*/
struct csSetToAddress
{
//@{
/// Set specifically sized type at unaligned memory address
static CS_FORCEINLINE void UInt16 (void *buff, uint16 s)
{
#ifdef CS_STRICT_ALIGNMENT
memcpy (buff, &s, sizeof (s));
#else
*((uint16 *)buff) = s;
#endif
}
static CS_FORCEINLINE void Int16 (void *buff, int16 s)
{ UInt16 (buff, (uint16)s); }
static CS_FORCEINLINE void UInt32 (void *buff, uint32 s)
{
#ifdef CS_STRICT_ALIGNMENT
memcpy (buff, &s, sizeof (s));
#else
*((uint32 *)buff) = s;
#endif
}
static CS_FORCEINLINE void Int32 (void *buff, int32 s)
{ UInt32 (buff, (uint32)s); }
static CS_FORCEINLINE void UInt64 (void *buff, uint64 s)
{
#ifdef CS_STRICT_ALIGNMENT
memcpy (buff, &s, sizeof (s));
#else
*((uint64 *)buff) = s;
#endif
}
static CS_FORCEINLINE void Int64 (void *buff, int64 s)
{ UInt64 (buff, (uint64)s); }
//@}
};
/*
To be able to painlessly transfer files betwen platforms, we should
avoid using native floating-point format. Here are a couple of routines
that are guaranteed to work on all platforms.
The floating point is converted to a fixed 1.7.25 bits format
(one bit sign, 7 bits exponent, 25 bits mantissa) and back,
so that we can binary store floating-point number without
cross-platform problems. If you wonder why 1+7+25 = 33 while we
only have 32 bits, we'll ommit the most significant bit of mantissa
since it is always 1 (we use normalized numbers). This increases the
precision twice.
For double, we use one bit sign, 15 bits exponent, 49 bits mantissa.
*/
/**
* Convert a float to a cross-platform 32-bit format (no endianess
* adjustments!)
* \deprecated Deprecated in 2.0. Use csIEEEfloat methods instead.
*/
CS_DEPRECATED_METHOD_MSG("Use csIEEEfloat methods instead")
static inline int32 csFloatToLong (float f)
{
int exp;
int32 mant = csQroundSure (frexp (f, &exp) * 0x1000000);
int32 sign = mant & 0x80000000;
if (mant < 0) mant = -mant;
if (exp > 63) exp = 63; else if (exp < -64) exp = -64;
return sign | ((exp & 0x7f) << 24) | (mant & 0xffffff);
}
/**
* Convert a 32-bit cross-platform float to native format (no endianess
* adjustments!)
* \deprecated Deprecated in 2.0. Use csIEEEfloat methods instead.
*/
CS_DEPRECATED_METHOD_MSG("Use csIEEEfloat methods instead")
static inline float csLongToFloat (int32 l)
{
int exp = (l >> 24) & 0x7f;
if (exp & 0x40) exp = exp | ~0x7f;
float mant = float (l & 0x00ffffff) / 0x1000000;
if (l & 0x80000000) mant = -mant;
return (float) ldexp (mant, exp);
}
/* Implementation note: csDoubleToLongLong() and csLongLongToDouble()
*
* We avoid use of CONST_INT64() because 64-bit constants are illegal with g++
* under -ansi -pedantic, and we want this header to be useful to external
* projects which use -ansi -pedantic. Instead, we use bit shifts, such as (1
* << 59), and construct `mask' manually.
*/
/**
* Convert a double to a cross-platform 64-bit format (no endianess
* adjustments!)
* \deprecated Deprecated in 2.0. Use csIEEEfloat methods instead.
*/
CS_DEPRECATED_METHOD_MSG("Use csIEEEfloat methods instead")
static inline int64 csDoubleToLongLong (double d)
{
int exp;
int64 mant = (int64) (frexp (d, &exp) * ((int64)1 << 48));
int64 sign = mant & ((int64)1 << 59);
if (mant < 0) mant = -mant;
if (exp > 32767) exp = 32767; else if (exp < -32768) exp = -32768;
int64 const mask = ((uint64)0xffff << 32) | (uint64)0xffffffff;
return sign | ((int64 (exp) & 0x7fff) << 48) | (mant & mask);
}
/**
* Convert a 64-bit cross-platform double to native format (no endianess
* adjustments!)
* \deprecated Deprecated in 2.0. Use csIEEEfloat methods instead.
*/
CS_DEPRECATED_METHOD_MSG("Use csIEEEfloat methods instead")
static inline double csLongLongToDouble (int64 i)
{
int exp = (i >> 48) & 0x7fff;
if (exp & 0x4000) exp = exp | ~0x7fff;
int64 const mask = ((uint64)0xffff << 32) | (uint64)0xffffffff;
double mant = double (i & mask) / ((int64)1 << 48);
if (i & ((int64)1 << 59)) mant = -mant;
return ldexp (mant, exp);
}
/* *\name Floating point conversions
* These routines are used for converting floating-point numbers
* into 16-bit shorts and back. This is useful for low-precision data.
* They use the 1.4.12 format. The range of numbers that can be represented
* in this format is from 2^-8 to 2^7. The precision for numbers near to
* 2^-8 (0.00390625) is near 0.000001, for numbers near 2^7 (128) is near 0.03.
* @{ */
/**
* Convert a float to a cross-platform 16-bit format (no endianess
* adjustments!)
* \deprecated Deprecated in 2.0. Use csIEEEfloat methods instead.
*/
CS_DEPRECATED_METHOD_MSG("Use csIEEEfloat methods instead")
static inline short csFloatToShort (float f)
{
int exp;
long mant = csQroundSure (frexp (f, &exp) * 0x1000);
long sign = mant & 0x8000;
if (mant < 0) mant = -mant;
if (exp > 7) mant = 0x7ff, exp = 7; else if (exp < -8) mant = 0, exp = -8;
return short(sign | ((exp & 0xf) << 11) | (mant & 0x7ff));
}
/**
* Convert a 16-bit cross-platform float to native format (no endianess
* adjustments!)
* \deprecated Deprecated in 2.0. Use csIEEEfloat methods instead.
*/
CS_DEPRECATED_METHOD_MSG("Use csIEEEfloat methods instead")
static inline float csShortToFloat (short s)
{
int exp = (s >> 11) & 0xf;
if (exp & 0x8) exp = exp | ~0xf;
float mant = float ((s & 0x07ff) | 0x0800) / 0x1000;
if (s & 0x8000) mant = -mant;
return (float) ldexp (mant, exp);
}
/** @} */
/** @} */
/** @} */
#endif // __CS_CSENDIAN_H__
|