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

68 lines
2.4KB

  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. #ifndef AVCODEC_FLOAT2HALF_H
  19. #define AVCODEC_FLOAT2HALF_H
  20. #include <stdint.h>
  21. static void float2half_tables(uint16_t *basetable, uint8_t *shifttable)
  22. {
  23. for (int i = 0; i < 256; i++) {
  24. int e = i - 127;
  25. if (e < -24) { // Very small numbers map to zero
  26. basetable[i|0x000] = 0x0000;
  27. basetable[i|0x100] = 0x8000;
  28. shifttable[i|0x000] = 24;
  29. shifttable[i|0x100] = 24;
  30. } else if (e < -14) { // Small numbers map to denorms
  31. basetable[i|0x000] = (0x0400>>(-e-14));
  32. basetable[i|0x100] = (0x0400>>(-e-14)) | 0x8000;
  33. shifttable[i|0x000] = -e-1;
  34. shifttable[i|0x100] = -e-1;
  35. } else if (e <= 15) { // Normal numbers just lose precision
  36. basetable[i|0x000] = ((e + 15) << 10);
  37. basetable[i|0x100] = ((e + 15) << 10) | 0x8000;
  38. shifttable[i|0x000] = 13;
  39. shifttable[i|0x100] = 13;
  40. } else if (e < 128) { // Large numbers map to Infinity
  41. basetable[i|0x000] = 0x7C00;
  42. basetable[i|0x100] = 0xFC00;
  43. shifttable[i|0x000] = 24;
  44. shifttable[i|0x100] = 24;
  45. } else{ // Infinity and NaN's stay Infinity and NaN's
  46. basetable[i|0x000] = 0x7C00;
  47. basetable[i|0x100] = 0xFC00;
  48. shifttable[i|0x000] = 13;
  49. shifttable[i|0x100] = 13;
  50. }
  51. }
  52. }
  53. static uint16_t float2half(uint32_t f, uint16_t *basetable, uint8_t *shifttable)
  54. {
  55. uint16_t h;
  56. h = basetable[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> shifttable[(f >> 23) & 0x1ff]);
  57. return h;
  58. }
  59. #endif /* AVCODEC_FLOAT2HALF_H */