PageRenderTime 327ms CodeModel.GetById 309ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llendianswizzle.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 94 lines | 44 code | 9 blank | 41 comment | 2 complexity | 49256c3633ea8330933a4a02b3f39235 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llendianswizzle.h
  3. * @brief Functions for in-place bit swizzling
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLENDIANSWIZZLE_H
  27. #define LL_LLENDIANSWIZZLE_H
  28. /* This function is intended to be used for in-place swizzling, particularly after fread() of
  29. binary values from a file. Such as:
  30. numRead = fread(scale.mV, sizeof(float), 3, fp);
  31. llendianswizzle(scale.mV, sizeof(float), 3);
  32. It assumes that the values in the file are LITTLE endian, so it's a no-op on a little endian machine.
  33. It keys off of typesize to do the correct swizzle, so make sure that typesize is the size of the native type.
  34. 64-bit types are not yet handled.
  35. */
  36. #ifdef LL_LITTLE_ENDIAN
  37. // little endian is native for most things.
  38. inline void llendianswizzle(void *,int,int)
  39. {
  40. // Nothing to do
  41. }
  42. #endif
  43. #ifdef LL_BIG_ENDIAN
  44. // big endian requires a bit of work.
  45. inline void llendianswizzle(void *p,int typesize, int count)
  46. {
  47. int i;
  48. switch(typesize)
  49. {
  50. case 2:
  51. {
  52. U16 temp;
  53. for(i=count ;i!=0 ;i--)
  54. {
  55. temp = ((U16*)p)[0];
  56. ((U16*)p)[0] = ((temp >> 8) & 0x000000FF) | ((temp << 8) & 0x0000FF00);
  57. p = (void*)(((U16*)p) + 1);
  58. }
  59. }
  60. break;
  61. case 4:
  62. {
  63. U32 temp;
  64. for(i=count; i!=0; i--)
  65. {
  66. temp = ((U32*)p)[0];
  67. ((U32*)p)[0] =
  68. ((temp >> 24) & 0x000000FF) |
  69. ((temp >> 8) & 0x0000FF00) |
  70. ((temp << 8) & 0x00FF0000) |
  71. ((temp << 24) & 0xFF000000);
  72. p = (void*)(((U32*)p) + 1);
  73. }
  74. }
  75. break;
  76. }
  77. }
  78. #endif
  79. // Use this when working with a single integral value you want swizzled
  80. #define llendianswizzleone(x) llendianswizzle(&(x), sizeof(x), 1)
  81. #endif // LL_LLENDIANSWIZZLE_H