1 //===-- Inf or Nan Converter for printf -------------------------*- 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 LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_INF_NAN_CONVERTER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_INF_NAN_CONVERTER_H
11 
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "src/stdio/printf_core/converter_utils.h"
14 #include "src/stdio/printf_core/core_structs.h"
15 #include "src/stdio/printf_core/writer.h"
16 
17 #include <inttypes.h>
18 #include <stddef.h>
19 
20 namespace __llvm_libc {
21 namespace printf_core {
22 
23 using MantissaInt = fputil::FPBits<long double>::UIntType;
24 
convert_inf_nan(Writer * writer,const FormatSection & to_conv)25 int inline convert_inf_nan(Writer *writer, const FormatSection &to_conv) {
26   // All of the letters will be defined relative to variable a, which will be
27   // the appropriate case based on the case of the conversion.
28   const char a = (to_conv.conv_name & 32) | 'A';
29 
30   bool is_negative;
31   MantissaInt mantissa;
32   if (to_conv.length_modifier == LengthModifier::L) {
33     fputil::FPBits<long double>::UIntType float_raw = to_conv.conv_val_raw;
34     fputil::FPBits<long double> float_bits(float_raw);
35     is_negative = float_bits.get_sign();
36     mantissa = float_bits.get_explicit_mantissa();
37   } else {
38     fputil::FPBits<double>::UIntType float_raw = to_conv.conv_val_raw;
39     fputil::FPBits<double> float_bits(float_raw);
40     is_negative = float_bits.get_sign();
41     mantissa = float_bits.get_explicit_mantissa();
42   }
43 
44   char sign_char = 0;
45 
46   if (is_negative)
47     sign_char = '-';
48   else if ((to_conv.flags & FormatFlags::FORCE_SIGN) == FormatFlags::FORCE_SIGN)
49     sign_char = '+'; // FORCE_SIGN has precedence over SPACE_PREFIX
50   else if ((to_conv.flags & FormatFlags::SPACE_PREFIX) ==
51            FormatFlags::SPACE_PREFIX)
52     sign_char = ' ';
53 
54   // Both "inf" and "nan" are the same number of characters, being 3.
55   int padding = to_conv.min_width - (sign_char > 0 ? 1 : 0) - 3;
56 
57   // The right justified pattern is (spaces), (sign), inf/nan
58   // The left justified pattern is  (sign), inf/nan, (spaces)
59 
60   if (padding > 0 && ((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) !=
61                       FormatFlags::LEFT_JUSTIFIED))
62     RET_IF_RESULT_NEGATIVE(writer->write_chars(' ', padding));
63 
64   if (sign_char)
65     RET_IF_RESULT_NEGATIVE(writer->write(&sign_char, 1));
66   if (mantissa == 0) { // inf
67     RET_IF_RESULT_NEGATIVE(writer->write((a == 'a' ? "inf" : "INF"), 3));
68   } else { // nan
69     RET_IF_RESULT_NEGATIVE(writer->write((a == 'a' ? "nan" : "NAN"), 3));
70   }
71 
72   if (padding > 0 && ((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) ==
73                       FormatFlags::LEFT_JUSTIFIED))
74     RET_IF_RESULT_NEGATIVE(writer->write_chars(' ', padding));
75 
76   return WRITE_OK;
77 }
78 
79 } // namespace printf_core
80 } // namespace __llvm_libc
81 
82 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_INF_NAN_CONVERTER_H
83