Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make double parsing locale independent (and faster) #1993

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions LICENSE
Expand Up @@ -251,3 +251,33 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-------------------------------------------------------------------------------

All files in symengine/utilities/fast_float were taken from the fastfloat library
and are licensed under the MIT License:

Copyright (c) 2021 The fast_float authors

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
44 changes: 44 additions & 0 deletions benchmarks/parsing_google.cpp
Expand Up @@ -11,9 +11,48 @@ static void parse_0(benchmark::State &state)
RCP<const Basic> a;
for (auto _ : state) {
a = parse("0");
benchmark::DoNotOptimize(a);
}
}

static void parse_double(benchmark::State &state)
{
std::string text = "-.123e-11";
RCP<const Basic> a;
for (auto _ : state) {
a = parse(text);
benchmark::DoNotOptimize(a);
}
}

static void parse_many_doubles(benchmark::State &state)
{
std::string text;
for (int i = 0; i < state.range(0); i++) {
text.append("+24.45645 - 0.9834e-2 - 23.243 + 0.058263e-11");
}
RCP<const Basic> a;
for (auto _ : state) {
a = parse(text);
benchmark::DoNotOptimize(a);
}
state.SetComplexityN(4 * state.range(0));
}

static void parse_many_doubles_implicit_mul(benchmark::State &state)
{
std::string text;
for (int i = 0; i < state.range(0); i++) {
text.append("+24.45645x - 0.9834e-2y - 23.243z + 0.058263e-11w");
}
RCP<const Basic> a;
for (auto _ : state) {
a = parse(text);
benchmark::DoNotOptimize(a);
}
state.SetComplexityN(4 * state.range(0));
}

static void parse_long_expr1(benchmark::State &state)
{
std::string text = "1";
Expand All @@ -24,6 +63,7 @@ static void parse_long_expr1(benchmark::State &state)
RCP<const Basic> a;
for (auto _ : state) {
a = parse(text);
benchmark::DoNotOptimize(a);
}
state.SetComplexityN(state.range(0));
}
Expand All @@ -38,11 +78,15 @@ static void parse_long_expr2(benchmark::State &state)
RCP<const Basic> a;
for (auto _ : state) {
a = parse(text);
benchmark::DoNotOptimize(a);
}
state.SetComplexityN(state.range(0));
}

BENCHMARK(parse_0);
BENCHMARK(parse_double);
BENCHMARK(parse_many_doubles)->Range(1, 4096)->Complexity();
BENCHMARK(parse_many_doubles_implicit_mul)->Range(1, 4096)->Complexity();
BENCHMARK(parse_long_expr1)->Range(2, 4096)->Complexity();
BENCHMARK(parse_long_expr2)->Range(2, 4096)->Complexity();

Expand Down
1 change: 1 addition & 0 deletions symengine/CMakeLists.txt
Expand Up @@ -248,6 +248,7 @@ set(HEADERS
refine.h
simplify.h
utilities/stream_fmt.h
utilities/fast_float/fast_float.h
tuple.h
matrix_expressions.h
matrices/matrix_expr.h
Expand Down
14 changes: 8 additions & 6 deletions symengine/parser/parser.cpp
Expand Up @@ -4,6 +4,7 @@
#include <symengine/real_mpfr.h>
#include <symengine/ntheory_funcs.h>
#include <symengine/parser/tokenizer.h>
#include <symengine/utilities/fast_float/fast_float.h>

namespace SymEngine
{
Expand Down Expand Up @@ -310,8 +311,8 @@ RCP<const Basic> Parser::parse_numeric(const std::string &expr)
}
}
if (digits <= 15) {
char *endptr = 0;
double d = std::strtod(startptr, &endptr);
double d;
fast_float::from_chars(startptr, startptr + expr.size(), d);
return real_double(d);
} else {
// mpmath.libmp.libmpf.dps_to_prec
Expand All @@ -320,8 +321,8 @@ RCP<const Basic> Parser::parse_numeric(const std::string &expr)
return real_mpfr(mpfr_class(expr, prec));
}
#else
char *endptr = 0;
double d = std::strtod(startptr, &endptr);
double d;
fast_float::from_chars(startptr, startptr + expr.size(), d);
return real_double(d);
#endif
}
Expand All @@ -331,8 +332,9 @@ std::tuple<RCP<const Basic>, RCP<const Basic>>
Parser::parse_implicit_mul(const std::string &expr)
{
const char *startptr = expr.c_str();
char *endptr = 0;
std::strtod(startptr, &endptr);
double result;
const char *endptr
= fast_float::from_chars(startptr, startptr + expr.size(), result).ptr;

RCP<const Basic> num = one, sym;

Expand Down
21 changes: 21 additions & 0 deletions symengine/tests/basic/test_parser.cpp
Expand Up @@ -807,6 +807,11 @@ TEST_CASE("Parsing: doubles", "[parser]")
REQUIRE(eq(*res, *real_double(1.324)));
REQUIRE(eq(*res, *parse(res->__str__())));

s = "+1.324";
res = parse(s);
REQUIRE(eq(*res, *real_double(1.324)));
REQUIRE(eq(*res, *parse(res->__str__())));

s = "0.0324*x + 2*3";
res = parse(s);
REQUIRE(eq(*res, *add(mul(real_double(0.0324), x), integer(6))));
Expand All @@ -817,6 +822,22 @@ TEST_CASE("Parsing: doubles", "[parser]")
REQUIRE(eq(*res, *add(mul(real_double(0.0324), x), integer(6))));
REQUIRE(eq(*res, *parse(res->__str__())));

s = " +0.324e-19x+4.238";
res = parse(s);
CAPTURE(res->__str__());
REQUIRE(eq(*res, *add(mul(real_double(0.324e-19), x), real_double(4.238))));
REQUIRE(eq(*res, *parse(res->__str__())));

s = ".32485123e-21";
res = parse(s);
REQUIRE(eq(*res, *real_double(0.32485123e-21)));
REQUIRE(eq(*res, *parse(res->__str__())));

s = "1345.35e13";
res = parse(s);
REQUIRE(eq(*res, *real_double(1345.35e13)));
REQUIRE(eq(*res, *parse(res->__str__())));

s = "1.324/(2+3)";
res = parse(s);
REQUIRE(is_a<RealDouble>(*res));
Expand Down