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
cd90097c
Commit
cd90097c
authored
Apr 21, 2018
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement handmade FP
parent
822eccc3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
5 deletions
+58
-5
include/fmt/format-inl.h
include/fmt/format-inl.h
+18
-5
include/fmt/format.h
include/fmt/format.h
+24
-0
test/util-test.cc
test/util-test.cc
+16
-0
No files found.
include/fmt/format-inl.h
View file @
cd90097c
...
@@ -219,8 +219,9 @@ FMT_FUNC void system_error::init(
...
@@ -219,8 +219,9 @@ FMT_FUNC void system_error::init(
base
=
std
::
runtime_error
(
to_string
(
buffer
));
base
=
std
::
runtime_error
(
to_string
(
buffer
));
}
}
namespace
internal
{
template
<
typename
T
>
template
<
typename
T
>
int
internal
::
char_traits
<
char
>::
format_float
(
int
char_traits
<
char
>::
format_float
(
char
*
buffer
,
std
::
size_t
size
,
const
char
*
format
,
char
*
buffer
,
std
::
size_t
size
,
const
char
*
format
,
unsigned
width
,
int
precision
,
T
value
)
{
unsigned
width
,
int
precision
,
T
value
)
{
if
(
width
==
0
)
{
if
(
width
==
0
)
{
...
@@ -234,7 +235,7 @@ int internal::char_traits<char>::format_float(
...
@@ -234,7 +235,7 @@ int internal::char_traits<char>::format_float(
}
}
template
<
typename
T
>
template
<
typename
T
>
int
internal
::
char_traits
<
wchar_t
>::
format_float
(
int
char_traits
<
wchar_t
>::
format_float
(
wchar_t
*
buffer
,
std
::
size_t
size
,
const
wchar_t
*
format
,
wchar_t
*
buffer
,
std
::
size_t
size
,
const
wchar_t
*
format
,
unsigned
width
,
int
precision
,
T
value
)
{
unsigned
width
,
int
precision
,
T
value
)
{
if
(
width
==
0
)
{
if
(
width
==
0
)
{
...
@@ -248,7 +249,7 @@ int internal::char_traits<wchar_t>::format_float(
...
@@ -248,7 +249,7 @@ int internal::char_traits<wchar_t>::format_float(
}
}
template
<
typename
T
>
template
<
typename
T
>
const
char
internal
::
basic_data
<
T
>::
DIGITS
[]
=
const
char
basic_data
<
T
>::
DIGITS
[]
=
"0001020304050607080910111213141516171819"
"0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839"
"2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859"
"4041424344454647484950515253545556575859"
...
@@ -267,18 +268,30 @@ const char internal::basic_data<T>::DIGITS[] =
...
@@ -267,18 +268,30 @@ const char internal::basic_data<T>::DIGITS[] =
factor * 1000000000
factor * 1000000000
template
<
typename
T
>
template
<
typename
T
>
const
uint32_t
internal
::
basic_data
<
T
>::
POWERS_OF_10_32
[]
=
{
const
uint32_t
basic_data
<
T
>::
POWERS_OF_10_32
[]
=
{
0
,
FMT_POWERS_OF_10
(
1
)
0
,
FMT_POWERS_OF_10
(
1
)
};
};
template
<
typename
T
>
template
<
typename
T
>
const
uint64_t
internal
::
basic_data
<
T
>::
POWERS_OF_10_64
[]
=
{
const
uint64_t
basic_data
<
T
>::
POWERS_OF_10_64
[]
=
{
0
,
0
,
FMT_POWERS_OF_10
(
1
),
FMT_POWERS_OF_10
(
1
),
FMT_POWERS_OF_10
(
1000000000ull
),
FMT_POWERS_OF_10
(
1000000000ull
),
10000000000000000000ull
10000000000000000000ull
};
};
FMT_FUNC
fp
operator
*
(
fp
x
,
fp
y
)
{
// Multiply 32-bit parts of significands.
uint64_t
mask
=
(
1ULL
<<
32
)
-
1
;
uint64_t
a
=
x
.
f
>>
32
,
b
=
x
.
f
&
mask
;
uint64_t
c
=
y
.
f
>>
32
,
d
=
y
.
f
&
mask
;
uint64_t
ac
=
a
*
c
,
bc
=
b
*
c
,
ad
=
a
*
d
,
bd
=
b
*
d
;
// Compute mid 64-bit of result and round.
uint64_t
mid
=
(
bd
>>
32
)
+
(
ad
&
mask
)
+
(
bc
&
mask
)
+
(
1U
<<
31
);
return
fp
(
ac
+
(
ad
>>
32
)
+
(
bc
>>
32
)
+
(
mid
>>
32
),
x
.
e
+
y
.
e
+
64
);
}
}
// namespace internal
#if FMT_USE_WINDOWS_H
#if FMT_USE_WINDOWS_H
FMT_FUNC
internal
::
utf8_to_utf16
::
utf8_to_utf16
(
string_view
s
)
{
FMT_FUNC
internal
::
utf8_to_utf16
::
utf8_to_utf16
(
string_view
s
)
{
...
...
include/fmt/format.h
View file @
cd90097c
...
@@ -245,6 +245,30 @@ inline dummy_int _finite(...) { return dummy_int(); }
...
@@ -245,6 +245,30 @@ inline dummy_int _finite(...) { return dummy_int(); }
inline
dummy_int
isnan
(...)
{
return
dummy_int
();
}
inline
dummy_int
isnan
(...)
{
return
dummy_int
();
}
inline
dummy_int
_isnan
(...)
{
return
dummy_int
();
}
inline
dummy_int
_isnan
(...)
{
return
dummy_int
();
}
// A handmade floating-point number f * pow(2, e).
struct
fp
{
uint64_t
f
;
int
e
;
fp
(
uint64_t
f
,
int
e
)
:
f
(
f
),
e
(
e
)
{}
};
// Returns an fp number representing x - y. Result may not be normalized.
inline
fp
operator
-
(
fp
x
,
fp
y
)
{
FMT_ASSERT
(
x
.
f
>=
y
.
f
&&
x
.
e
==
y
.
e
,
"invalid operands"
);
return
fp
(
x
.
f
-
y
.
f
,
x
.
e
);
}
// Computes an fp number r with r.f = x.f * y.f / pow(2, 32) rounded to nearest
// with half-up tie breaking, r.e = x.e + y.e + 32. Result may not be normalized.
fp
operator
*
(
fp
x
,
fp
y
);
// Compute k such that its cached power c_k = c_k.f * pow(2, c_k.e) satisfies
// alpha <= c_k.e + e <= alpha + 3.
inline
int
compute_cached_power_index
(
int
e
,
int
alpha
)
{
constexpr
double
one_over_log2_10
=
0.30102999566398114
;
// 1 / log2(10)
return
std
::
ceil
((
alpha
-
e
+
63
)
*
one_over_log2_10
);
}
template
<
typename
Allocator
>
template
<
typename
Allocator
>
typename
Allocator
::
value_type
*
allocate
(
Allocator
&
alloc
,
std
::
size_t
n
)
{
typename
Allocator
::
value_type
*
allocate
(
Allocator
&
alloc
,
std
::
size_t
n
)
{
#if __cplusplus >= 201103L || FMT_MSC_VER >= 1700
#if __cplusplus >= 201103L || FMT_MSC_VER >= 1700
...
...
test/util-test.cc
View file @
cd90097c
...
@@ -37,6 +37,7 @@ using fmt::basic_format_arg;
...
@@ -37,6 +37,7 @@ using fmt::basic_format_arg;
using
fmt
::
internal
::
basic_buffer
;
using
fmt
::
internal
::
basic_buffer
;
using
fmt
::
basic_memory_buffer
;
using
fmt
::
basic_memory_buffer
;
using
fmt
::
string_view
;
using
fmt
::
string_view
;
using
fmt
::
internal
::
fp
;
using
fmt
::
internal
::
value
;
using
fmt
::
internal
::
value
;
using
testing
::
_
;
using
testing
::
_
;
...
@@ -869,3 +870,18 @@ TEST(UtilTest, ParseNonnegativeInt) {
...
@@ -869,3 +870,18 @@ TEST(UtilTest, ParseNonnegativeInt) {
parse_nonnegative_int
(
s
,
fmt
::
internal
::
error_handler
()),
parse_nonnegative_int
(
s
,
fmt
::
internal
::
error_handler
()),
fmt
::
format_error
,
"number is too big"
);
fmt
::
format_error
,
"number is too big"
);
}
}
TEST
(
UtilTest
,
FPSubtract
)
{
auto
r
=
fp
(
123
,
1
)
-
fp
(
102
,
1
);
EXPECT_EQ
(
r
.
f
,
21u
);
EXPECT_EQ
(
r
.
e
,
1
);
}
TEST
(
UtilTest
,
FPMultiply
)
{
auto
r
=
fp
(
123ULL
<<
32
,
4
)
*
fp
(
56ULL
<<
32
,
7
);
EXPECT_EQ
(
r
.
f
,
123u
*
56u
);
EXPECT_EQ
(
r
.
e
,
4
+
7
+
64
);
r
=
fp
(
123ULL
<<
32
,
4
)
*
fp
(
567ULL
<<
31
,
8
);
EXPECT_EQ
(
r
.
f
,
(
123
*
567
+
1u
)
/
2
);
EXPECT_EQ
(
r
.
e
,
4
+
8
+
64
);
}
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