Skip to content

Commit

Permalink
Merge pull request #311 from ckormanyos/implement_to_string
Browse files Browse the repository at this point in the history
Fixes #153 with implementation of to_string and to_chars
  • Loading branch information
ckormanyos authored Aug 27, 2022
2 parents 9c874c5 + ed1e954 commit 385374f
Show file tree
Hide file tree
Showing 5 changed files with 553 additions and 94 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/wide_integer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ jobs:
- name: build-example-stm32f429
run: |
mkdir -p bin
emu_env/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-g++ -std=${{ matrix.standard }} -Wall -Wextra -pedantic -O0 -g -gdwarf-2 -ffunction-sections -fdata-sections -x c++ -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-nonansi-builtins -fno-threadsafe-statics -fno-enforce-eh-specs -ftemplate-depth=32 -mcpu=cortex-m4 -mtune=cortex-m4 -mthumb -mfloat-abi=soft -mno-unaligned-access -mno-long-calls -I. -DWIDE_INTEGER_DISABLE_IOSTREAM -DWIDE_INTEGER_DISABLE_TRIVIAL_COPY_AND_STD_LAYOUT_CHECKS -DWIDE_INTEGER_NAMESPACE=ckormanyos -DWIDE_INTEGER_STANDALONE_${{ steps.upcase_my_example.outputs.uppercase }} examples/${{ matrix.example }}.cpp ./target/micros/stm32f429/make/single/crt.cpp -nostartfiles -Wl,--gc-sections -Wl,-Map,./bin/${{ matrix.example }}.map -T ./target/micros/stm32f429/make/stm32f429.ld --specs=nano.specs --specs=nosys.specs -o ./bin/${{ matrix.example }}.elf
emu_env/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-g++ -std=${{ matrix.standard }} -Wall -Wextra -pedantic -O0 -g -gdwarf-2 -ffunction-sections -fdata-sections -x c++ -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-nonansi-builtins -fno-threadsafe-statics -fno-enforce-eh-specs -ftemplate-depth=32 -mcpu=cortex-m4 -mtune=cortex-m4 -mthumb -mfloat-abi=soft -mno-unaligned-access -mno-long-calls -I. -DWIDE_INTEGER_DISABLE_IOSTREAM -DWIDE_INTEGER_DISABLE_TO_STRING -DWIDE_INTEGER_DISABLE_TRIVIAL_COPY_AND_STD_LAYOUT_CHECKS -DWIDE_INTEGER_NAMESPACE=ckormanyos -DWIDE_INTEGER_STANDALONE_${{ steps.upcase_my_example.outputs.uppercase }} examples/${{ matrix.example }}.cpp ./target/micros/stm32f429/make/single/crt.cpp -nostartfiles -Wl,--gc-sections -Wl,-Map,./bin/${{ matrix.example }}.map -T ./target/micros/stm32f429/make/stm32f429.ld --specs=nano.specs --specs=nosys.specs -o ./bin/${{ matrix.example }}.elf
emu_env/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-objcopy ./bin/${{ matrix.example }}.elf -O ihex ./bin/${{ matrix.example }}.hex
ls -la ./bin/${{ matrix.example }}.elf ./bin/${{ matrix.example }}.hex ./bin/${{ matrix.example }}.map
working-directory: ./
Expand Down Expand Up @@ -658,8 +658,8 @@ jobs:
mkdir bin
echo ${{ steps.upcase_my_example.outputs.uppercase }}
echo 'compile examples/${{ matrix.example }}.cpp with:'
echo 'avr-g++ -x c++ -std=${{ matrix.standard }} -Os -Werror -Wall -Wextra -pedantic -Wmain -Wundef -Wconversion -Wsign-conversion -Wunused-parameter -Wuninitialized -Wmissing-declarations -Wshadow -Wunreachable-code -Wswitch-default -Wswitch-enum -Wcast-align -Wmissing-include-dirs -Winit-self -Wfloat-equal -Wdouble-promotion -mmcu=atmega328p -mrelax -finline-functions -finline-limit=32 -fsigned-char -g -gdwarf-2 -fno-exceptions -ffunction-sections -fdata-sections -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-threadsafe-statics -fno-enforce-eh-specs -ftemplate-depth=32 -Wzero-as-null-pointer-constant -I. -I../real-time-cpp-root/ref_app/src -I../real-time-cpp-root/ref_app/src/util/STL -DWIDE_INTEGER_DISABLE_IOSTREAM -DWIDE_INTEGER_DISABLE_TRIVIAL_COPY_AND_STD_LAYOUT_CHECKS -DWIDE_INTEGER_DISABLE_IMPLEMENT_UTIL_DYNAMIC_ARRAY -DWIDE_INTEGER_NAMESPACE=ckormanyos -DWIDE_INTEGER_STANDALONE_${{ steps.upcase_my_example.outputs.uppercase }} examples/${{ matrix.example }}.cpp -Wl,--gc-sections -Wl,-Map,./bin/${{ matrix.example }}.map -o bin/${{ matrix.example }}.elf'
avr-g++ -x c++ -std=${{ matrix.standard }} -Os -Werror -Wall -Wextra -pedantic -Wmain -Wundef -Wconversion -Wsign-conversion -Wunused-parameter -Wuninitialized -Wmissing-declarations -Wshadow -Wunreachable-code -Wswitch-default -Wswitch-enum -Wcast-align -Wmissing-include-dirs -Winit-self -Wfloat-equal -Wdouble-promotion -mmcu=atmega328p -mrelax -finline-functions -finline-limit=32 -fsigned-char -g -gdwarf-2 -fno-exceptions -ffunction-sections -fdata-sections -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-threadsafe-statics -fno-enforce-eh-specs -ftemplate-depth=32 -Wzero-as-null-pointer-constant -I. -I../real-time-cpp-root/ref_app/src -I../real-time-cpp-root/ref_app/src/util/STL -DWIDE_INTEGER_DISABLE_IOSTREAM -DWIDE_INTEGER_DISABLE_TRIVIAL_COPY_AND_STD_LAYOUT_CHECKS -DWIDE_INTEGER_DISABLE_IMPLEMENT_UTIL_DYNAMIC_ARRAY -DWIDE_INTEGER_NAMESPACE=ckormanyos -DWIDE_INTEGER_STANDALONE_${{ steps.upcase_my_example.outputs.uppercase }} examples/${{ matrix.example }}.cpp -Wl,--gc-sections -Wl,-Map,./bin/${{ matrix.example }}.map -o bin/${{ matrix.example }}.elf
echo 'avr-g++ -x c++ -std=${{ matrix.standard }} -Os -Werror -Wall -Wextra -pedantic -Wmain -Wundef -Wconversion -Wsign-conversion -Wunused-parameter -Wuninitialized -Wmissing-declarations -Wshadow -Wunreachable-code -Wswitch-default -Wswitch-enum -Wcast-align -Wmissing-include-dirs -Winit-self -Wfloat-equal -Wdouble-promotion -mmcu=atmega328p -mrelax -finline-functions -finline-limit=32 -fsigned-char -g -gdwarf-2 -fno-exceptions -ffunction-sections -fdata-sections -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-threadsafe-statics -fno-enforce-eh-specs -ftemplate-depth=32 -Wzero-as-null-pointer-constant -I. -I../real-time-cpp-root/ref_app/src -I../real-time-cpp-root/ref_app/src/util/STL -DWIDE_INTEGER_DISABLE_IOSTREAM -DWIDE_INTEGER_DISABLE_TO_STRING -DWIDE_INTEGER_DISABLE_TRIVIAL_COPY_AND_STD_LAYOUT_CHECKS -DWIDE_INTEGER_DISABLE_IMPLEMENT_UTIL_DYNAMIC_ARRAY -DWIDE_INTEGER_NAMESPACE=ckormanyos -DWIDE_INTEGER_STANDALONE_${{ steps.upcase_my_example.outputs.uppercase }} examples/${{ matrix.example }}.cpp -Wl,--gc-sections -Wl,-Map,./bin/${{ matrix.example }}.map -o bin/${{ matrix.example }}.elf'
avr-g++ -x c++ -std=${{ matrix.standard }} -Os -Werror -Wall -Wextra -pedantic -Wmain -Wundef -Wconversion -Wsign-conversion -Wunused-parameter -Wuninitialized -Wmissing-declarations -Wshadow -Wunreachable-code -Wswitch-default -Wswitch-enum -Wcast-align -Wmissing-include-dirs -Winit-self -Wfloat-equal -Wdouble-promotion -mmcu=atmega328p -mrelax -finline-functions -finline-limit=32 -fsigned-char -g -gdwarf-2 -fno-exceptions -ffunction-sections -fdata-sections -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-threadsafe-statics -fno-enforce-eh-specs -ftemplate-depth=32 -Wzero-as-null-pointer-constant -I. -I../real-time-cpp-root/ref_app/src -I../real-time-cpp-root/ref_app/src/util/STL -DWIDE_INTEGER_DISABLE_IOSTREAM -DWIDE_INTEGER_DISABLE_TO_STRING -DWIDE_INTEGER_DISABLE_TRIVIAL_COPY_AND_STD_LAYOUT_CHECKS -DWIDE_INTEGER_DISABLE_IMPLEMENT_UTIL_DYNAMIC_ARRAY -DWIDE_INTEGER_NAMESPACE=ckormanyos -DWIDE_INTEGER_STANDALONE_${{ steps.upcase_my_example.outputs.uppercase }} examples/${{ matrix.example }}.cpp -Wl,--gc-sections -Wl,-Map,./bin/${{ matrix.example }}.map -o bin/${{ matrix.example }}.elf
echo
echo 'run objcopy with:'
echo 'avr-objcopy bin/${{ matrix.example }}.elf -O ihex bin/${{ matrix.example }}.hex'
Expand Down
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
Wide-integer
==================

>ANNOUNCEMENT: Support for C++11 will be deprecated in this library starting in summer 2022.
>New features will require _at_ _least_ C++14, as will existing features starting with the deprecation.
<p align="center">
<a href="https://github.com/ckormanyos/wide-integer/actions">
<img src="https://github.com/ckormanyos/wide-integer/actions/workflows/wide_integer.yml/badge.svg" alt="Build Status"></a>
Expand Down Expand Up @@ -35,15 +32,16 @@ unsigned and signed integral types.
This C++ template header-only library implements drop-in big integer types
such as `uint128_t`, `uint256_t`, `uint384_t`, `uint512_t`, `uint1024_t`, `uint1536_t`, etc.
These can be used essentially like regular built-in integers.
Corresponding _signed_ integer types such as `int128_t`, `int256_t`, etc. can also be used.
Corresponding _signed_ integer types such as `int128_t`, `int256_t`, and the like
can also be used.

The big integer class is called `math::wide_integer::uintwide_t`
(i.e., `uintwide_t` residing in the `namespace` `math::wide_integer`),
as shown in greater detail below.

Wide-integer supports both unsigned as well as
signed integral types having width of $1{\ldots}63{\times}2^{N}$
while being $16$, $24$, $32$ or larger.
signed integral types having width of $1{\ldots}63{\times}2^N$
while being 16, 24, 32 or larger.
In addition, small integer types such as software-synthesized versions of
`uint24_t`, `uint48_t`, `uint64_t`, `uint96_t`, `uint128_t`, etc.
(or signed counterparts of these) can also be created with wide-integer.
Expand Down Expand Up @@ -321,11 +319,10 @@ readme page.
Wide-Integer has been tested with numerous compilers, for target systems ranging from 8 to 64 bits.
The library is specifically designed for efficiency with small to medium bit counts.
Supported bit counts include integers
$1{\ldots}63{\times}2^{N}$
while being $16$, $24$, $32$ or larger such as
$256$, $384$, $512$, $768$, $1024$,
or other less common bit counts such as $11,264$, etc.
Supported bit counts include integers $1{\ldots}63{\times}2^N$
while being 16, 24, 32 or larger such as
256, 384, 512, 768, 1024,
or other less common bit counts such as 11,264, etc.
Small, medium and large bit counts are supported.
Common applications might use the range of `uint128_t`, `uint256_t` or `uint512_t`.
Expand All @@ -347,6 +344,7 @@ enabled or disabled at compile time with the compiler switches:
```cpp
#define WIDE_INTEGER_DISABLE_IOSTREAM
#define WIDE_INTEGER_DISABLE_TO_STRING
#define WIDE_INTEGER_DISABLE_FLOAT_INTEROP
#define WIDE_INTEGER_DISABLE_IMPLEMENT_UTIL_DYNAMIC_ARRAY
#define WIDE_INTEGER_HAS_LIMB_TYPE_UINT64
Expand All @@ -365,6 +363,17 @@ I/O streaming can optionally be disabled with the compiler switch:
The default setting is `WIDE_INTEGER_DISABLE_IOSTREAM` not set
and I/O streaming operations are enabled.
Conversion to `std::string` is supported with the namespace-specific function
`to_string`. This analagous to the standard library's `std::to_string` function,
but implemented specifically for instances of `uintwide_t`.
Wide-integer's local, namespace-specific `to_string`
function (and the inclusion of the necessary `<string>` header)
are both deactivated with:
```cpp
#define WIDE_INTEGER_DISABLE_TO_STRING
```
Interoperability with built-in floating-point types
such as construct-from, cast-to, binary arithmetic with
built-in floating-point types can be
Expand Down Expand Up @@ -535,8 +544,8 @@ auto main() -> int
```

Wide-integer also supports a small selection of number-theoretical
functions such as least and most significant bit, square root,
$k^{th}$ root,
functions such as least and most significant bit,
square root, $k^{th}$ root,
power, power-modulus, greatest common denominator
and random number generation. These functions are be found via ADL.

Expand Down Expand Up @@ -605,7 +614,7 @@ auto main() -> int
The next example computes the real-valued cube root of $10^{3,333}$.
The real-valued cube root of this very large unsigned integer is $10^{1,111}$.
We will use the (somewhat uncommon) integral data type `uint11264_t`.
Since `uint11264_t` has approximately $3,390$ decimal digits of precision,
Since `uint11264_t` has approximately 3,390 decimal digits of precision,
it is large enough to hold the value of $10^{3,333}$
prior to (and following) the cube root operation.

Expand Down Expand Up @@ -739,7 +748,7 @@ negative arguments in number theoretical functions.
- `cbrt` of `x` nexative integer returns `-cbrt(-x)`.
- $k^{th}$ root of `x` negative returns zero unless the cube root is being computed, in which case `-cbrt(-x)` is returned.
- GCD of `a`, `b` signed converts both arguments to positive and negates the result for `a`, `b` having opposite signs.
- Miller-Rabin primality testing treats negative inetegers as positive when testing for prime, thus extending the set of primes $p{\in}{\mathbb{Z}}$.
- Miller-Rabin primality testing treats negative inetegers as positive when testing for prime, thus extending the set of primes to negative integers.
- MSB/LSB (most/least significant bit) do not differentiate between positive or negative argument such that MSB of a negative integer will be the highest bit of the corresponding unsigned type.
- Printing both positive-valued and negative-valued signed integers in hexadecimal format is supported. When printing negative-valued, signed `uintwide_t` in hexadecimal format, the sign bit and all other bits are treated as if the integer were unsigned. The negative sign is not explicitly shown when using hexadecimal format, even if the underlying integer is signed and negative-valued. A potential positive sign, however, will be shown for positive-valued signed integers in hexadecimal form in the presence of `std::showpos`.
Expand Down
14 changes: 6 additions & 8 deletions boost/multiprecision/uintwide_t_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,14 @@
static_cast<typename std::vector<char>::size_type>(representation_type::wr_string_max_buffer_size_dec)
);

const std::uint_fast8_t base_rep = (((format_flags & std::ios::hex) != 0) ? 16U : 10U);
const bool show_base = ( (format_flags & std::ios::showbase) != 0);
const bool show_pos = ( (format_flags & std::ios::showpos) != 0);
const bool is_uppercase = ( (format_flags & std::ios::uppercase) != 0);
const auto base_rep = static_cast<std::uint_fast8_t>(((format_flags & std::ios::hex) != 0) ? 16U : 10U);
const auto show_base = ((format_flags & std::ios::showbase) != 0);
const auto show_pos = ((format_flags & std::ios::showpos) != 0);
const auto is_uppercase = ((format_flags & std::ios::uppercase) != 0);

const bool wr_string_is_ok = m_value.wr_string(pstr.data(), base_rep, show_base, show_pos, is_uppercase);
const auto wr_string_is_ok = m_value.wr_string(pstr.data(), base_rep, show_base, show_pos, is_uppercase);

std::string str_result = (wr_string_is_ok ? std::string(pstr.data()) : std::string());

return str_result;
return (wr_string_is_ok ? std::string(pstr.data()) : std::string());
}

WIDE_INTEGER_CONSTEXPR auto negate() -> void
Expand Down
Loading

0 comments on commit 385374f

Please sign in to comment.