fp working now
parent
2bab551319
commit
cbb747634a
|
@ -2,6 +2,7 @@
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/BLT.iml" filepath="$PROJECT_DIR$/.idea/BLT.iml" />
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/Code.iml" filepath="$PROJECT_DIR$/.idea/Code.iml" />
|
<module fileurl="file://$PROJECT_DIR$/.idea/Code.iml" filepath="$PROJECT_DIR$/.idea/Code.iml" />
|
||||||
</modules>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
include(cmake/color.cmake)
|
include(cmake/color.cmake)
|
||||||
|
|
||||||
set(BLT_VERSION 0.16.0)
|
set(BLT_VERSION 0.16.1)
|
||||||
set(BLT_TEST_VERSION 0.0.1)
|
set(BLT_TEST_VERSION 0.0.1)
|
||||||
|
|
||||||
set(BLT_TARGET BLT)
|
set(BLT_TARGET BLT)
|
||||||
|
|
|
@ -19,9 +19,71 @@
|
||||||
#ifndef BLT_FIXED_POINT_H
|
#ifndef BLT_FIXED_POINT_H
|
||||||
#define BLT_FIXED_POINT_H
|
#define BLT_FIXED_POINT_H
|
||||||
|
|
||||||
|
#include <blt/std/types.h>
|
||||||
|
#include <blt/std/utility.h>
|
||||||
|
|
||||||
namespace blt
|
namespace blt
|
||||||
{
|
{
|
||||||
|
struct fp64
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
u64 v = 0;
|
||||||
|
|
||||||
|
fp64() = default;
|
||||||
|
|
||||||
|
explicit fp64(u64 v): v(v)
|
||||||
|
{}
|
||||||
|
|
||||||
|
public:
|
||||||
|
static fp64 from_u64(u64 ui)
|
||||||
|
{
|
||||||
|
fp64 fp;
|
||||||
|
fp.v = ui << 32;
|
||||||
|
return fp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fp64 from_i64(i64 si)
|
||||||
|
{
|
||||||
|
u64 ui = static_cast<u64>(si);
|
||||||
|
fp64 fp;
|
||||||
|
fp.v = ui << 32;
|
||||||
|
return fp;
|
||||||
|
}
|
||||||
|
|
||||||
|
BLT_ATTRIB_NO_INLINE friend fp64 operator+(fp64 left, fp64 right)
|
||||||
|
{
|
||||||
|
return fp64(left.v + right.v);
|
||||||
|
}
|
||||||
|
|
||||||
|
BLT_ATTRIB_NO_INLINE friend fp64 operator-(fp64 left, fp64 right)
|
||||||
|
{
|
||||||
|
return fp64(left.v - right.v);
|
||||||
|
}
|
||||||
|
|
||||||
|
BLT_ATTRIB_NO_INLINE friend fp64 operator*(fp64 left, fp64 right)
|
||||||
|
{
|
||||||
|
auto lhs = static_cast<__int128>(left.v);
|
||||||
|
auto rhs = static_cast<__int128>(right.v);
|
||||||
|
return fp64(static_cast<u64>((lhs * rhs) >> 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
BLT_ATTRIB_NO_INLINE friend fp64 operator/(fp64 left, fp64 right)
|
||||||
|
{
|
||||||
|
auto lhs = static_cast<__int128>(left.v);
|
||||||
|
auto rhs = static_cast<__int128>(right.v);
|
||||||
|
return fp64(static_cast<u64>((lhs / rhs) << 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] u64 as_u64() const
|
||||||
|
{
|
||||||
|
return v >> 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] i64 as_i64() const
|
||||||
|
{
|
||||||
|
return static_cast<i64>(v >> 32);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_FIXED_POINT_H
|
#endif //BLT_FIXED_POINT_H
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 946ebad67a21212d11a0dd4deb7cdedc297d47bc
|
Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5
|
|
@ -37,6 +37,7 @@ namespace blt::test
|
||||||
}
|
}
|
||||||
|
|
||||||
void vector_run();
|
void vector_run();
|
||||||
|
void allocator();
|
||||||
void fixed_point();
|
void fixed_point();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* <Short Description>
|
||||||
|
* Copyright (C) 2024 Brett Terpstra
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <blt_tests.h>
|
||||||
|
|
||||||
|
namespace blt::test
|
||||||
|
{
|
||||||
|
void allocator()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -85,6 +85,8 @@ int main(int argc, const char** argv)
|
||||||
parser.addArgument(blt::arg_builder("--utility").setHelp("Run tests on utility functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
parser.addArgument(blt::arg_builder("--utility").setHelp("Run tests on utility functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
parser.addArgument(blt::arg_builder("--data").setHelp("Run tests on data functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
parser.addArgument(blt::arg_builder("--data").setHelp("Run tests on data functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
parser.addArgument(blt::arg_builder("--vector").setHelp("Run tests for the vectors").setAction(blt::arg_action_t::STORE_TRUE).build());
|
parser.addArgument(blt::arg_builder("--vector").setHelp("Run tests for the vectors").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
|
parser.addArgument(blt::arg_builder("--fixed_point").setHelp("Run tests for the vectors").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
|
parser.addArgument(blt::arg_builder("--allocator").setHelp("Run tests for the vectors").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
|
|
||||||
auto args = parser.parse_args(argc, argv);
|
auto args = parser.parse_args(argc, argv);
|
||||||
|
|
||||||
|
@ -110,6 +112,9 @@ int main(int argc, const char** argv)
|
||||||
if (args.contains("--fixed_point"))
|
if (args.contains("--fixed_point"))
|
||||||
blt::test::fixed_point();
|
blt::test::fixed_point();
|
||||||
|
|
||||||
|
if (args.contains("--allocator"))
|
||||||
|
blt::test::allocator();
|
||||||
|
|
||||||
if (args.contains("--nbt"))
|
if (args.contains("--nbt"))
|
||||||
{
|
{
|
||||||
auto v = blt::arg_parse::get<std::string>(args["nbt"]);
|
auto v = blt::arg_parse::get<std::string>(args["nbt"]);
|
||||||
|
|
|
@ -16,13 +16,23 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include <blt/math/fixed_point.h>
|
#include <blt/math/fixed_point.h>
|
||||||
|
#include <blt/std/logging.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <blt_tests.h>
|
||||||
|
|
||||||
namespace blt::test
|
namespace blt::test
|
||||||
{
|
{
|
||||||
|
|
||||||
void fixed_point()
|
void fixed_point()
|
||||||
{
|
{
|
||||||
|
fp64 uv = fp64::from_u64(32);
|
||||||
|
fp64 iv = fp64::from_i64(16);
|
||||||
|
|
||||||
|
std::cout << uv.as_i64() << " : " << uv.as_u64() << std::endl;
|
||||||
|
std::cout << iv.as_i64() << " : " << iv.as_u64() << std::endl;
|
||||||
|
|
||||||
|
std::cout << (uv * iv).as_i64() << std::endl;
|
||||||
|
std::cout << (uv * iv).as_u64() << std::endl;
|
||||||
|
std::cout << (uv / iv).as_i64() << std::endl;
|
||||||
|
std::cout << (uv / iv).as_u64() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue