eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
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.

ieee80.c 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2022 Ulrich Müller <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Alternatively, at your option, you can distribute this file under
  19. * the terms of the 2-clause BSD license.
  20. */
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include "ieee80.h"
  24. #ifndef INFINITY
  25. # define INFINITY 0.
  26. #endif
  27. #ifndef NAN
  28. # define NAN 0.
  29. #endif
  30. /*
  31. * Convert an IEEE 754 80-bit extended precision floating-point number
  32. * to a double. Input is expected as 10 bytes in big-endian order.
  33. *
  34. * Implemented according to the format described in:
  35. * https://en.wikipedia.org/wiki/Extended_precision
  36. * https://en.wikipedia.org/wiki/IEEE_754
  37. */
  38. double
  39. ieee_extended_to_double(const unsigned char *bytes)
  40. {
  41. int sign, exp, i;
  42. uint64_t mant;
  43. double ret;
  44. sign = (bytes[0] & 0x80) != 0;
  45. exp = (bytes[0] & 0x7f) << 8 | bytes[1];
  46. /* Unfortunately, there is no 64-bit variant of ntohl(), and we
  47. cannot use be64toh() either, because it is nonstandard */
  48. mant = 0;
  49. for (i = 2; i < 10; i++)
  50. mant = (mant << 8) | bytes[i];
  51. switch (exp) {
  52. case 0: /* zero or denormalized number */
  53. ret = (mant == 0) ? 0. : ldexp(mant, - (16382 + 63));
  54. break;
  55. case 0x7fff: /* infinity or not a number */
  56. /* Convert infinity to INFINITY, and anything else
  57. (signalling NaN, quiet NaN, indefinite) to NAN */
  58. ret = ((mant & 0x7fffffffffffffff) == 0) ? INFINITY : NAN;
  59. break;
  60. default:
  61. ret = ldexp(mant, exp - (16383 + 63));
  62. }
  63. if (sign) ret = -ret;
  64. return ret;
  65. }