Giter VIP home page Giter VIP logo

Comments (1)

Pharap avatar Pharap commented on May 29, 2024

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)

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.