Skip to content

Commit

Permalink
Merge pull request #8 from Rinzii/next
Browse files Browse the repository at this point in the history
Next - matrix update
  • Loading branch information
Rinzii authored Aug 20, 2023
2 parents b48ceac + 4e9a580 commit ee7ff6d
Show file tree
Hide file tree
Showing 43 changed files with 2,255 additions and 1,760 deletions.
74 changes: 41 additions & 33 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
---
Language: Cpp
BasedOnStyle: LLVM
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Linux
ColumnLimit: 160
SpaceAfterTemplateKeyword: true
Standard: c++20
TabWidth: 4
IndentWidth: 4
UseTab: Always
NamespaceIndentation: All
FixNamespaceComments: true
CompactNamespaces: true
BasedOnStyle: Microsoft
AccessModifierOffset: -4
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveMacros: Consecutive
AlignEscapedNewlines: Right
AlignTrailingComments: Always
AllowShortEnumsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortLambdasOnASingleLine: All
AllowShortBlocksOnASingleLine: Always
AllowShortIfStatementsOnASingleLine: Always
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AccessModifierOffset: -4
AlwaysBreakTemplateDeclarations: Yes
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterAttributes: Never
BreakBeforeBraces: Allman
ColumnLimit: 160
CompactNamespaces: false
FixNamespaceComments: true
IndentPPDirectives: BeforeHash
IndentWidth: 4
IncludeCategories:
# Headers in <> with .h extension.
- Regex: '<([A-Za-z0-9\/-_])+\.h>'
Expand All @@ -31,22 +49,12 @@ IncludeCategories:
# Headers in <> without extension.
- Regex: '<([A-Za-z0-9\/-_])+>'
Priority: 30
PointerAlignment: Left
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterEnum: false
AfterFunction: false
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BeforeLambdaBody: false
BeforeWhile: false
NamespaceIndentation: All
PointerAlignment: Middle
SpaceAfterTemplateKeyword: true
Standard: c++20
TabWidth: 4
UseTab: Always



8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: init
- name: install g++11 and ninja
run: sudo apt install -yqq ninja-build g++-11
- name: update to clang 16
run: sudo apt --purge remove python3-lldb-14 && sudo wget https://apt.llvm.org/llvm.sh && sudo chmod +x llvm.sh && sudo ./llvm.sh 16
- name: configure gcc
run: cmake -S . --preset=default -B build -DCMAKE_CXX_COMPILER=g++-11
- name: configure clang
run: cmake -S . --preset=ninja-clang -B clang
run: cmake -S . --preset=ninja-clang -B clang -DCMAKE_CXX_COMPILER=clang++-16
- name: build gcc
run: cmake --build build --config=Release
- name: build clang
Expand All @@ -44,6 +46,8 @@ jobs:
run: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- name: install ninja and gcc
run: brew install ninja gcc@11
- name: update AppleClang to 14.0.3
run: sudo xcode-select -s /Applications/Xcode_14.3.app/Contents/Developer
- name: configure gcc
run: cmake -S . --preset=default -B build -DCMAKE_CXX_COMPILER=g++-11
- name: configure clang
Expand Down
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ endif()

# We will hit 1.0.0 when we have a stable API
# We will hit 0.1.0 when we have finished the basic API
set(build_version 0.0.3)
set(build_version 0.0.4)

include(CheckPIESupported)
check_pie_supported()
Expand All @@ -30,7 +30,6 @@ endif()
# TO THE IMPLEMENTOR: If MIM_BUILD_TEST is set to OFF then googletest can be deleted from the ext folder.
option(MIM_BUILD_TEST "Build mim tests" ${is_root_project})
option(MIM_INSTALL "Setup install and package steps" ${is_root_project})
option(MIM_NO_CMAKE_WARNINGS "Suppress mim warnings outputted through cmake" OFF)
option(MIM_USE_SIMD "Use SIMD instructions" OFF)


Expand Down Expand Up @@ -74,7 +73,7 @@ endif()

if (MIM_USE_SIMD)
# TODO: Add a better way to handle enabling simd internally.
#add_compile_definitions(INTERNAL_ENABLE_CHECK_FOR_SIMD)
add_compile_definitions(INTERNAL_ENABLE_CHECK_FOR_SIMD)
endif ()


Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- Nearly completely header only.
- Various utility functions for common math operations.
- Lightweight and portable. No external dependencies.
- Using modern C++20 features.
- Using modern C++ with support for C++17 and C++20.

> Mim is still in development and not ready for production use. Expect many bugs and missing features. Along with constant API change till we reach a stable release!
Expand All @@ -24,7 +24,7 @@


### Technology Used
- C++20
- C++17 & C++20
- Cmake 3.18 (may reduce)
- Doxygen (planned)
- Cppcheck
Expand Down
45 changes: 9 additions & 36 deletions cmake/GlobalConfig.cmake
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
include(cmake/Macros.cmake)

# TODO: I need more data on CYGWIN and MSYS to know if they are supported or not.
# Try to look into this further at a later date.

# check for cygwin
if(CYGWIN)
if(NOT MIM_NO_CMAKE_WARNINGS)
message(WARNING "MIM:
Cygwin may not be supported. If you notice any issues please report it!
REPORT ALL ISSUES HERE: https://github.com/Rinzii/mim/issues/
To disable this warning set MIM_NO_CMAKE_WARNINGS to ON
" )
endif ()
endif()

# check for msys
if(MSYS)
if(NOT MIM_NO_CMAKE_WARNINGS)
message(WARNING "MIM:
MSYS may not be supported. If you notice any issues please report it!
REPORT ALL ISSUES HERE: https://github.com/Rinzii/mim/issues/
To disable this warning set MIM_NO_CMAKE_WARNINGS to ON
" )
endif ()
endif()



# detect our OS
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
set(MIM_OS_WINDOWS 1)
Expand All @@ -43,7 +14,7 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "^OpenBSD$")
elseif(CMAKE_SYSTEM_NAME MATCHES "^NetBSD$")
set(MIM_OS_NETBSD 1)
else()
message(FATAL_ERROR "Unsupported OS: ${CMAKE_SYSTEM_NAME}")
message(WARNING "Mim could not detected a know supported OS: ${CMAKE_SYSTEM_NAME}")
endif()


Expand Down Expand Up @@ -71,12 +42,14 @@ endif()


# detect the architecture (note: this test won't work for cross-compilation)
include(CheckTypeSize)
check_type_size(void* SIZEOF_VOID_PTR)
if(${SIZEOF_VOID_PTR} STREQUAL "4")
set(MIM_ARCH_32BITS 1)
elseif(${SIZEOF_VOID_PTR} STREQUAL "8")
set(MIM_ARCH_64BITS 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(MIM_ARCH_X64 1)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(MIM_ARCH_X86 1)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^armv7")
set(MIM_ARCH_ARM 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
set(MIM_ARCH_ARM64 1)
else()
message(FATAL_ERROR "Unsupported architecture")
return()
Expand Down
106 changes: 78 additions & 28 deletions include/mim/cmath.hpp
Original file line number Diff line number Diff line change
@@ -1,78 +1,128 @@
// Copyright (c) 2023-Present Mim contributors (see LICENSE)

/**
* @file cmath.hpp
* @brief mim cmath provides a set of math functions from the standard cmath library implemented in constexpr.
*
* This is not a comprehensive implementation of the cmath library, but rather a set of functions that are
* useful for mim that are needed to be constexpr.
*/

#pragma once

#include <limits>
#include <type_traits>
#include <cmath>

#include "mim/mimConstants.hpp"

namespace mim::math
{



template <typename T>
constexpr T abs(const T& val)
[[nodiscard]] constexpr bool isnan(const T& val) noexcept
{
return (val >= 0) ? val : -val;
return val != val;
}

template <typename T>
constexpr T min(const T& a, const T& b)
[[nodiscard]] constexpr T abs(const T& val) noexcept
{
static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type.");
return (b < a) ? b : a;
return val == 0 ? 0 : (val < 0 ? -val : val);
}

template <typename T, typename... Args>
constexpr T min(const T& a, const T& b, const Args&... args)
{
return min(min(a, b), std::forward<Args>(args)...);
}
[[nodiscard]] constexpr auto min(const T&& a, const T&& b, const Args&&... args) noexcept
{
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(a) : std::forward<T>(b);
} else {
return ::mim::math::min(::mim::math::min(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}
}

template <typename T>
constexpr T max(const T& a, const T& b)
{
static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type.");
return (a < b) ? b : a;
}
template <typename T, typename... Args>
[[nodiscard]] constexpr auto min(T&& a, T&& b, Args&&... args) noexcept
{
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(a) : std::forward<T>(b);
} else {
return ::mim::math::min(::mim::math::min(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}
}

template <typename T, typename... Args>
constexpr T max(const T& a, const T& b, const Args&... args)
[[nodiscard]] constexpr auto max(const T&& a, const T&& b, const Args&&... args) noexcept
{
return max(max(a, b), std::forward<Args>(args)...);
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(b) : std::forward<T>(a);
} else {
return ::mim::math::max(::mim::math::max(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}

}

template <typename T, typename... Args>
[[nodiscard]] constexpr auto max(T&& a, T&& b, Args&&... args) noexcept
{
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(b) : std::forward<T>(a);
} else {
return ::mim::math::max(::mim::math::max(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}
}

namespace detail
{
template <typename T>
constexpr bool is_close(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
[[nodiscard]] constexpr bool isclose(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
{
return mim::math::abs(a - b) <= epsilon * mim::math::max(mim::math::abs(a), mim::math::abs(b));
return ::mim::math::abs(a - b) <= epsilon * ::mim::math::max(::mim::math::abs(a), ::mim::math::abs(b));
}

template <typename T>
constexpr T sqrt_helper(T x, T y) noexcept
[[nodiscard]] constexpr T sqrt_helper(T x, T y) noexcept
{
return is_close(x, y*y) ? y : sqrt_helper(x, (y + x/y) / 2);
return ::mim::math::detail::isclose(x, y*y) ? y : sqrt_helper(x, (y + x/y) / 2);
}
}


template <typename T>
constexpr T clamp(const T& val, const T& min, const T& max)
[[nodiscard]] constexpr auto clamp(const T& val, const T& lo, const T& hi) noexcept
{
return min(max(val, min), max);
return ::mim::math::min(::mim::math::max(val, lo), hi);
}

template <typename T>
constexpr T sqrt(T x) noexcept
[[nodiscard]] constexpr T sqrt(T x) noexcept
{
return detail::sqrt_helper(x, x);
return ::mim::math::detail::sqrt_helper(x, x);
}

template <typename T>
constexpr bool is_close(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
[[nodiscard]] constexpr bool isclose(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
{
return detail::is_close(a, b, epsilon);
return ::mim::math::detail::isclose(a, b, epsilon);
}

namespace detail
{
template <typename T>
[[nodiscard]] constexpr T compute_sine(const T x) noexcept
{
T{2} * x / (T{1} + x * x);
}

template <typename T>
[[nodiscard]] constexpr T sin_impl(const T x) noexcept
{
return
::mim::math::isnan(x) ? std::numeric_limits<T>::quiet_NaN() : std::numeric_limits<T>::min();
}
}

// TODO: Implement sine and cosine functions as constexpr.

} // namespace mim
Loading

0 comments on commit ee7ff6d

Please sign in to comment.