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
10642e60
Commit
10642e60
authored
Feb 08, 2022
by
Junekey Jeon
Committed by
Victor Zverovich
Feb 13, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize remove_trailing_zeros
parent
7b4323e1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
91 deletions
+62
-91
include/fmt/format-inl.h
include/fmt/format-inl.h
+62
-91
No files found.
include/fmt/format-inl.h
View file @
10642e60
...
@@ -1835,115 +1835,86 @@ bool is_left_endpoint_integer_shorter_interval(int exponent) noexcept {
...
@@ -1835,115 +1835,86 @@ bool is_left_endpoint_integer_shorter_interval(int exponent) noexcept {
// Remove trailing zeros from n and return the number of zeros removed (float)
// Remove trailing zeros from n and return the number of zeros removed (float)
FMT_INLINE
int
remove_trailing_zeros
(
uint32_t
&
n
)
noexcept
{
FMT_INLINE
int
remove_trailing_zeros
(
uint32_t
&
n
)
noexcept
{
#ifdef FMT_BUILTIN_CTZ
FMT_ASSERT
(
n
!=
0
,
""
);
int
t
=
FMT_BUILTIN_CTZ
(
n
);
const
uint32_t
mod_inv_5
=
0xcccccccd
;
#else
const
uint32_t
mod_inv_25
=
mod_inv_5
*
mod_inv_5
;
int
t
=
ctz
(
n
);
#endif
if
(
t
>
float_info
<
float
>::
max_trailing_zeros
)
t
=
float_info
<
float
>::
max_trailing_zeros
;
const
uint32_t
mod_inv1
=
0xcccccccd
;
const
uint32_t
max_quotient1
=
0x33333333
;
const
uint32_t
mod_inv2
=
0xc28f5c29
;
const
uint32_t
max_quotient2
=
0x0a3d70a3
;
int
s
=
0
;
int
s
=
0
;
for
(;
s
<
t
-
1
;
s
+=
2
)
{
while
(
true
)
{
if
(
n
*
mod_inv2
>
max_quotient2
)
break
;
auto
q
=
rotr
(
n
*
mod_inv_25
,
2
);
n
*=
mod_inv2
;
if
(
q
<=
std
::
numeric_limits
<
uint32_t
>::
max
()
/
100
)
{
n
=
q
;
s
+=
2
;
}
else
{
break
;
}
}
}
if
(
s
<
t
&&
n
*
mod_inv1
<=
max_quotient1
)
{
auto
q
=
rotr
(
n
*
mod_inv_5
,
1
);
n
*=
mod_inv1
;
if
(
q
<=
std
::
numeric_limits
<
uint32_t
>::
max
()
/
10
)
{
++
s
;
n
=
q
;
s
|=
1
;
}
}
n
>>=
s
;
return
s
;
return
s
;
}
}
// Removes trailing zeros and returns the number of zeros removed (double)
// Removes trailing zeros and returns the number of zeros removed (double)
FMT_INLINE
int
remove_trailing_zeros
(
uint64_t
&
n
)
noexcept
{
FMT_INLINE
int
remove_trailing_zeros
(
uint64_t
&
n
)
noexcept
{
#ifdef FMT_BUILTIN_CTZLL
FMT_ASSERT
(
n
!=
0
,
""
);
int
t
=
FMT_BUILTIN_CTZLL
(
n
);
#else
// This magic number is ceil(2^90 / 10^8).
int
t
=
ctzll
(
n
);
constexpr
auto
magic_number
=
uint64_t
(
12379400392853802749ull
);
#endif
auto
nm
=
umul128
(
n
,
magic_number
);
if
(
t
>
float_info
<
double
>::
max_trailing_zeros
)
t
=
float_info
<
double
>::
max_trailing_zeros
;
// Is n is divisible by 10^8?
// Divide by 10^8 and reduce to 32-bits
if
((
nm
.
high
()
&
((
1ull
<<
(
90
-
64
))
-
1
))
==
0
&&
nm
.
low
()
<
magic_number
)
{
// Since ret_value.significand <= (2^64 - 1) / 1000 < 10^17,
// If yes, work with the quotient.
// both of the quotient and the r should fit in 32-bits
auto
n32
=
static_cast
<
uint32_t
>
(
nm
.
high
()
>>
(
90
-
64
));
const
uint32_t
mod_inv1
=
0xcccccccd
;
const
uint32_t
mod_inv_5
=
0xcccccccd
;
const
uint32_t
max_quotient1
=
0x33333333
;
const
uint32_t
mod_inv_25
=
mod_inv_5
*
mod_inv_5
;
const
uint64_t
mod_inv8
=
0xc767074b22e90e21
;
const
uint64_t
max_quotient8
=
0x00002af31dc46118
;
int
s
=
8
;
while
(
true
)
{
// If the number is divisible by 1'0000'0000, work with the quotient
auto
q
=
rotr
(
n32
*
mod_inv_25
,
2
);
if
(
t
>=
8
)
{
if
(
q
<=
std
::
numeric_limits
<
uint32_t
>::
max
()
/
100
)
{
auto
quotient_candidate
=
n
*
mod_inv8
;
n32
=
q
;
s
+=
2
;
if
(
quotient_candidate
<=
max_quotient8
)
{
}
else
{
auto
quotient
=
static_cast
<
uint32_t
>
(
quotient_candidate
>>
8
);
break
;
int
s
=
8
;
for
(;
s
<
t
;
++
s
)
{
if
(
quotient
*
mod_inv1
>
max_quotient1
)
break
;
quotient
*=
mod_inv1
;
}
}
quotient
>>=
(
s
-
8
);
n
=
quotient
;
return
s
;
}
}
}
auto
q
=
rotr
(
n32
*
mod_inv_5
,
1
);
if
(
q
<=
std
::
numeric_limits
<
uint32_t
>::
max
()
/
10
)
{
// Otherwise, work with the remainder
n32
=
q
;
auto
quotient
=
static_cast
<
uint32_t
>
(
n
/
100000000
);
s
|=
1
;
auto
remainder
=
static_cast
<
uint32_t
>
(
n
-
100000000
*
quotient
);
}
if
(
t
==
0
||
remainder
*
mod_inv1
>
max_quotient1
)
{
return
0
;
}
remainder
*=
mod_inv1
;
if
(
t
==
1
||
remainder
*
mod_inv1
>
max_quotient1
)
{
n
=
(
remainder
>>
1
)
+
quotient
*
10000000ull
;
return
1
;
}
remainder
*=
mod_inv1
;
if
(
t
==
2
||
remainder
*
mod_inv1
>
max_quotient1
)
{
n
=
(
remainder
>>
2
)
+
quotient
*
1000000ull
;
return
2
;
}
remainder
*=
mod_inv1
;
if
(
t
==
3
||
remainder
*
mod_inv1
>
max_quotient1
)
{
n
=
n32
;
n
=
(
remainder
>>
3
)
+
quotient
*
100000ull
;
return
s
;
return
3
;
}
}
remainder
*=
mod_inv1
;
if
(
t
==
4
||
remainder
*
mod_inv1
>
max_quotient1
)
{
// If n is not divisible by 10^8, work with n itself.
n
=
(
remainder
>>
4
)
+
quotient
*
10000ull
;
const
uint64_t
mod_inv_5
=
0xcccccccc'cccccccd
;
return
4
;
const
uint64_t
mod_inv_25
=
mod_inv_5
*
mod_inv_5
;
}
remainder
*=
mod_inv1
;
if
(
t
==
5
||
remainder
*
mod_inv1
>
max_quotient1
)
{
int
s
=
0
;
n
=
(
remainder
>>
5
)
+
quotient
*
1000ull
;
while
(
true
)
{
return
5
;
auto
q
=
rotr
(
n
*
mod_inv_25
,
2
);
if
(
q
<=
std
::
numeric_limits
<
uint64_t
>::
max
()
/
100
)
{
n
=
q
;
s
+=
2
;
}
else
{
break
;
}
}
}
remainder
*=
mod_inv1
;
auto
q
=
rotr
(
n
*
mod_inv_5
,
1
);
if
(
q
<=
std
::
numeric_limits
<
uint64_t
>::
max
()
/
10
)
{
if
(
t
==
6
||
remainder
*
mod_inv1
>
max_quotient1
)
{
n
=
q
;
n
=
(
remainder
>>
6
)
+
quotient
*
100ull
;
s
|=
1
;
return
6
;
}
}
remainder
*=
mod_inv1
;
n
=
(
remainder
>>
7
)
+
quotient
*
10ull
;
return
s
;
return
7
;
}
}
// The main algorithm for shorter interval case
// The main algorithm for shorter interval case
...
...
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