| 1 | // Copyright 2018 Ulf Adams |
| 2 | // |
| 3 | // The contents of this file may be used under the terms of the Apache License, |
| 4 | // Version 2.0. |
| 5 | // |
| 6 | // (See accompanying file LICENSE-Apache or copy at |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0) |
| 8 | // |
| 9 | // Alternatively, the contents of this file may be used under the terms of |
| 10 | // the Boost Software License, Version 1.0. |
| 11 | // (See accompanying file LICENSE-Boost or copy at |
| 12 | // https://www.boost.org/LICENSE_1_0.txt) |
| 13 | // |
| 14 | // Unless required by applicable law or agreed to in writing, this software |
| 15 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | // KIND, either express or implied. |
| 17 | #ifndef RYU_GENERIC_128_H |
| 18 | #define RYU_GENERIC_128_H |
| 19 | |
| 20 | |
| 21 | // NOTE: These symbols are declared extern "C" upstream, but we don't want that |
| 22 | // because it'd override the internal linkage of the anonymous namespace into |
| 23 | // which this header is included. |
| 24 | |
| 25 | // This is a generic 128-bit implementation of float to shortest conversion |
| 26 | // using the Ryu algorithm. It can handle any IEEE-compatible floating-point |
| 27 | // type up to 128 bits. In order to use this correctly, you must use the |
| 28 | // appropriate *_to_fd128 function for the underlying type - DO NOT CAST your |
| 29 | // input to another floating-point type, doing so will result in incorrect |
| 30 | // output! |
| 31 | // |
| 32 | // For any floating-point type that is not natively defined by the compiler, |
| 33 | // you can use generic_binary_to_decimal to work directly on the underlying bit |
| 34 | // representation. |
| 35 | |
| 36 | #define FD128_EXCEPTIONAL_EXPONENT 0x7FFFFFFF |
| 37 | |
| 38 | // A floating decimal representing (-1)^s * m * 10^e. |
| 39 | struct floating_decimal_128 { |
| 40 | uint128_t mantissa; |
| 41 | int32_t exponent; |
| 42 | bool sign; |
| 43 | }; |
| 44 | |
| 45 | // Converts the given decimal floating point number to a string, writing to result, and returning |
| 46 | // the number characters written. Does not terminate the buffer with a 0. In the worst case, this |
| 47 | // function can write up to 53 characters. |
| 48 | // |
| 49 | // Maximal char buffer requirement: |
| 50 | // sign + mantissa digits + decimal dot + 'E' + exponent sign + exponent digits |
| 51 | // = 1 + 39 + 1 + 1 + 1 + 10 = 53 |
| 52 | int generic_to_chars(const struct floating_decimal_128 v, char* const result); |
| 53 | |
| 54 | |
| 55 | #endif // RYU_GENERIC_128_H |