1/* 2 * This file is part of FFmpeg. 3 * 4 * FFmpeg is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * FFmpeg is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with FFmpeg; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 19#ifndef AVCODEC_FLOAT2HALF_H 20#define AVCODEC_FLOAT2HALF_H 21 22#include <stdint.h> 23 24static void float2half_tables(uint16_t *basetable, uint8_t *shifttable) 25{ 26 for (int i = 0; i < 256; i++) { 27 int e = i - 127; 28 29 if (e < -24) { // Very small numbers map to zero 30 basetable[i|0x000] = 0x0000; 31 basetable[i|0x100] = 0x8000; 32 shifttable[i|0x000] = 24; 33 shifttable[i|0x100] = 24; 34 } else if (e < -14) { // Small numbers map to denorms 35 basetable[i|0x000] = (0x0400>>(-e-14)); 36 basetable[i|0x100] = (0x0400>>(-e-14)) | 0x8000; 37 shifttable[i|0x000] = -e-1; 38 shifttable[i|0x100] = -e-1; 39 } else if (e <= 15) { // Normal numbers just lose precision 40 basetable[i|0x000] = ((e + 15) << 10); 41 basetable[i|0x100] = ((e + 15) << 10) | 0x8000; 42 shifttable[i|0x000] = 13; 43 shifttable[i|0x100] = 13; 44 } else if (e < 128) { // Large numbers map to Infinity 45 basetable[i|0x000] = 0x7C00; 46 basetable[i|0x100] = 0xFC00; 47 shifttable[i|0x000] = 24; 48 shifttable[i|0x100] = 24; 49 } else { // Infinity and NaN's stay Infinity and NaN's 50 basetable[i|0x000] = 0x7C00; 51 basetable[i|0x100] = 0xFC00; 52 shifttable[i|0x000] = 13; 53 shifttable[i|0x100] = 13; 54 } 55 } 56} 57 58static uint16_t float2half(uint32_t f, uint16_t *basetable, uint8_t *shifttable) 59{ 60 uint16_t h; 61 62 h = basetable[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> shifttable[(f >> 23) & 0x1ff]); 63 64 return h; 65} 66 67#endif /* AVCODEC_FLOAT2HALF_H */ 68