1 //===-- runtime/edit-output.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 #ifndef FORTRAN_RUNTIME_EDIT_OUTPUT_H_ 10 #define FORTRAN_RUNTIME_EDIT_OUTPUT_H_ 11 12 // Output data editing templates implementing the FORMAT data editing 13 // descriptors E, EN, ES, EX, D, F, and G for REAL data (and COMPLEX 14 // components, I and G for INTEGER, and B/O/Z for both. 15 // See subclauses in 13.7.2.3 of Fortran 2018 for the 16 // detailed specifications of these descriptors. 17 // List-directed output (13.10.4) for numeric types is also done here. 18 // Drives the same fast binary-to-decimal formatting templates used 19 // in the f18 front-end. 20 21 #include "format.h" 22 #include "io-stmt.h" 23 #include "flang/Common/uint128.h" 24 #include "flang/Decimal/decimal.h" 25 26 namespace Fortran::runtime::io { 27 28 // I, B, O, Z, and G output editing for INTEGER. 29 // The DataEdit reference is const here (and elsewhere in this header) so that 30 // one edit descriptor with a repeat factor may safely serve to edit 31 // multiple elements of an array. 32 template <typename INT = std::int64_t, typename UINT = std::uint64_t> 33 bool EditIntegerOutput(IoStatementState &, const DataEdit &, INT); 34 35 // Encapsulates the state of a REAL output conversion. 36 class RealOutputEditingBase { 37 protected: 38 explicit RealOutputEditingBase(IoStatementState &io) : io_{io} {} 39 40 static bool IsDecimalNumber(const char *p) { 41 if (!p) { 42 return false; 43 } 44 if (*p == '-' || *p == '+') { 45 ++p; 46 } 47 return *p >= '0' && *p <= '9'; 48 } 49 50 const char *FormatExponent(int, const DataEdit &edit, int &length); 51 bool EmitPrefix(const DataEdit &, std::size_t length, std::size_t width); 52 bool EmitSuffix(const DataEdit &); 53 54 IoStatementState &io_; 55 int trailingBlanks_{0}; // created when Gw editing maps to Fw 56 char exponent_[16]; 57 }; 58 59 template <int binaryPrecision = 53> 60 class RealOutputEditing : public RealOutputEditingBase { 61 public: 62 template <typename A> 63 RealOutputEditing(IoStatementState &io, A x) 64 : RealOutputEditingBase{io}, x_{x} {} 65 bool Edit(const DataEdit &); 66 67 private: 68 using BinaryFloatingPoint = 69 decimal::BinaryFloatingPointNumber<binaryPrecision>; 70 71 // The DataEdit arguments here are const references or copies so that 72 // the original DataEdit can safely serve multiple array elements when 73 // it has a repeat count. 74 bool EditEorDOutput(const DataEdit &); 75 bool EditFOutput(const DataEdit &); 76 DataEdit EditForGOutput(DataEdit); // returns an E or F edit 77 bool EditEXOutput(const DataEdit &); 78 bool EditListDirectedOutput(const DataEdit &); 79 80 bool IsZero() const { return x_.IsZero(); } 81 82 decimal::ConversionToDecimalResult Convert( 83 int significantDigits, const DataEdit &, int flags = 0); 84 85 BinaryFloatingPoint x_; 86 char buffer_[BinaryFloatingPoint::maxDecimalConversionDigits + 87 EXTRA_DECIMAL_CONVERSION_SPACE]; 88 }; 89 90 bool ListDirectedLogicalOutput( 91 IoStatementState &, ListDirectedStatementState<Direction::Output> &, bool); 92 bool EditLogicalOutput(IoStatementState &, const DataEdit &, bool); 93 bool ListDirectedDefaultCharacterOutput(IoStatementState &, 94 ListDirectedStatementState<Direction::Output> &, const char *, std::size_t); 95 bool EditDefaultCharacterOutput( 96 IoStatementState &, const DataEdit &, const char *, std::size_t); 97 98 extern template bool EditIntegerOutput<std::int64_t, std::uint64_t>( 99 IoStatementState &, const DataEdit &, std::int64_t); 100 extern template bool EditIntegerOutput<common::uint128_t, common::uint128_t>( 101 IoStatementState &, const DataEdit &, common::uint128_t); 102 103 extern template class RealOutputEditing<8>; 104 extern template class RealOutputEditing<11>; 105 extern template class RealOutputEditing<24>; 106 extern template class RealOutputEditing<53>; 107 extern template class RealOutputEditing<64>; 108 extern template class RealOutputEditing<113>; 109 110 } // namespace Fortran::runtime::io 111 #endif // FORTRAN_RUNTIME_EDIT_OUTPUT_H_ 112