Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
tug
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
naaice
tug
Commits
d05ae677
Commit
d05ae677
authored
Nov 23, 2023
by
Max Lübke
Browse files
Options
Downloads
Patches
Plain Diff
Add simple fixed point type
parent
3dc9dc4a
No related branches found
No related tags found
No related merge requests found
Pipeline
#66782
passed
Nov 23, 2023
Stage: build
Stage: test
Stage: static_analyze
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/tug/Datatypes/FixedPoint.hpp
+98
-0
98 additions, 0 deletions
include/tug/Datatypes/FixedPoint.hpp
test/CMakeLists.txt
+3
-0
3 additions, 0 deletions
test/CMakeLists.txt
test/testFixedPoint.cpp
+35
-0
35 additions, 0 deletions
test/testFixedPoint.cpp
with
136 additions
and
0 deletions
include/tug/Datatypes/FixedPoint.hpp
0 → 100644
+
98
−
0
View file @
d05ae677
#ifndef TUG_FIXED_TYPE
#define TUG_FIXED_TYPE
#include
<cstdint>
#include
<ostream>
#include
<type_traits>
namespace
tug
{
template
<
typename
BaseType
,
typename
GreaterType
,
unsigned
int
FractionBits
,
unsigned
int
IntegerMult
=
BaseType
(
1
)
<<
FractionBits
>
class
FixedPoint
{
public:
// zero init
constexpr
FixedPoint
()
:
_internal_value
(
0
)
{}
// construct a fixed point from value
template
<
typename
T
>
constexpr
FixedPoint
(
T
val
)
:
_internal_value
(
static_cast
<
BaseType
>
(
val
*
IntegerMult
))
{}
// cast a fixed point to a data type
template
<
typename
T
>
constexpr
operator
T
()
const
{
return
static_cast
<
T
>
(
static_cast
<
T
>
(
_internal_value
)
/
IntegerMult
);
}
// addition
template
<
typename
T
>
constexpr
FixedPoint
operator
+
(
const
T
&
rhs
)
const
{
return
FixedPoint
(
add
(
this
->
_internal_value
,
rhs
),
{});
}
// subtraction
template
<
typename
T
>
constexpr
FixedPoint
operator
-
(
const
T
&
rhs
)
const
{
return
FixedPoint
(
sub
(
this
->
_internal_value
,
rhs
),
{});
}
// multiplication
template
<
typename
T
>
constexpr
FixedPoint
operator
*
(
const
T
&
rhs
)
const
{
return
FixedPoint
(
mul
(
this
->
_internal_value
,
rhs
),
{});
}
// division
template
<
typename
T
>
constexpr
FixedPoint
operator
/
(
const
T
&
rhs
)
const
{
return
FixedPoint
(
div
(
this
->
_internal_value
,
rhs
),
{});
}
// equality
template
<
typename
T
>
constexpr
bool
operator
==
(
const
T
&
rhs
)
const
{
return
eq
(
this
->
_internal_value
,
rhs
);
}
private
:
struct
placeholder
{};
constexpr
FixedPoint
(
BaseType
_val
,
placeholder
)
:
_internal_value
(
_val
)
{}
static
constexpr
BaseType
add
(
const
BaseType
_lhs
,
const
FixedPoint
_rhs
)
{
return
static_cast
<
BaseType
>
(
_lhs
+
_rhs
.
_internal_value
);
}
static
constexpr
BaseType
sub
(
const
BaseType
_lhs
,
const
FixedPoint
_rhs
)
{
return
static_cast
<
BaseType
>
(
_lhs
-
_rhs
.
_internal_value
);
}
static
constexpr
BaseType
mul
(
const
BaseType
_lhs
,
const
FixedPoint
_rhs
)
{
return
static_cast
<
BaseType
>
(
static_cast
<
GreaterType
>
(
_lhs
*
_rhs
.
_internal_value
)
/
IntegerMult
);
}
static
constexpr
BaseType
div
(
const
BaseType
_lhs
,
const
FixedPoint
_rhs
)
{
return
static_cast
<
BaseType
>
(
static_cast
<
GreaterType
>
(
_lhs
*
IntegerMult
)
/
_rhs
.
_internal_value
);
}
static
constexpr
bool
eq
(
const
BaseType
_lhs
,
const
FixedPoint
_rhs
)
{
return
_lhs
==
_rhs
.
_internal_value
;
}
BaseType
_internal_value
;
};
template
<
typename
B
,
typename
G
,
unsigned
int
F
>
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
FixedPoint
<
B
,
G
,
F
>
&
fp
)
{
os
<<
static_cast
<
double
>
(
fp
);
return
os
;
}
template
<
unsigned
int
F
>
using
Fixed32
=
FixedPoint
<
std
::
int32_t
,
std
::
int64_t
,
F
>
;
template
<
unsigned
int
F
>
using
Fixed16
=
FixedPoint
<
std
::
int16_t
,
std
::
int32_t
,
F
>
;
template
<
unsigned
int
F
>
using
Fixed8
=
FixedPoint
<
std
::
int8_t
,
std
::
int16_t
,
F
>
;
}
// namespace tug
#endif // TUG_FIXED_TYPE
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/CMakeLists.txt
+
3
−
0
View file @
d05ae677
...
...
@@ -25,3 +25,6 @@ add_custom_target(
check
COMMAND $<TARGET_FILE:testTug>
DEPENDS testTug
)
add_executable
(
fixed_point testFixedPoint.cpp
)
target_link_libraries
(
fixed_point PRIVATE tug
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/testFixedPoint.cpp
0 → 100644
+
35
−
0
View file @
d05ae677
#include
"tug/Datatypes/FixedPoint.hpp"
#include
<cstdint>
#include
<iostream>
using
namespace
tug
;
using
FPToTest
=
Fixed8
<
5
>
;
int
main
()
{
FPToTest
with
(
1.5
);
FPToTest
without
(
0.5
);
const
int
test
=
0
;
const
FPToTest
lol
=
0
;
if
(
lol
==
test
)
{
std
::
cout
<<
"This works
\n
"
;
}
double
operation
;
operation
=
with
+
without
;
std
::
cout
<<
"Addition: "
<<
operation
<<
std
::
endl
;
operation
=
with
-
without
;
std
::
cout
<<
"Subtraction: "
<<
operation
<<
std
::
endl
;
operation
=
with
*
without
;
std
::
cout
<<
"Mult: "
<<
operation
<<
std
::
endl
;
operation
=
with
/
without
;
std
::
cout
<<
"Div: "
<<
operation
<<
std
::
endl
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment