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.

75 lines
2.3KB

  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_HALF2FLOAT_H
  19. #define AVCODEC_HALF2FLOAT_H
  20. #include <stdint.h>
  21. static uint32_t convertmantissa(uint32_t i)
  22. {
  23. uint32_t m = i << 13; // Zero pad mantissa bits
  24. uint32_t e = 0; // Zero exponent
  25. while (!(m & 0x00800000UL)){ // While not normalized
  26. e -= 0x00800000UL; // Decrement exponent (1<<23)
  27. m <<= 1; // Shift mantissa
  28. }
  29. m &= ~0x00800000UL; // Clear leading 1 bit
  30. e += 0x38800000UL; // Adjust bias ((127-14)<<23)
  31. return m | e; // Return combined number
  32. }
  33. static void half2float_table(uint32_t *mantissatable, uint32_t *exponenttable,
  34. uint16_t *offsettable)
  35. {
  36. mantissatable[0] = 0;
  37. for (int i = 1; i < 1024; i++)
  38. mantissatable[i] = convertmantissa(i);
  39. for (int i = 1024; i < 2048; i++)
  40. mantissatable[i] = 0x38000000UL + ((i - 1024) << 13UL);
  41. exponenttable[0] = 0;
  42. for (int i = 1; i < 31; i++)
  43. exponenttable[i] = i << 23;
  44. for (int i = 33; i < 63; i++)
  45. exponenttable[i] = 0x80000000UL + ((i - 32) << 23UL);
  46. exponenttable[31]= 0x47800000UL;
  47. exponenttable[32]= 0x80000000UL;
  48. exponenttable[63]= 0xC7800000UL;
  49. offsettable[0] = 0;
  50. for (int i = 1; i < 64; i++)
  51. offsettable[i] = 1024;
  52. offsettable[32] = 0;
  53. }
  54. static uint32_t half2float(uint16_t h, uint32_t *mantissatable, uint32_t *exponenttable,
  55. uint16_t *offsettable)
  56. {
  57. uint32_t f;
  58. f = mantissatable[offsettable[h >> 10] + (h & 0x3ff)] + exponenttable[h >> 10];
  59. return f;
  60. }
  61. #endif /* AVCODEC_HALF2FLOAT_H */