Giter Site home page Giter Site logo

mays's Introduction

๐Ÿงฎ mays ๐ŸŒฝ

Tests Coverage Formatting Static analysis

mays is a well-tested header-only collection of โ€œsafe-ishโ€ C++ integer math routines intended for GNU-compatible compilers (Clang and GCC). They are intended to help coders avoid subtle machine and C/C++ arithmetic pitfalls, as well as perform compile-time safety checks and precomputation with constexpr.

Example

This uses the Scale and Reduce routines to build a Scaler that scales int16_t values at compile time:

#include "mays/reduce.h"
#include "mays/scale.h"

// Simplify the ratio 51 / 114 to 17 / 38 at compile time.
constexpr std::tuple ratio = mays::Reduce(int16_t{51}, int16_t{114});

// Construct a Scaler that operates on int16_t values using |ratio|.
// Because this is marked constexpr, if the ratio can overflow the scaling
// computation then this will fail to compile.
constexpr mays::Scaler scaler = mays::MakeScaler<int16_t>(ratio);

// This performs a scaling operation on the value 30000. The return type is
// std::optional<int16_t> to represent the possibility of overflow when
// scaling up numbers. The ratio here is โ‰ค 1 so overflow is not possible.
constexpr std::optional value = scaler.Scale(int16_t{30'000});
static_assert(value == 13'421);

// It's also possible to use a different rounding policy.
static_assert(scaler.Scale(int16_t{30'000},
                           mays::RoundPolicy::kRoundAwayFromZero) == 13'422);

// |scaler| can be used on values not known at compile time, e.g. if you're
// building a viewport resizing function:
std::function<int16_t(int16_t)> get_resized_dimension = [scaler](int16_t dim) {
  return scaler.Scale(dim).value();
};

Usage

The included CMakeLists.txt exports a CMake library target mays. โ€œLinkingโ€ your project against it will pull in the necessary include directory, which is intended to be the root of this project:

# Assuming the mays project has already been included
target_link_libraries(${YOUR_PROJECT_NAME} PRIVATE mays)

To download the code from GitHub and include the mays CMake project in your own project, FetchContent is a good choice:

Include(FetchContent)

FetchContent_Declare(
  mays
  GIT_REPOSITORY https://github.com/GHF/mays.git
  GIT_TAG        dev
)

FetchContent_MakeAvailable(mays)

add_executable(my_program main.cpp)
target_link_libraries(my_program PRIVATE mays)

To build and run the unit tests, download the code and build it with CMake:

git clone https://github.com/GHF/mays.git
cd mays
cmake -S . -B build
cd build
cmake --build .
ctest

For other build systems, it's only necessary to place the header files into your build:

top level
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ mays
    โ”œโ”€โ”€ internal
    โ”‚ย ย  โ””โ”€โ”€ โ€ฆ.h
    โ””โ”€โ”€ โ€ฆ.h

Guide

The code can be logically organized by their intended purpose:

Overflow-safe basic arithmetic

Safety-conscious computation

Numeric utilities

Opinionated tasks

  • RangeMap Joystick-to-process value mapping code
  • Crc Single-header (no C++ or mays includes) CRC with compile-time generated look-up tables

License

mays is licensed under the Apache 2.0 license.

mays's People

Contributors

ghf avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.