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.

101 lines
2.9KB

  1. /*
  2. * portable IEEE float/double read/write functions
  3. *
  4. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * portable IEEE float/double read/write functions
  25. */
  26. #include <stdint.h>
  27. #include "mathematics.h"
  28. #include "intfloat_readwrite.h"
  29. #include "version.h"
  30. #if FF_API_INTFLOAT
  31. double av_int2dbl(int64_t v){
  32. if((uint64_t)v+v > 0xFFEULL<<52)
  33. return NAN;
  34. return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
  35. }
  36. float av_int2flt(int32_t v){
  37. if((uint32_t)v+v > 0xFF000000U)
  38. return NAN;
  39. return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
  40. }
  41. double av_ext2dbl(const AVExtFloat ext){
  42. uint64_t m = 0;
  43. int e, i;
  44. for (i = 0; i < 8; i++)
  45. m = (m<<8) + ext.mantissa[i];
  46. e = (((int)ext.exponent[0]&0x7f)<<8) | ext.exponent[1];
  47. if (e == 0x7fff && m)
  48. return NAN;
  49. e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
  50. * mantissa bit is written as opposed to the
  51. * single and double precision formats. */
  52. if (ext.exponent[0]&0x80)
  53. m= -m;
  54. return ldexp(m, e);
  55. }
  56. int64_t av_dbl2int(double d){
  57. int e;
  58. if ( !d) return 0;
  59. else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
  60. d= frexp(d, &e);
  61. return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
  62. }
  63. int32_t av_flt2int(float d){
  64. int e;
  65. if ( !d) return 0;
  66. else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
  67. d= frexp(d, &e);
  68. return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
  69. }
  70. AVExtFloat av_dbl2ext(double d){
  71. struct AVExtFloat ext= {{0}};
  72. int e, i; double f; uint64_t m;
  73. f = fabs(frexp(d, &e));
  74. if (f >= 0.5 && f < 1) {
  75. e += 16382;
  76. ext.exponent[0] = e>>8;
  77. ext.exponent[1] = e;
  78. m = (uint64_t)ldexp(f, 64);
  79. for (i=0; i < 8; i++)
  80. ext.mantissa[i] = m>>(56-(i<<3));
  81. } else if (f != 0.0) {
  82. ext.exponent[0] = 0x7f; ext.exponent[1] = 0xff;
  83. if (f != INFINITY)
  84. ext.mantissa[0] = ~0;
  85. }
  86. if (d < 0)
  87. ext.exponent[0] |= 0x80;
  88. return ext;
  89. }
  90. #endif /* FF_API_INTFLOAT */