1 //===-- Shared Converter Utilities for printf -------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H
12 #include "src/__support/CPP/limits.h"
13 #include "src/__support/common.h"
14 #include "src/stdio/printf_core/core_structs.h"
19 namespace LIBC_NAMESPACE
{
20 namespace printf_core
{
22 LIBC_INLINE
uintmax_t apply_length_modifier(uintmax_t num
, LengthModifier lm
) {
24 case LengthModifier::none
:
25 return num
& cpp::numeric_limits
<unsigned int>::max();
26 case LengthModifier::l
:
27 return num
& cpp::numeric_limits
<unsigned long>::max();
28 case LengthModifier::ll
:
29 case LengthModifier::L
:
30 return num
& cpp::numeric_limits
<unsigned long long>::max();
31 case LengthModifier::h
:
32 return num
& cpp::numeric_limits
<unsigned short>::max();
33 case LengthModifier::hh
:
34 return num
& cpp::numeric_limits
<unsigned char>::max();
35 case LengthModifier::z
:
36 return num
& cpp::numeric_limits
<size_t>::max();
37 case LengthModifier::t
:
38 // We don't have unsigned ptrdiff so uintptr_t is used, since we need an
39 // unsigned type and ptrdiff is usually the same size as a pointer.
40 static_assert(sizeof(ptrdiff_t) == sizeof(uintptr_t));
41 return num
& cpp::numeric_limits
<uintptr_t>::max();
42 case LengthModifier::j
:
43 return num
; // j is intmax, so no mask is necessary.
45 __builtin_unreachable();
48 #define RET_IF_RESULT_NEGATIVE(func) \
50 int result = (func); \
55 } // namespace printf_core
56 } // namespace LIBC_NAMESPACE
58 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H