half.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /******************************************************************************
  2. Copyright (C) 2020 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. /******************************************************************************
  15. The MIT License (MIT)
  16. Copyright (c) 2011-2019 Microsoft Corp
  17. Permission is hereby granted, free of charge, to any person obtaining a copy of this
  18. software and associated documentation files (the "Software"), to deal in the Software
  19. without restriction, including without limitation the rights to use, copy, modify,
  20. merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  21. permit persons to whom the Software is furnished to do so, subject to the following
  22. conditions:
  23. The above copyright notice and this permission notice shall be included in all copies
  24. or substantial portions of the Software.
  25. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  26. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  27. PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  28. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  29. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  30. OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. ******************************************************************************/
  32. #pragma once
  33. #include "math-defs.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. struct half {
  38. uint16_t u;
  39. };
  40. /* adapted from DirectXMath XMConvertFloatToHalf */
  41. static struct half half_from_float(float f)
  42. {
  43. uint32_t Result;
  44. uint32_t IValue;
  45. memcpy(&IValue, &f, sizeof(IValue));
  46. uint32_t Sign = (IValue & 0x80000000U) >> 16U;
  47. IValue = IValue & 0x7FFFFFFFU; // Hack off the sign
  48. if (IValue > 0x477FE000U) {
  49. // The number is too large to be represented as a half. Saturate to infinity.
  50. if (((IValue & 0x7F800000) == 0x7F800000) &&
  51. ((IValue & 0x7FFFFF) != 0)) {
  52. Result = 0x7FFF; // NAN
  53. } else {
  54. Result = 0x7C00U; // INF
  55. }
  56. } else if (!IValue) {
  57. Result = 0;
  58. } else {
  59. if (IValue < 0x38800000U) {
  60. // The number is too small to be represented as a normalized half.
  61. // Convert it to a denormalized value.
  62. uint32_t Shift = 113U - (IValue >> 23U);
  63. IValue = (0x800000U | (IValue & 0x7FFFFFU)) >> Shift;
  64. } else {
  65. // Rebias the exponent to represent the value as a normalized half.
  66. IValue += 0xC8000000U;
  67. }
  68. Result = ((IValue + 0x0FFFU + ((IValue >> 13U) & 1U)) >> 13U) &
  69. 0x7FFFU;
  70. }
  71. struct half h;
  72. h.u = (uint16_t)(Result | Sign);
  73. return h;
  74. }
  75. static struct half half_from_bits(uint16_t u)
  76. {
  77. struct half h;
  78. h.u = u;
  79. return h;
  80. }
  81. #ifdef __cplusplus
  82. }
  83. #endif