Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
b308159b
Commit
b308159b
authored
Dec 21, 2019
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make round_direction a scoped enum
parent
162995fe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
16 deletions
+18
-16
include/fmt/format-inl.h
include/fmt/format-inl.h
+8
-7
test/format-impl-test.cc
test/format-impl-test.cc
+10
-9
No files found.
include/fmt/format-inl.h
View file @
b308159b
...
@@ -722,7 +722,7 @@ class bigint {
...
@@ -722,7 +722,7 @@ class bigint {
}
}
};
};
enum
round_direction
{
unknown
,
up
,
down
};
enum
class
round_direction
{
unknown
,
up
,
down
};
// Given the divisor (normally a power of 10), the remainder = v % divisor for
// Given the divisor (normally a power of 10), the remainder = v % divisor for
// some number v and the error, returns whether v should be rounded up, down, or
// some number v and the error, returns whether v should be rounded up, down, or
...
@@ -735,13 +735,13 @@ inline round_direction get_round_direction(uint64_t divisor, uint64_t remainder,
...
@@ -735,13 +735,13 @@ inline round_direction get_round_direction(uint64_t divisor, uint64_t remainder,
FMT_ASSERT
(
error
<
divisor
-
error
,
""
);
// error * 2 won't overflow.
FMT_ASSERT
(
error
<
divisor
-
error
,
""
);
// error * 2 won't overflow.
// Round down if (remainder + error) * 2 <= divisor.
// Round down if (remainder + error) * 2 <= divisor.
if
(
remainder
<=
divisor
-
remainder
&&
error
*
2
<=
divisor
-
remainder
*
2
)
if
(
remainder
<=
divisor
-
remainder
&&
error
*
2
<=
divisor
-
remainder
*
2
)
return
down
;
return
round_direction
::
down
;
// Round up if (remainder - error) * 2 >= divisor.
// Round up if (remainder - error) * 2 >= divisor.
if
(
remainder
>=
error
&&
if
(
remainder
>=
error
&&
remainder
-
error
>=
divisor
-
(
remainder
-
error
))
{
remainder
-
error
>=
divisor
-
(
remainder
-
error
))
{
return
up
;
return
round_direction
::
up
;
}
}
return
unknown
;
return
round_direction
::
unknown
;
}
}
namespace
digits
{
namespace
digits
{
...
@@ -857,8 +857,8 @@ struct fixed_handler {
...
@@ -857,8 +857,8 @@ struct fixed_handler {
if
(
precision
>
0
)
return
digits
::
more
;
if
(
precision
>
0
)
return
digits
::
more
;
if
(
precision
<
0
)
return
digits
::
done
;
if
(
precision
<
0
)
return
digits
::
done
;
auto
dir
=
get_round_direction
(
divisor
,
remainder
,
error
);
auto
dir
=
get_round_direction
(
divisor
,
remainder
,
error
);
if
(
dir
==
unknown
)
return
digits
::
error
;
if
(
dir
==
round_direction
::
unknown
)
return
digits
::
error
;
buf
[
size
++
]
=
dir
==
up
?
'1'
:
'0'
;
buf
[
size
++
]
=
dir
==
round_direction
::
up
?
'1'
:
'0'
;
return
digits
::
done
;
return
digits
::
done
;
}
}
...
@@ -876,7 +876,8 @@ struct fixed_handler {
...
@@ -876,7 +876,8 @@ struct fixed_handler {
FMT_ASSERT
(
error
==
1
&&
divisor
>
2
,
""
);
FMT_ASSERT
(
error
==
1
&&
divisor
>
2
,
""
);
}
}
auto
dir
=
get_round_direction
(
divisor
,
remainder
,
error
);
auto
dir
=
get_round_direction
(
divisor
,
remainder
,
error
);
if
(
dir
!=
up
)
return
dir
==
down
?
digits
::
done
:
digits
::
error
;
if
(
dir
!=
round_direction
::
up
)
return
dir
==
round_direction
::
down
?
digits
::
done
:
digits
::
error
;
++
buf
[
size
-
1
];
++
buf
[
size
-
1
];
for
(
int
i
=
size
-
1
;
i
>
0
&&
buf
[
i
]
>
'9'
;
--
i
)
{
for
(
int
i
=
size
-
1
;
i
>
0
&&
buf
[
i
]
>
'9'
;
--
i
)
{
buf
[
i
]
=
'0'
;
buf
[
i
]
=
'0'
;
...
...
test/format-impl-test.cc
View file @
b308159b
...
@@ -272,26 +272,27 @@ TEST(FPTest, GetCachedPower) {
...
@@ -272,26 +272,27 @@ TEST(FPTest, GetCachedPower) {
}
}
TEST
(
FPTest
,
GetRoundDirection
)
{
TEST
(
FPTest
,
GetRoundDirection
)
{
using
fmt
::
internal
::
round_direction
;
using
fmt
::
internal
::
get_round_direction
;
using
fmt
::
internal
::
get_round_direction
;
EXPECT_EQ
(
fmt
::
internal
::
down
,
get_round_direction
(
100
,
50
,
0
));
EXPECT_EQ
(
round_direction
::
down
,
get_round_direction
(
100
,
50
,
0
));
EXPECT_EQ
(
fmt
::
internal
::
up
,
get_round_direction
(
100
,
51
,
0
));
EXPECT_EQ
(
round_direction
::
up
,
get_round_direction
(
100
,
51
,
0
));
EXPECT_EQ
(
fmt
::
internal
::
down
,
get_round_direction
(
100
,
40
,
10
));
EXPECT_EQ
(
round_direction
::
down
,
get_round_direction
(
100
,
40
,
10
));
EXPECT_EQ
(
fmt
::
internal
::
up
,
get_round_direction
(
100
,
60
,
10
));
EXPECT_EQ
(
round_direction
::
up
,
get_round_direction
(
100
,
60
,
10
));
for
(
size_t
i
=
41
;
i
<
60
;
++
i
)
for
(
size_t
i
=
41
;
i
<
60
;
++
i
)
EXPECT_EQ
(
fmt
::
internal
::
unknown
,
get_round_direction
(
100
,
i
,
10
));
EXPECT_EQ
(
round_direction
::
unknown
,
get_round_direction
(
100
,
i
,
10
));
uint64_t
max
=
max_value
<
uint64_t
>
();
uint64_t
max
=
max_value
<
uint64_t
>
();
EXPECT_THROW
(
get_round_direction
(
100
,
100
,
0
),
assertion_failure
);
EXPECT_THROW
(
get_round_direction
(
100
,
100
,
0
),
assertion_failure
);
EXPECT_THROW
(
get_round_direction
(
100
,
0
,
100
),
assertion_failure
);
EXPECT_THROW
(
get_round_direction
(
100
,
0
,
100
),
assertion_failure
);
EXPECT_THROW
(
get_round_direction
(
100
,
0
,
50
),
assertion_failure
);
EXPECT_THROW
(
get_round_direction
(
100
,
0
,
50
),
assertion_failure
);
// Check that remainder + error doesn't overflow.
// Check that remainder + error doesn't overflow.
EXPECT_EQ
(
fmt
::
internal
::
up
,
get_round_direction
(
max
,
max
-
1
,
2
));
EXPECT_EQ
(
round_direction
::
up
,
get_round_direction
(
max
,
max
-
1
,
2
));
// Check that 2 * (remainder + error) doesn't overflow.
// Check that 2 * (remainder + error) doesn't overflow.
EXPECT_EQ
(
fmt
::
internal
::
unknown
,
EXPECT_EQ
(
round_direction
::
unknown
,
get_round_direction
(
max
,
max
/
2
+
1
,
max
/
2
));
get_round_direction
(
max
,
max
/
2
+
1
,
max
/
2
));
// Check that remainder - error doesn't overflow.
// Check that remainder - error doesn't overflow.
EXPECT_EQ
(
fmt
::
internal
::
unknown
,
get_round_direction
(
100
,
40
,
41
));
EXPECT_EQ
(
round_direction
::
unknown
,
get_round_direction
(
100
,
40
,
41
));
// Check that 2 * (remainder - error) doesn't overflow.
// Check that 2 * (remainder - error) doesn't overflow.
EXPECT_EQ
(
fmt
::
internal
::
up
,
get_round_direction
(
max
,
max
-
1
,
1
));
EXPECT_EQ
(
round_direction
::
up
,
get_round_direction
(
max
,
max
-
1
,
1
));
}
}
TEST
(
FPTest
,
FixedHandler
)
{
TEST
(
FPTest
,
FixedHandler
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment