/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include namespace paddle_mobile { namespace fpga { int16_t fp32_2_fp16(float fp32_num) { unsigned long tmp = *(unsigned long *)(&fp32_num); // NOLINT auto t = (int16_t)(((tmp & 0x007fffff) >> 13) | ((tmp & 0x80000000) >> 16) | (((tmp & 0x7f800000) >> 13) - (112 << 10))); if (tmp & 0x1000) { t++; // roundoff } return t; } float fp16_2_fp32(int16_t fp16_num) { if (0 == fp16_num) { return 0; } int frac = (fp16_num & 0x3ff); int exp = ((fp16_num & 0x7c00) >> 10) + 112; int s = fp16_num & 0x8000; int tmp = 0; float fp32_num; tmp = s << 16 | exp << 23 | frac << 13; fp32_num = *(float *)&tmp; // NOLINT return fp32_num; } } // namespace fpga } // namespace paddle_mobile