FLANG
decimal.h
1/*===-- include/flang/Decimal/decimal.h ---------------------------*- C++ -*-===
2 *
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 *
7 * ===-----------------------------------------------------------------------===
8 */
9
10/* C and C++ API for binary-to/from-decimal conversion package. */
11
12#ifndef FORTRAN_DECIMAL_DECIMAL_H_
13#define FORTRAN_DECIMAL_DECIMAL_H_
14
15#include "flang/Common/api-attrs.h"
16#include <stddef.h>
17
18#ifdef __cplusplus
19// Binary-to-decimal conversions (formatting) produce a sequence of decimal
20// digit characters in a NUL-terminated user-supplied buffer that constitute
21// a decimal fraction (or zero), accompanied by a decimal exponent that
22// you'll get to adjust and format yourself. There can be a leading sign
23// character.
24// Negative zero is "-0". The result can also be "NaN", "Inf", "+Inf",
25// or "-Inf".
26// If the conversion can't fit in the user-supplied buffer, a null pointer
27// is returned.
28
29#include "binary-floating-point.h"
30namespace Fortran::decimal {
31#endif /* C++ */
32
33enum ConversionResultFlags {
34 Exact = 0,
35 Overflow = 1,
36 Inexact = 2,
37 Invalid = 4,
38 Underflow = 8,
39};
40
42 const char *str; /* may not be original buffer pointer; null if overflow */
43 size_t length; /* does not include NUL terminator */
44 int decimalExponent; /* assuming decimal point to the left of first digit */
45 enum ConversionResultFlags flags;
46};
47
48/* The "minimize" flag causes the fewest number of output digits
49 * to be emitted such that reading them back into the same binary
50 * floating-point format with RoundNearest will return the same
51 * value.
52 */
53enum DecimalConversionFlags {
54 Minimize = 1, /* Minimize # of digits */
55 AlwaysSign = 2, /* emit leading '+' if not negative */
56};
57
58/*
59 * When allocating decimal conversion output buffers, use the maximum
60 * number of significant decimal digits in the representation of the
61 * least nonzero value, and add this extra space for a sign, a NUL, and
62 * some extra due to the library working internally in base 10**16
63 * and computing its output size in multiples of 16.
64 */
65#define EXTRA_DECIMAL_CONVERSION_SPACE (1 + 1 + 2 * 16 - 1)
66
67#ifdef __cplusplus
68
69RT_OFFLOAD_API_GROUP_BEGIN
70
71template <int PREC>
72RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal(char *, size_t,
73 DecimalConversionFlags, int digits, enum FortranRounding rounding,
75
76extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<8>(
77 char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
79extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<11>(
80 char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
82extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<24>(
83 char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
85extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<53>(
86 char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
88extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<64>(
89 char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
91extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<113>(
92 char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
94
95template <int PREC> struct ConversionToBinaryResult {
97 enum ConversionResultFlags flags { Exact };
98};
99
100template <int PREC>
101RT_API_ATTRS ConversionToBinaryResult<PREC> ConvertToBinary(const char *&,
102 enum FortranRounding = RoundNearest, const char *end = nullptr);
103
104extern template RT_API_ATTRS ConversionToBinaryResult<8> ConvertToBinary<8>(
105 const char *&, enum FortranRounding, const char *end);
106extern template RT_API_ATTRS ConversionToBinaryResult<11> ConvertToBinary<11>(
107 const char *&, enum FortranRounding, const char *end);
108extern template RT_API_ATTRS ConversionToBinaryResult<24> ConvertToBinary<24>(
109 const char *&, enum FortranRounding, const char *end);
110extern template RT_API_ATTRS ConversionToBinaryResult<53> ConvertToBinary<53>(
111 const char *&, enum FortranRounding, const char *end);
112extern template RT_API_ATTRS ConversionToBinaryResult<64> ConvertToBinary<64>(
113 const char *&, enum FortranRounding, const char *end);
114extern template RT_API_ATTRS ConversionToBinaryResult<113> ConvertToBinary<113>(
115 const char *&, enum FortranRounding, const char *end);
116
117RT_OFFLOAD_API_GROUP_END
118
119} // namespace Fortran::decimal
120extern "C" {
121#define NS(x) Fortran::decimal::x
122#else /* C++ */
123#define NS(x) x
124#endif /* C++ */
125
126RT_API_ATTRS struct NS(ConversionToDecimalResult)
127 ConvertFloatToDecimal(char *, size_t, enum NS(DecimalConversionFlags),
128 int digits, enum NS(FortranRounding), float);
129RT_API_ATTRS struct NS(ConversionToDecimalResult)
130 ConvertDoubleToDecimal(char *, size_t, enum NS(DecimalConversionFlags),
131 int digits, enum NS(FortranRounding), double);
132RT_API_ATTRS struct NS(ConversionToDecimalResult)
133 ConvertLongDoubleToDecimal(char *, size_t, enum NS(DecimalConversionFlags),
134 int digits, enum NS(FortranRounding), long double);
135
136RT_API_ATTRS enum NS(ConversionResultFlags)
137 ConvertDecimalToFloat(const char **, float *, enum NS(FortranRounding));
138RT_API_ATTRS enum NS(ConversionResultFlags)
139 ConvertDecimalToDouble(const char **, double *, enum NS(FortranRounding));
140RT_API_ATTRS enum NS(ConversionResultFlags) ConvertDecimalToLongDouble(
141 const char **, long double *, enum NS(FortranRounding));
142#undef NS
143#ifdef __cplusplus
144} // extern "C"
145#endif
146#endif
Definition binary-floating-point.h:34
Definition decimal.h:41