This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 209
Fixing misuse of the standard math library (performance audit) #41
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
60498fc
Poisoned the C library identifiers
pavel-kirienko e7af7b1
Un-poisoned fabs
pavel-kirienko a162fe1
Fixed inclusions in matrix/
pavel-kirienko 0d915ae
Removed all use of C library from the matrix namespace
pavel-kirienko da138f1
Removed all uses of C library from tests
pavel-kirienko e8768bd
Fixed stdlib imports
pavel-kirienko 14e9465
NuttX workaround
pavel-kirienko 8ef2527
NuttX math lib workaround
pavel-kirienko 41fe2cd
Style fix
pavel-kirienko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
|
||
#pragma once | ||
|
||
#include <cmath> | ||
#include <cstdio> | ||
#include <cstring> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,6 @@ | |
|
||
#pragma once | ||
|
||
#include <cmath> | ||
|
||
#include "math.hpp" | ||
|
||
namespace matrix | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#pragma once | ||
|
||
#include "math.hpp" | ||
#include <cmath> | ||
|
||
namespace matrix | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#pragma once | ||
|
||
#include "stdlib_imports.hpp" | ||
#ifdef __PX4_QURT | ||
#include "dspal_math.h" | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* @file stdlib_imports.hpp | ||
* | ||
* This file is needed to shadow the C standard library math functions with ones provided by the C++ standard library. | ||
* This way we can guarantee that unwanted functions from the C library will never creep back in unexpectedly. | ||
* | ||
* @author Pavel Kirienko <[email protected]> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cmath> | ||
#include <cstdlib> | ||
#include <cinttypes> | ||
|
||
namespace matrix { | ||
|
||
#if defined(__PX4_NUTTX) | ||
/* | ||
* NuttX has no usable C++ math library, so we need to provide the needed definitions here manually. | ||
*/ | ||
#define MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(name) \ | ||
inline float name(float x) { return ::name##f(x); } \ | ||
inline double name(double x) { return ::name(x); } \ | ||
inline long double name(long double x) { return ::name##l(x); } | ||
|
||
#define MATRIX_NUTTX_WRAP_MATH_FUN_BINARY(name) \ | ||
inline float name(float x, float y) { return ::name##f(x, y); } \ | ||
inline double name(double x, double y) { return ::name(x, y); } \ | ||
inline long double name(long double x, long double y) { return ::name##l(x, y); } | ||
|
||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(fabs) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(log) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(log10) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(exp) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(sqrt) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(sin) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(cos) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(tan) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(asin) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(acos) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(atan) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(sinh) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(cosh) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(tanh) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(ceil) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_UNARY(floor) | ||
|
||
MATRIX_NUTTX_WRAP_MATH_FUN_BINARY(pow) | ||
MATRIX_NUTTX_WRAP_MATH_FUN_BINARY(atan2) | ||
|
||
#else // Not NuttX, using the C++ standard library | ||
|
||
using std::abs; | ||
using std::div; | ||
using std::fabs; | ||
using std::fmod; | ||
using std::exp; | ||
using std::log; | ||
using std::log10; | ||
using std::pow; | ||
using std::sqrt; | ||
using std::sin; | ||
using std::cos; | ||
using std::tan; | ||
using std::asin; | ||
using std::acos; | ||
using std::atan; | ||
using std::atan2; | ||
using std::sinh; | ||
using std::cosh; | ||
using std::tanh; | ||
using std::ceil; | ||
using std::floor; | ||
using std::frexp; | ||
using std::ldexp; | ||
using std::modf; | ||
|
||
# if (__cplusplus >= 201103L) | ||
|
||
using std::imaxabs; | ||
using std::imaxdiv; | ||
using std::remainder; | ||
using std::remquo; | ||
using std::fma; | ||
using std::fmax; | ||
using std::fmin; | ||
using std::fdim; | ||
using std::nan; | ||
using std::nanf; | ||
using std::nanl; | ||
using std::exp2; | ||
using std::expm1; | ||
using std::log2; | ||
using std::log1p; | ||
using std::cbrt; | ||
using std::hypot; | ||
using std::asinh; | ||
using std::acosh; | ||
using std::atanh; | ||
using std::erf; | ||
using std::erfc; | ||
using std::tgamma; | ||
using std::lgamma; | ||
using std::trunc; | ||
using std::round; | ||
using std::nearbyint; | ||
using std::rint; | ||
using std::scalbn; | ||
using std::ilogb; | ||
using std::logb; | ||
using std::nextafter; | ||
using std::copysign; | ||
using std::fpclassify; | ||
using std::isfinite; | ||
using std::isinf; | ||
using std::isnan; | ||
using std::isnormal; | ||
using std::signbit; | ||
using std::isgreater; | ||
using std::isgreaterequal; | ||
using std::isless; | ||
using std::islessequal; | ||
using std::islessgreater; | ||
using std::isunordered; | ||
|
||
# endif | ||
#endif | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to detect NuttX in general instead of the PX4 flavor of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davids5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pavel-kirienko
For the record no there is none. So far, a change to add NUTTX to the generated config.h will not be accepted upstream. So this has to be set in the build by the flags, and we do that with __PX4_NUTTX.