IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
kPrint.hpp
Go to the documentation of this file.
1
11
12
13#pragma once
14
15
16// C++
17#include <bit>
18#include <concepts>
19#include <cstdarg>
20#include <limits>
21#include <span>
22#include <string_view>
23#include <type_traits>
24// IgrOS-Kernel arch
25#include <arch/types.hpp>
26// IgrOS-Kernel library
27#include <klib/kMath.hpp>
28
29
30// Kernel library code zone
31namespace igros::klib {
32
33
35 enum class [[nodiscard("Radix value is meaningless if discarded")]] radix_t : igros_byte_t {
36 BIN = 0x02_u8,
37 OCT = 0x08_u8,
38 DEC = 0x0A_u8,
39 HEX = 0x10_u8,
40 };
41
42
52 template<std::unsigned_integral U>
53 [[nodiscard]]
54 constexpr auto kdigits(const std::span<char> buffer, U value, const radix_t radix, const bool upper = false) noexcept -> std::string_view {
55
56 // Digit symbols
57 constexpr auto LOWER {std::string_view {"0123456789abcdef"}};
58 constexpr auto UPPER {std::string_view {"0123456789ABCDEF"}};
59 const auto digits {upper ? UPPER : LOWER};
60
61 // Digits are written backwards from buffer end
62 auto pos {buffer.end()};
63
64 // Power of two radix: shifts and masks (no division)
65 if (radix_t::DEC != radix) {
66 const auto shift {static_cast<igros_dword_t>(std::countr_zero(static_cast<igros_dword_t>(radix)))};
67 const auto mask {static_cast<U>(static_cast<U>(radix) - 1U)};
68 do {
69 *--pos = digits[static_cast<igros_usize_t>(value & mask)];
70 value = static_cast<U>(value >> shift);
71 } while (0U != value);
72 return std::string_view {pos, buffer.end()};
73 }
74
75 // Values wider than CPU word (64-bit on i386) use slow software division:
76 // split into 9-digit chunks so it runs per chunk, not per digit
77 if constexpr (sizeof(U) > sizeof(igros_usize_t)) {
78 constexpr auto CHUNK {static_cast<U>(1000000000U)};
79 constexpr auto CHUNK_DIGITS {9};
80 while (value > std::numeric_limits<igros_usize_t>::max()) {
81 const auto res {kdivmod(value, CHUNK)};
82 auto chunk {static_cast<igros_usize_t>(res.remainder)};
83 for (auto i {0}; i < CHUNK_DIGITS; ++i) {
84 *--pos = digits[chunk % 10U];
85 chunk /= 10U;
86 }
87 value = res.quotient;
88 }
89 }
90
91 // Rest fits CPU word: native division
92 using N = std::conditional_t<(sizeof(U) > sizeof(igros_usize_t)), igros_usize_t, U>;
93 auto rest {static_cast<N>(value)};
94 do {
95 *--pos = digits[static_cast<igros_usize_t>(rest % 10U)];
96 rest = static_cast<N>(rest / 10U);
97 } while (0U != rest);
98 return std::string_view {pos, buffer.end()};
99 }
100
101
108 [[gnu::format(printf, 3, 0)]]
109 auto kvsnprintf(char* const buffer, const igros_usize_t size, const char* const format, std::va_list list) noexcept -> igros_usize_t;
110
117 [[gnu::format(printf, 3, 4)]]
118 auto ksnprintf(char* const buffer, const igros_usize_t size, const char* const format, ...) noexcept -> igros_usize_t;
119
123 [[gnu::format(printf, 1, 2)]]
124 void kprintf(const char* const format, ...) noexcept;
125
126
127} // namespace igros::klib
128
std::size_t igros_usize_t
Unsigned size.
Definition types.hpp:51
std::uint8_t igros_byte_t
8-bit unsigned integer
Definition types.hpp:41
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
Kernel math functions definitions.
Freestanding kernel library: printing, math, alignment, flags, strings.
auto ksnprintf(char *const buffer, const igros_usize_t size, const char *const format,...) noexcept -> igros_usize_t
Kernel snprintf function (returns written length without terminator).
Definition kPrint.cpp:344
void kprintf(const char *const format,...) noexcept
Kernel printf function (arguments are checked against format at compile time).
Definition kPrint.cpp:358
auto kvsnprintf(char *const buffer, const igros_usize_t size, const char *const format, std::va_list list) noexcept -> igros_usize_t
Kernel vsnprintf function (returns written length without terminator).
Definition kPrint.cpp:255
radix_t
Integer representation types.
Definition kPrint.hpp:35
@ BIN
(Base 2) Binary integer format
Definition kPrint.hpp:36
@ DEC
(Base 10) Decimal integer format
Definition kPrint.hpp:38
@ OCT
(Base 8) Oct integer format
Definition kPrint.hpp:37
@ HEX
(Base 16) Hexidemical integer format
Definition kPrint.hpp:39
constexpr auto kdivmod(const T dividend, const D divisor) noexcept -> divmod_t< T >
Divide integer by integer of the same signedness.
Definition kMath.hpp:115
constexpr auto kdigits(const std::span< char > buffer, U value, const radix_t radix, const bool upper=false) noexcept -> std::string_view
Digits of unsigned value written to the end of buffer (no sign, no terminator).
Definition kPrint.hpp:54
Common C++ types redefinition.