| 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 | |
| 15 | #include "utils.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | unsigned short float32_to_float16(float value) |
| 20 | { |
| 21 | // 1 : 8 : 23 |
| 22 | union |
| 23 | { |
| 24 | unsigned int u; |
| 25 | float f; |
| 26 | } tmp; |
| 27 | |
| 28 | tmp.f = value; |
| 29 | |
| 30 | // 1 : 8 : 23 |
| 31 | unsigned short sign = (tmp.u & 0x80000000) >> 31; |
| 32 | unsigned short exponent = (tmp.u & 0x7F800000) >> 23; |
| 33 | unsigned int significand = tmp.u & 0x7FFFFF; |
| 34 | |
| 35 | // NCNN_LOGE("%d %d %d", sign, exponent, significand); |
| 36 | |
| 37 | // 1 : 5 : 10 |
| 38 | unsigned short fp16; |
| 39 | if (exponent == 0) |
| 40 | { |
| 41 | // zero or denormal, always underflow |
| 42 | fp16 = (sign << 15) | (0x00 << 10) | 0x00; |
| 43 | } |
| 44 | else if (exponent == 0xFF) |
| 45 | { |
| 46 | // infinity or NaN |
| 47 | fp16 = (sign << 15) | (0x1F << 10) | (significand ? 0x200 : 0x00); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | // normalized |
| 52 | short newexp = exponent + (-127 + 15); |
| 53 | if (newexp >= 31) |
| 54 | { |
| 55 | // overflow, return infinity |
| 56 | fp16 = (sign << 15) | (0x1F << 10) | 0x00; |
| 57 | } |
| 58 | else if (newexp <= 0) |
| 59 | { |
| 60 | // Some normal fp32 cannot be expressed as normal fp16 |
| 61 | fp16 = (sign << 15) | (0x00 << 10) | 0x00; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | // normal fp16 |
| 66 | fp16 = (sign << 15) | (newexp << 10) | (significand >> 13); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return fp16; |
| 71 | } |
| 72 | |
| 73 | float float16_to_float32(unsigned short value) |
| 74 | { |
| 75 | // 1 : 5 : 10 |
| 76 | unsigned short sign = (value & 0x8000) >> 15; |
| 77 | unsigned short exponent = (value & 0x7c00) >> 10; |
| 78 | unsigned short significand = value & 0x03FF; |
| 79 | |
| 80 | // NCNN_LOGE("%d %d %d", sign, exponent, significand); |
| 81 | |
| 82 | // 1 : 8 : 23 |
| 83 | union |
| 84 | { |
| 85 | unsigned int u; |
| 86 | float f; |
| 87 | } tmp; |
| 88 | if (exponent == 0) |
| 89 | { |
| 90 | if (significand == 0) |
| 91 | { |
| 92 | // zero |
| 93 | tmp.u = (sign << 31); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | // denormal |
| 98 | exponent = 0; |
| 99 | // find non-zero bit |
| 100 | while ((significand & 0x200) == 0) |
| 101 | { |
| 102 | significand <<= 1; |
| 103 | exponent++; |
| 104 | } |
| 105 | significand <<= 1; |
| 106 | significand &= 0x3FF; |
| 107 | tmp.u = (sign << 31) | ((-exponent + (-15 + 127)) << 23) | (significand << 13); |
| 108 | } |
| 109 | } |
| 110 | else if (exponent == 0x1F) |
| 111 | { |
| 112 | // infinity or NaN |
| 113 | tmp.u = (sign << 31) | (0xFF << 23) | (significand << 13); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | // normalized |
| 118 | tmp.u = (sign << 31) | ((exponent + (-15 + 127)) << 23) | (significand << 13); |
| 119 | } |
| 120 | |
| 121 | return tmp.f; |
| 122 | } |
| 123 | |
| 124 | } // namespace pnnx |