Comments (1)
I started investigating this today to see if I could work out a way to make it viable.
Out of the techniques I've tried so far, the only one that both produced the correct result and resulted in a saving was:
template< unsigned Integer >
SFixed<Integer, 8> operator *(const SFixed<Integer, 8> & left, const SFixed<Integer, 8> & right)
{
static_assert(((Integer + 1) * 2 + 8 * 2) <= FIXED_POINTS_DETAILS::BitSize<intmax_t>::Value, "Multiplication cannot be performed, the intermediary type would be too large");
using InternalType = typename SFixed<Integer, 8>::InternalType;
using PrecisionType = typename SFixed<Integer * 2, 8 * 2>::InternalType;
const auto intermediary = (static_cast<PrecisionType>(left.getInternal()) * static_cast<PrecisionType>(right.getInternal()));
char buffer[sizeof(PrecisionType)];
for(size_t index = 0; index < sizeof(PrecisionType); ++index)
buffer[index] = reinterpret_cast<const char *>(&intermediary)[index];
const InternalType result = *reinterpret_cast<const InternalType *>(&buffer[1]);
return SFixed<Integer, 8>::fromInternal(result);
}
But this only appeared to result in an 8 byte saving in progmem, and I'm not 100% sure if it's 'legal'.
I may look into creating an AVR assembly implementation, and if that creates a significant enough saving then I'll consider adding it in, but most likely only when enabled using conditional compilation via an opt-in macro.
Regardless of which result I come up with, I won't be making this a general feature, it will be specifically for AVR because:
- Creating a version that works with big endian CPUs will be double the work
- Testing it on other CPUs (most of which I don't own) would be another hurdle
And I'd make it an opt-in feature because any solution is probably going to require the constexpr
to be dropped because Arduino only targets C++11 and it's unlikely that the operations required to pull this off without creeping into the land of undefined behaviour will be usable under C++11's constexpr
restrictions (which outlaw both variables and loops).
from fixedpointsarduino.
Related Issues (20)
- Support the use of volatile HOT 2
- Add more integer cast operators HOT 2
- Add bitshift operators HOT 6
- Replace use of void in zero-parameter functions
- Use of 'inline' in 'RawType' is redundant
- Add support for postincrement and postdecrement HOT 3
- Provide trigonometry functions HOT 1
- Disable random number functionality for non-AVR boards
- Add random number support for other board targets
- Update the dates on all licence headers
- Replace use of LargerType in constructors with ShiftType HOT 1
- Fix shift overflow bug in preincrement and predecrement operators
- Consider replacing some member functions and operators with free functions
- Consider providing constants in a separate namespace
- Provide formatting and printing functions
- Consider providing templates implementing other ways of expressing fixed points
- Add template aliases SQ and UQ in FixedPointsCommon
- Investigate the potential of adding specalisations for fixed points with zero-width fractions
- Make casting negative numbers more predictable. HOT 2
- Converting to float provides an incorrect result for certain types.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fixedpointsarduino.