llendianswizzle.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @file llendianswizzle.h
  3. * @brief Functions for in-place bit swizzling
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreemen
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online a
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLENDIANSWIZZLE_H
  33. #define LL_LLENDIANSWIZZLE_H
  34. #include "llpreprocessor.h"
  35. /* This function is intended to be used for in-place swizzling, particularly
  36. after fread() of binary values from a file. Such as:
  37. numRead = fread(scale.mV, sizeof(float), 3, fp);
  38. llendianswizzle(scale.mV, sizeof(float), 3);
  39. It assumes that the values in the file are LITTLE endian, so it's a no-op
  40. on a little endian machine.
  41. It keys off of typesize to do the correct swizzle, so make sure tha
  42. typesize is the size of the native type.
  43. 64-bit types are not yet handled.
  44. */
  45. #if LL_BIG_ENDIAN
  46. // Big endian requires a bit of work.
  47. LL_INLINE void llendianswizzle(void* p, int typesize, int count)
  48. {
  49. switch (typesize)
  50. {
  51. case 2:
  52. {
  53. for (int i = count; i != 0; --i)
  54. {
  55. U16 temp = ((U16*)p)[0];
  56. ((U16*)p)[0] = ((temp >> 8) & 0x000000FF) |
  57. ((temp << 8) & 0x0000FF00);
  58. p = (void*)(((U16*)p) + 1);
  59. }
  60. break;
  61. }
  62. case 4:
  63. {
  64. for (int i = count; i != 0; --i)
  65. {
  66. U32 temp = ((U32*)p)[0];
  67. ((U32*)p)[0] = ((temp >> 24) & 0x000000FF) |
  68. ((temp >> 8) & 0x0000FF00) |
  69. ((temp << 8) & 0x00FF0000) |
  70. ((temp << 24) & 0xFF000000);
  71. p = (void*)(((U32*)p) + 1);
  72. }
  73. break;
  74. }
  75. }
  76. }
  77. #else
  78. // Little endian is native for most things.
  79. LL_INLINE void llendianswizzle(void*, int, int)
  80. {
  81. // Nothing to do
  82. }
  83. #endif
  84. // Use this when working with a single integral value you want swizzled
  85. #define llendianswizzleone(x) llendianswizzle(&(x), sizeof(x), 1)
  86. #endif // LL_LLENDIANSWIZZLE_H