tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Maths.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11#pragma once
12
13namespace tracktion::inline core
14{
15
17constexpr int subtractNoWrap (int a, int b)
18{
19 if ((b > 0 && a < std::numeric_limits<int>::min() + b)
20 || (b < 0 && a > std::numeric_limits<int>::max() + b))
21 {
22 // Overflow would occur
24 }
25
26 return a - b;
27}
28
29//==============================================================================
30// _ _ _ _
31// __| | ___ | |_ __ _ (_)| | ___
32// / _` | / _ \| __| / _` || || |/ __|
33// | (_| || __/| |_ | (_| || || |\__ \ _ _ _
34// \__,_| \___| \__| \__,_||_||_||___/(_)(_)(_)
35//
36// Code beyond this point is implementation detail...
37//
38//==============================================================================
41static_assert(subtractNoWrap (std::numeric_limits<int>::min(), 0) == std::numeric_limits<int>::min());
42static_assert(subtractNoWrap (0, std::numeric_limits<int>::max()) == std::numeric_limits<int>::min() + 1);
43static_assert(subtractNoWrap (0, std::numeric_limits<int>::min() + 1) == std::numeric_limits<int>::max());
44static_assert(subtractNoWrap (1024, -2147483648) == std::numeric_limits<int>::min());
45static_assert(subtractNoWrap (1024, 1024) == 0);
46static_assert(subtractNoWrap (0, 0) == 0);
47static_assert(subtractNoWrap (-1'073'741'824, 1'073'741'824) == std::numeric_limits<int>::min());
48}
T max(T... args)
T min(T... args)
constexpr int subtractNoWrap(int a, int b)
Subtracts b from a without wrapping.