46 changes: 25 additions & 21 deletions libc/src/__support/float_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ constexpr size_t MID_INT_SIZE = 192;
namespace internal {

// Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
constexpr inline uint32_t log10_pow2(const uint32_t e) {
LIBC_INLINE constexpr uint32_t log10_pow2(const uint32_t e) {
// The first value this approximation fails for is 2^1651 which is just
// greater than 10^297. assert(e >= 0); assert(e <= 1650);
return (e * 78913) >> 18;
Expand All @@ -69,14 +69,14 @@ constexpr inline uint32_t log10_pow2(const uint32_t e) {
// power of 2 was also a power of 10, but since that doesn't exist this is
// always accurate. This is used to calculate the maximum number of base-10
// digits a given e-bit number could have.
constexpr inline uint32_t ceil_log10_pow2(const uint32_t e) {
LIBC_INLINE constexpr uint32_t ceil_log10_pow2(const uint32_t e) {
return log10_pow2(e) + 1;
}

// Returns the maximum number of 9 digit blocks a number described by the given
// index (which is ceil(exponent/16)) and mantissa width could need.
constexpr inline uint32_t length_for_num(const uint32_t idx,
const uint32_t mantissa_width) {
LIBC_INLINE constexpr uint32_t length_for_num(const uint32_t idx,
const uint32_t mantissa_width) {
//+8 to round up when dividing by 9
return (ceil_log10_pow2(16 * idx) + ceil_log10_pow2(mantissa_width) +
(BLOCK_SIZE - 1)) /
Expand All @@ -90,7 +90,7 @@ constexpr inline uint32_t length_for_num(const uint32_t idx,
// floor(5^(-9i) * 2^(e + c_1 - 9i) + 1) % (10^9 * 2^c_1)

template <size_t INT_SIZE>
constexpr inline cpp::UInt<MID_INT_SIZE>
LIBC_INLINE constexpr cpp::UInt<MID_INT_SIZE>
get_table_positive(int exponent, size_t i, const size_t constant) {
// INT_SIZE is the size of int that is used for the internal calculations of
// this function. It should be large enough to hold 2^(exponent+constant), so
Expand Down Expand Up @@ -131,8 +131,8 @@ get_table_positive(int exponent, size_t i, const size_t constant) {
// calculations.
// The formula being used looks more like this:
// floor(10^(9*(-i)) * 2^(c_0 + (-e))) % (10^9 * 2^c_0)
inline cpp::UInt<MID_INT_SIZE> get_table_negative(int exponent, size_t i,
const size_t constant) {
LIBC_INLINE cpp::UInt<MID_INT_SIZE> get_table_negative(int exponent, size_t i,
const size_t constant) {
constexpr size_t INT_SIZE = 1024;
int shift_amount = constant - exponent;
cpp::UInt<INT_SIZE> num(1);
Expand Down Expand Up @@ -234,7 +234,7 @@ class FloatToString {
// constexpr void init_convert();

public:
constexpr FloatToString(T init_float) : float_bits(init_float) {
LIBC_INLINE constexpr FloatToString(T init_float) : float_bits(init_float) {
is_negative = float_bits.get_sign();
exponent = float_bits.get_exponent();
mantissa = float_bits.get_explicit_mantissa();
Expand All @@ -254,13 +254,15 @@ class FloatToString {
// init_convert();
}

constexpr bool is_nan() { return float_bits.is_nan(); }
constexpr bool is_inf() { return float_bits.is_inf(); }
constexpr bool is_inf_or_nan() { return float_bits.is_inf_or_nan(); }
LIBC_INLINE constexpr bool is_nan() { return float_bits.is_nan(); }
LIBC_INLINE constexpr bool is_inf() { return float_bits.is_inf(); }
LIBC_INLINE constexpr bool is_inf_or_nan() {
return float_bits.is_inf_or_nan();
}

// get_block returns an integer that represents the digits in the requested
// block.
constexpr BlockInt get_positive_block(int block_index) {
LIBC_INLINE constexpr BlockInt get_positive_block(int block_index) {
if (exponent >= -MANT_WIDTH) {
// idx is ceil(exponent/16) or 0 if exponent is negative. This is used to
// find the coarse section of the POW10_SPLIT table that will be used to
Expand All @@ -285,7 +287,8 @@ class FloatToString {
return 0;
}
}
constexpr BlockInt get_negative_block(int block_index) {

LIBC_INLINE constexpr BlockInt get_negative_block(int block_index) {
if (exponent < 0) {
const int32_t idx = -exponent / 16;
uint32_t i = block_index;
Expand All @@ -312,15 +315,15 @@ class FloatToString {
}
}

constexpr BlockInt get_block(int block_index) {
LIBC_INLINE constexpr BlockInt get_block(int block_index) {
if (block_index >= 0) {
return get_positive_block(block_index);
} else {
return get_negative_block(-1 - block_index);
}
}

constexpr size_t get_positive_blocks() {
LIBC_INLINE constexpr size_t get_positive_blocks() {
if (exponent >= -MANT_WIDTH) {
const uint32_t idx =
exponent < 0 ? 0 : static_cast<uint32_t>(exponent + 15) / 16;
Expand All @@ -333,14 +336,14 @@ class FloatToString {

// This takes the index of a block after the decimal point (a negative block)
// and return if it's sure that all of the digits after it are zero.
constexpr bool is_lowest_block(size_t block_index) {
LIBC_INLINE constexpr bool is_lowest_block(size_t block_index) {
const int32_t idx = -exponent / 16;
const uint32_t p = POW10_OFFSET_2[idx] + block_index - MIN_BLOCK_2[idx];
// If the remaining digits are all 0, then this is the lowest block.
return p >= POW10_OFFSET_2[idx + 1];
}

constexpr size_t zero_blocks_after_point() {
LIBC_INLINE constexpr size_t zero_blocks_after_point() {
return MIN_BLOCK_2[-exponent / 16];
}
};
Expand All @@ -355,17 +358,18 @@ class FloatToString {
// }

template <>
constexpr size_t FloatToString<long double>::zero_blocks_after_point() {
LIBC_INLINE constexpr size_t
FloatToString<long double>::zero_blocks_after_point() {
return 0;
}

template <>
constexpr bool FloatToString<long double>::is_lowest_block(size_t) {
LIBC_INLINE constexpr bool FloatToString<long double>::is_lowest_block(size_t) {
return false;
}

template <>
constexpr BlockInt
LIBC_INLINE constexpr BlockInt
FloatToString<long double>::get_positive_block(int block_index) {
if (exponent >= -MANT_WIDTH) {
const uint32_t pos_exp = (exponent < 0 ? 0 : exponent);
Expand Down Expand Up @@ -401,7 +405,7 @@ FloatToString<long double>::get_positive_block(int block_index) {
}

template <>
constexpr BlockInt
LIBC_INLINE constexpr BlockInt
FloatToString<long double>::get_negative_block(int block_index) {
if (exponent < 0) {
const int32_t idx = -exponent / 16;
Expand Down
46 changes: 26 additions & 20 deletions libc/src/__support/integer_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"

namespace __llvm_libc {

Expand Down Expand Up @@ -44,10 +45,10 @@ namespace __llvm_libc {
// char b30buf[IntegerToString::bufsize<30, int>(a)];
// auto str = IntegerToString::convert<30>(a, b30buf);
class IntegerToString {
static cpp::string_view convert_uintmax(uintmax_t uval,
cpp::span<char> &buffer,
bool lowercase,
const uint8_t conv_base) {
LIBC_INLINE static cpp::string_view convert_uintmax(uintmax_t uval,
cpp::span<char> &buffer,
bool lowercase,
const uint8_t conv_base) {
const char a = lowercase ? 'a' : 'A';

size_t len = 0;
Expand All @@ -67,9 +68,10 @@ class IntegerToString {
return cpp::string_view(buffer.data() + buffer.size() - len, len);
}

static cpp::string_view convert_intmax(intmax_t val, cpp::span<char> &buffer,
bool lowercase,
const uint8_t conv_base) {
LIBC_INLINE static cpp::string_view convert_intmax(intmax_t val,
cpp::span<char> &buffer,
bool lowercase,
const uint8_t conv_base) {
if (val >= 0)
return convert_uintmax(uintmax_t(val), buffer, lowercase, conv_base);
uintmax_t uval = uintmax_t(-val);
Expand All @@ -80,7 +82,7 @@ class IntegerToString {
return cpp::string_view(buffer.data() + buffer.size() - len, len);
}

static constexpr inline size_t floor_log_2(size_t num) {
LIBC_INLINE static constexpr size_t floor_log_2(size_t num) {
size_t i = 0;
for (; num > 1; num /= 2) {
++i;
Expand Down Expand Up @@ -110,7 +112,8 @@ class IntegerToString {
// For other bases, we approximate by rounding down to the nearest power of
// two base, since the space needed is easy to calculate and it won't
// overestimate by too much.
template <uint8_t BASE, typename T> static constexpr size_t bufsize() {
template <uint8_t BASE, typename T>
LIBC_INLINE static constexpr size_t bufsize() {
constexpr size_t BITS_PER_DIGIT = floor_log_2(BASE);
constexpr size_t BUFSIZE_COMMON =
((sizeof(T) * 8 + (BITS_PER_DIGIT - 1)) / BITS_PER_DIGIT);
Expand All @@ -119,27 +122,27 @@ class IntegerToString {
(BASE == 10 ? BUFSIZE_BASE10 : BUFSIZE_COMMON);
}

template <typename T> static constexpr size_t dec_bufsize() {
template <typename T> LIBC_INLINE static constexpr size_t dec_bufsize() {
return bufsize<10, T>();
}

template <typename T> static constexpr size_t hex_bufsize() {
template <typename T> LIBC_INLINE static constexpr size_t hex_bufsize() {
return bufsize<16, T>();
}

template <typename T> static constexpr size_t oct_bufsize() {
template <typename T> LIBC_INLINE static constexpr size_t oct_bufsize() {
return bufsize<8, T>();
}

template <typename T> static constexpr size_t bin_bufsize() {
template <typename T> LIBC_INLINE static constexpr size_t bin_bufsize() {
return bufsize<2, T>();
}

template <uint8_t BASE, typename T,
cpp::enable_if_t<2 <= BASE && BASE <= 36 && cpp::is_integral_v<T>,
int> = 0>
static cpp::optional<cpp::string_view> convert(T val, cpp::span<char> buffer,
bool lowercase = true) {
LIBC_INLINE static cpp::optional<cpp::string_view>
convert(T val, cpp::span<char> buffer, bool lowercase = true) {
if (buffer.size() < bufsize<BASE, T>())
return cpp::optional<cpp::string_view>();
if (cpp::is_signed_v<T>)
Expand All @@ -149,23 +152,26 @@ class IntegerToString {
}

template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::string_view> dec(T val, cpp::span<char> buffer) {
LIBC_INLINE static cpp::optional<cpp::string_view>
dec(T val, cpp::span<char> buffer) {
return convert<10>(val, buffer);
}

template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::string_view> hex(T val, cpp::span<char> buffer,
bool lowercase = true) {
LIBC_INLINE static cpp::optional<cpp::string_view>
hex(T val, cpp::span<char> buffer, bool lowercase = true) {
return convert<16>(val, buffer, lowercase);
}

template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::string_view> oct(T val, cpp::span<char> buffer) {
LIBC_INLINE static cpp::optional<cpp::string_view>
oct(T val, cpp::span<char> buffer) {
return convert<8>(val, buffer);
}

template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::string_view> bin(T val, cpp::span<char> buffer) {
LIBC_INLINE static cpp::optional<cpp::string_view>
bin(T val, cpp::span<char> buffer) {
return convert<2>(val, buffer);
}
};
Expand Down
8 changes: 5 additions & 3 deletions libc/src/__support/integer_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_INTEGER_UTILS_H
#define LLVM_LIBC_SRC_SUPPORT_INTEGER_UTILS_H

#include "CPP/type_traits.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"

#include "builtin_wrappers.h"
#include "number_pair.h"

Expand All @@ -20,7 +22,7 @@ namespace __llvm_libc {
template <typename T> NumberPair<T> full_mul(T a, T b);

template <>
inline NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a, uint32_t b) {
LIBC_INLINE NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a, uint32_t b) {
uint64_t prod = uint64_t(a) * uint64_t(b);
NumberPair<uint32_t> result;
result.lo = uint32_t(prod);
Expand All @@ -29,7 +31,7 @@ inline NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a, uint32_t b) {
}

template <>
inline NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a, uint64_t b) {
LIBC_INLINE NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a, uint64_t b) {
#ifdef __SIZEOF_INT128__
__uint128_t prod = __uint128_t(a) * __uint128_t(b);
NumberPair<uint64_t> result;
Expand Down
5 changes: 3 additions & 2 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ template <class T> LIBC_INLINE void set_implicit_bit(fputil::FPBits<T> &) {

#if defined(SPECIAL_X86_LONG_DOUBLE)
template <>
inline void set_implicit_bit<long double>(fputil::FPBits<long double> &result) {
LIBC_INLINE void
set_implicit_bit<long double>(fputil::FPBits<long double> &result) {
result.set_implicit_bit(result.get_unbiased_exponent() != 0);
}
#endif
Expand Down Expand Up @@ -190,7 +191,7 @@ eisel_lemire(typename fputil::FPBits<T>::UIntType mantissa, int32_t exp10,

#if !defined(LONG_DOUBLE_IS_DOUBLE)
template <>
inline bool eisel_lemire<long double>(
LIBC_INLINE bool eisel_lemire<long double>(
typename fputil::FPBits<long double>::UIntType mantissa, int32_t exp10,
typename fputil::FPBits<long double>::UIntType *outputMantissa,
uint32_t *outputExp2) {
Expand Down
1 change: 1 addition & 0 deletions libc/src/stdio/printf_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_object_library(
libc.src.__support.CPP.bit
libc.src.__support.CPP.string_view
libc.src.__support.CPP.type_traits
libc.src.__support.common
)

add_object_library(
Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/printf_core/char_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"

namespace __llvm_libc {
namespace printf_core {

int inline convert_char(Writer *writer, const FormatSection &to_conv) {
LIBC_INLINE int convert_char(Writer *writer, const FormatSection &to_conv) {
char c = to_conv.conv_val_raw;

constexpr int string_len = 1;
Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/printf_core/converter_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H

#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/core_structs.h"

#include <inttypes.h>
Expand All @@ -18,7 +19,7 @@
namespace __llvm_libc {
namespace printf_core {

inline uintmax_t apply_length_modifier(uintmax_t num, LengthModifier lm) {
LIBC_INLINE uintmax_t apply_length_modifier(uintmax_t num, LengthModifier lm) {
switch (lm) {
case LengthModifier::none:
return num & cpp::numeric_limits<unsigned int>::max();
Expand Down
15 changes: 8 additions & 7 deletions libc/src/stdio/printf_core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "src/__support/CPP/type_traits.h"
#include "src/__support/arg_list.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/printf_config.h"

Expand Down Expand Up @@ -39,7 +40,7 @@ class Parser {
struct TypeDesc {
uint8_t size;
PrimaryType primary_type;
constexpr bool operator==(const TypeDesc &other) const {
LIBC_INLINE constexpr bool operator==(const TypeDesc &other) const {
return (size == other.size) && (primary_type == other.primary_type);
}
};
Expand All @@ -60,10 +61,10 @@ class Parser {

public:
#ifndef LLVM_LIBC_PRINTF_DISABLE_INDEX_MODE
Parser(const char *__restrict new_str, internal::ArgList &args)
LIBC_INLINE Parser(const char *__restrict new_str, internal::ArgList &args)
: str(new_str), args_cur(args), args_start(args) {}
#else
Parser(const char *__restrict new_str, internal::ArgList &args)
LIBC_INLINE Parser(const char *__restrict new_str, internal::ArgList &args)
: str(new_str), args_cur(args) {}
#endif // LLVM_LIBC_PRINTF_DISABLE_INDEX_MODE

Expand All @@ -87,7 +88,7 @@ class Parser {
LengthModifier parse_length_modifier(size_t *local_pos);

// get_next_arg_value gets the next value from the arg list as type T.
template <class T> T inline get_next_arg_value() {
template <class T> LIBC_INLINE T get_next_arg_value() {
return args_cur.next_var<T>();
}

Expand All @@ -104,7 +105,7 @@ class Parser {
// local_pos.
size_t parse_index(size_t *local_pos);

template <typename T> static constexpr TypeDesc get_type_desc() {
template <typename T> LIBC_INLINE static constexpr TypeDesc get_type_desc() {
if constexpr (cpp::is_same_v<T, void>) {
return TypeDesc{0, PrimaryType::Integer};
} else {
Expand All @@ -117,15 +118,15 @@ class Parser {
}
}

void inline set_type_desc(size_t index, TypeDesc value) {
LIBC_INLINE void set_type_desc(size_t index, TypeDesc value) {
if (index != 0 && index <= DESC_ARR_LEN)
desc_arr[index - 1] = value;
}

// get_arg_value gets the value from the arg list at index (starting at 1).
// This may require parsing the format string. An index of 0 is interpreted as
// the next value.
template <class T> T inline get_arg_value(size_t index) {
template <class T> LIBC_INLINE T get_arg_value(size_t index) {
if (!(index == 0 || index == args_index))
args_to_index(index);

Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/printf_core/string_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
Expand All @@ -19,7 +20,7 @@
namespace __llvm_libc {
namespace printf_core {

int inline convert_string(Writer *writer, const FormatSection &to_conv) {
LIBC_INLINE int convert_string(Writer *writer, const FormatSection &to_conv) {
size_t string_len = 0;

for (char *cur_str = reinterpret_cast<char *>(to_conv.conv_val_ptr);
Expand Down
9 changes: 5 additions & 4 deletions libc/src/stdio/scanf_core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_PARSER_H

#include "src/__support/arg_list.h"
#include "src/__support/common.h"
#include "src/stdio/scanf_core/core_structs.h"
#include "src/stdio/scanf_core/scanf_config.h"

Expand All @@ -34,10 +35,10 @@ class Parser {

public:
#ifndef LLVM_LIBC_SCANF_DISABLE_INDEX_MODE
Parser(const char *__restrict new_str, internal::ArgList &args)
LIBC_INLINE Parser(const char *__restrict new_str, internal::ArgList &args)
: str(new_str), args_cur(args), args_start(args) {}
#else
Parser(const char *__restrict new_str, internal::ArgList &args)
LIBC_INLINE Parser(const char *__restrict new_str, internal::ArgList &args)
: str(new_str), args_cur(args) {}
#endif // LLVM_LIBC_SCANF_DISABLE_INDEX_MODE

Expand All @@ -55,7 +56,7 @@ class Parser {
LengthModifier parse_length_modifier(size_t *local_pos);

// get_next_arg_value gets the next value from the arg list as type T.
template <class T> T inline get_next_arg_value() {
template <class T> LIBC_INLINE T get_next_arg_value() {
return args_cur.next_var<T>();
}

Expand All @@ -75,7 +76,7 @@ class Parser {
// get_arg_value gets the value from the arg list at index (starting at 1).
// This may require parsing the format string. An index of 0 is interpreted as
// the next value.
template <class T> T inline get_arg_value(size_t index) {
template <class T> LIBC_INLINE T get_arg_value(size_t index) {
if (!(index == 0 || index == args_index))
args_to_index(index);

Expand Down