1

I work a lot in embedded systems and I'm just curious if anyone knows if it's generally more efficient to keep variables the same type - specifically signed or unsigned and size - when doing arithmetic operations. Or does it not matter because of compiler kungfu?

For example, is either better:

// These are member variables
int32_t output;
uint16_t alpha;  // <- Different type

// This is called by a timer
step(int32_t input) {
  output = (input - output) * alpha + output;
}

or

// These are member variables
int32_t output;
int32_t alpha;  // <- Same type

// This is called by a timer
step(int32t input) {
  output = (input - output) * alpha + output;
}
kkemper
  • 307
  • 2
  • 15
  • What type of efficiency do you talk about? Processing speed? Code size? Safe code? *(value overflow/type error/other)* – Julo Jan 20 '19 at 20:19
  • 2
    aside: If you're worried about the couple of cycles required to do conversions like this, then you're worried about the wrong thing... – UKMonkey Jan 20 '19 at 20:21
  • Possible duplicate of [What is uint\_fast32\_t and why should it be used instead of the regular int and uint32\_t?](https://stackoverflow.com/questions/8500677/what-is-uint-fast32-t-and-why-should-it-be-used-instead-of-the-regular-int-and-u) – Phisn Jan 20 '19 at 20:28
  • 2
    You'll do best by looking at machine language generated by your compiler. It may be on your microcontroller that extensions from smaller sized ints to larger ones cost extra (e.g., clearing the high byte(s) of a register) or they may be free (just done by the load instruction). Generally signed vs unsigned makes little difference - except in the important case of today's highly optimizing compilers which take advantage of the language loophole where overflow == undefined behavior == compiler can do any damn thing it wants including eliminating whole pieces of code you thought would execute! – davidbak Jan 20 '19 at 22:05
  • @UKMonkey: Mostly this is just out of curiosity but it will add up in resource constrained systems. When you do it at 10kHz on a 40 MHz microcontroller every little bit helps. – kkemper Jan 20 '19 at 22:05
  • @UKMonkey - there are real reasons to worry about cycles on real programs executing on real machines. Not every embedded system requires programmer attention to cycle-counting ... but some do. – davidbak Jan 20 '19 at 22:06
  • @davidbak: I was hoping there might be some general understanding but maybe this is too architecture/compiler dependent to be universal. I suppose I should just compare the assembly code. – kkemper Jan 20 '19 at 22:13
  • This is architecture dependent. On a 16-bit processor with native 16-bit support, without 32-bit support, 32-bit calculations would take ~2 more instructions to calculate per operation. On a 32-bit processor with support for 32-bit operations it would be probably no difference between these two. – KamilCuk Jan 20 '19 at 22:35
  • If the operation are done in software, they can be much slower for larger types and generate bigger code so it might be very important if you want a lot from a tiny CPU without much memory. – Phil1970 Jan 20 '19 at 23:51
  • I guess maybe a more succinct version of the question is "is there a cost to casting from unsigned to signed?" – kkemper Jan 21 '19 at 00:18
  • 1
    No cost for casting from unsigned to signed - machine representation is identical, only difference is semantics. Costs are for a) changing representation (e.g., extending 32-bits to 64-bits) on some machines and b) arithmetic on different sizes may cost differently (in cycles) (e.g., 16x16 multiply -> (low) 16 vs 16x16 -> 32 vs 32x32 -> (low) 32 and so on). – davidbak Jan 21 '19 at 03:16
  • @davidbak if you're impacted by this on even a micro controller, then your issue is that your algorithm is probably wrong. – UKMonkey Jan 21 '19 at 21:20
  • @UKMonkey to me it's not really about how big of an impact but if there is one or not. If I can keep these rules in mind as I write the code then the overall execution speed will be better. The code in the question is a simple but real example of doing a first order low-pass filter. "alpha" can't be negative but the other signals can be and there may be 10s of these small equations happening at a frequency that impacts other parts of the system. But it's more about knowing more than anything. – kkemper Jan 21 '19 at 21:47

0 Answers0