dtoa.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Tiny float64 printing and parsing library
  3. *
  4. * Copyright (c) 2024 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. //#define JS_DTOA_DUMP_STATS
  25. /* maximum number of digits for fixed and frac formats */
  26. #define JS_DTOA_MAX_DIGITS 101
  27. /* radix != 10 is only supported with flags = JS_DTOA_FORMAT_FREE */
  28. /* use as many digits as necessary */
  29. #define JS_DTOA_FORMAT_FREE (0 << 0)
  30. /* use n_digits significant digits (1 <= n_digits <= JS_DTOA_MAX_DIGITS) */
  31. #define JS_DTOA_FORMAT_FIXED (1 << 0)
  32. /* force fractional format: [-]dd.dd with n_digits fractional digits.
  33. 0 <= n_digits <= JS_DTOA_MAX_DIGITS */
  34. #define JS_DTOA_FORMAT_FRAC (2 << 0)
  35. #define JS_DTOA_FORMAT_MASK (3 << 0)
  36. /* select exponential notation either in fixed or free format */
  37. #define JS_DTOA_EXP_AUTO (0 << 2)
  38. #define JS_DTOA_EXP_ENABLED (1 << 2)
  39. #define JS_DTOA_EXP_DISABLED (2 << 2)
  40. #define JS_DTOA_EXP_MASK (3 << 2)
  41. #define JS_DTOA_MINUS_ZERO (1 << 4) /* show the minus sign for -0 */
  42. /* only accepts integers (no dot, no exponent) */
  43. #define JS_ATOD_INT_ONLY (1 << 0)
  44. /* accept Oo and Ob prefixes in addition to 0x prefix if radix = 0 */
  45. #define JS_ATOD_ACCEPT_BIN_OCT (1 << 1)
  46. /* accept O prefix as octal if radix == 0 and properly formed (Annex B) */
  47. #define JS_ATOD_ACCEPT_LEGACY_OCTAL (1 << 2)
  48. /* accept _ between digits as a digit separator */
  49. #define JS_ATOD_ACCEPT_UNDERSCORES (1 << 3)
  50. typedef struct {
  51. uint64_t mem[37];
  52. } JSDTOATempMem;
  53. typedef struct {
  54. uint64_t mem[27];
  55. } JSATODTempMem;
  56. /* return a maximum bound of the string length */
  57. int js_dtoa_max_len(double d, int radix, int n_digits, int flags);
  58. /* return the string length */
  59. int js_dtoa(char *buf, double d, int radix, int n_digits, int flags,
  60. JSDTOATempMem *tmp_mem);
  61. double js_atod(const char *str, const char **pnext, int radix, int flags,
  62. JSATODTempMem *tmp_mem);
  63. #ifdef JS_DTOA_DUMP_STATS
  64. void js_dtoa_dump_stats(void);
  65. #endif
  66. /* additional exported functions */
  67. size_t u32toa(char *buf, uint32_t n);
  68. size_t i32toa(char *buf, int32_t n);
  69. size_t u64toa(char *buf, uint64_t n);
  70. size_t i64toa(char *buf, int64_t n);
  71. size_t u64toa_radix(char *buf, uint64_t n, unsigned int radix);
  72. size_t i64toa_radix(char *buf, int64_t n, unsigned int radix);