llquantize.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file llquantize.h
  3. * @brief useful routines for quantizing floats to various length ints
  4. * and back out again
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewergpl$
  7. *
  8. * Copyright (c) 2001-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLQUANTIZE_H
  34. #define LL_LLQUANTIZE_H
  35. #include "llpreprocessor.h"
  36. constexpr U16 U16MAX = 65535;
  37. alignas(16) const F32 F_U16MAX_4A[4] = { 65535.f, 65535.f, 65535.f, 65535.f };
  38. constexpr F32 OOU16MAX = 1.f / (F32)(U16MAX);
  39. alignas(16) const F32 F_OOU16MAX_4A[4] = { OOU16MAX, OOU16MAX, OOU16MAX, OOU16MAX };
  40. constexpr U8 U8MAX = 255;
  41. alignas(16) const F32 F_U8MAX_4A[4] = { 255.f, 255.f, 255.f, 255.f };
  42. constexpr F32 OOU8MAX = 1.f / (F32)(U8MAX);
  43. alignas(16) const F32 F_OOU8MAX_4A[4] = { OOU8MAX, OOU8MAX, OOU8MAX, OOU8MAX };
  44. LL_INLINE U16 F32_to_U16_ROUND(F32 val, F32 lower, F32 upper)
  45. {
  46. val = llclamp(val, lower, upper);
  47. // Make sure that the value is positive and normalized to <0, 1>
  48. val -= lower;
  49. val /= (upper - lower);
  50. // Round the value and return the U16
  51. return (U16)(ll_round(val * U16MAX));
  52. }
  53. LL_INLINE U16 F32_to_U16(F32 val, F32 lower, F32 upper)
  54. {
  55. val = llclamp(val, lower, upper);
  56. // Make sure that the value is positive and normalized to <0, 1>
  57. val -= lower;
  58. val /= (upper - lower);
  59. // Return the U16
  60. return (U16)(llfloor(val * U16MAX));
  61. }
  62. LL_INLINE F32 U16_to_F32(U16 ival, F32 lower, F32 upper)
  63. {
  64. F32 val = ival * OOU16MAX;
  65. F32 delta = (upper - lower);
  66. val *= delta;
  67. val += lower;
  68. F32 max_error = delta * OOU16MAX;
  69. // Make sure that zero's come through as zero
  70. if (fabsf(val) < max_error)
  71. {
  72. val = 0.f;
  73. }
  74. return val;
  75. }
  76. LL_INLINE U8 F32_to_U8_ROUND(F32 val, F32 lower, F32 upper)
  77. {
  78. val = llclamp(val, lower, upper);
  79. // Make sure that the value is positive and normalized to <0, 1>
  80. val -= lower;
  81. val /= (upper - lower);
  82. // Return the rounded U8
  83. return (U8)(ll_round(val * U8MAX));
  84. }
  85. LL_INLINE U8 F32_to_U8(F32 val, F32 lower, F32 upper)
  86. {
  87. val = llclamp(val, lower, upper);
  88. // Make sure that the value is positive and normalized to <0, 1>
  89. val -= lower;
  90. val /= (upper - lower);
  91. // Return the U8
  92. return (U8)(llfloor(val * U8MAX));
  93. }
  94. LL_INLINE F32 U8_to_F32(U8 ival, F32 lower, F32 upper)
  95. {
  96. F32 val = ival * OOU8MAX;
  97. F32 delta = (upper - lower);
  98. val *= delta;
  99. val += lower;
  100. F32 max_error = delta * OOU8MAX;
  101. // Make sure that zero's come through as zero
  102. if (fabsf(val) < max_error)
  103. {
  104. val = 0.f;
  105. }
  106. return val;
  107. }
  108. #endif