You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.cpp 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "utils.h"
  15. namespace pnnx {
  16. unsigned short float32_to_float16(float value)
  17. {
  18. // 1 : 8 : 23
  19. union
  20. {
  21. unsigned int u;
  22. float f;
  23. } tmp;
  24. tmp.f = value;
  25. // 1 : 8 : 23
  26. unsigned short sign = (tmp.u & 0x80000000) >> 31;
  27. unsigned short exponent = (tmp.u & 0x7F800000) >> 23;
  28. unsigned int significand = tmp.u & 0x7FFFFF;
  29. // NCNN_LOGE("%d %d %d", sign, exponent, significand);
  30. // 1 : 5 : 10
  31. unsigned short fp16;
  32. if (exponent == 0)
  33. {
  34. // zero or denormal, always underflow
  35. fp16 = (sign << 15) | (0x00 << 10) | 0x00;
  36. }
  37. else if (exponent == 0xFF)
  38. {
  39. // infinity or NaN
  40. fp16 = (sign << 15) | (0x1F << 10) | (significand ? 0x200 : 0x00);
  41. }
  42. else
  43. {
  44. // normalized
  45. short newexp = exponent + (-127 + 15);
  46. if (newexp >= 31)
  47. {
  48. // overflow, return infinity
  49. fp16 = (sign << 15) | (0x1F << 10) | 0x00;
  50. }
  51. else if (newexp <= 0)
  52. {
  53. // Some normal fp32 cannot be expressed as normal fp16
  54. fp16 = (sign << 15) | (0x00 << 10) | 0x00;
  55. }
  56. else
  57. {
  58. // normal fp16
  59. fp16 = (sign << 15) | (newexp << 10) | (significand >> 13);
  60. }
  61. }
  62. return fp16;
  63. }
  64. float float16_to_float32(unsigned short value)
  65. {
  66. // 1 : 5 : 10
  67. unsigned short sign = (value & 0x8000) >> 15;
  68. unsigned short exponent = (value & 0x7c00) >> 10;
  69. unsigned short significand = value & 0x03FF;
  70. // NCNN_LOGE("%d %d %d", sign, exponent, significand);
  71. // 1 : 8 : 23
  72. union
  73. {
  74. unsigned int u;
  75. float f;
  76. } tmp;
  77. if (exponent == 0)
  78. {
  79. if (significand == 0)
  80. {
  81. // zero
  82. tmp.u = (sign << 31);
  83. }
  84. else
  85. {
  86. // denormal
  87. exponent = 0;
  88. // find non-zero bit
  89. while ((significand & 0x200) == 0)
  90. {
  91. significand <<= 1;
  92. exponent++;
  93. }
  94. significand <<= 1;
  95. significand &= 0x3FF;
  96. tmp.u = (sign << 31) | ((-exponent + (-15 + 127)) << 23) | (significand << 13);
  97. }
  98. }
  99. else if (exponent == 0x1F)
  100. {
  101. // infinity or NaN
  102. tmp.u = (sign << 31) | (0xFF << 23) | (significand << 13);
  103. }
  104. else
  105. {
  106. // normalized
  107. tmp.u = (sign << 31) | ((exponent + (-15 + 127)) << 23) | (significand << 13);
  108. }
  109. return tmp.f;
  110. }
  111. } // namespace pnnx