Misc code questions

Official FreeEMS vanilla firmware development, the heart and soul of the system!
jonr
QFP80 - Contributor
Posts: 46
Joined: Mon Aug 29, 2011 12:59 pm

Re: Misc code questions

Post by jonr »

I agree, although what a short/int/long is is up to the compilers. So int16 is better than int in that it actually means something one can rely on.

Also consider:

int16 Something = int32_to_int16( long 32 bit formula...);

where int32_to_int16() can be a subroutine that checks for overflow or it can be a macro the does nothing (and thus takes zero time/bytes).

#ifndef OVERFLOW_DEBUG
#define int32_to_int16(x) (short int)(x)
#endif

Very useful when you have those very rare glitches that are so hard to find (in someone else's code of course).

Other misc things - use "register int" where appropriate and keep the scope of variables as small as possible - ie:

lots of code ...
{ int i;
code that uses i
}

Is better than i being defined up higher. But maybe the compiler is good enough that it doesn't matter.
Open5xxxECU.org
User avatar
Fred
Moderator
Posts: 15431
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: Misc code questions

Post by Fred »

short is always 16, char is always 8, long is always at least 32, but could be 64. This is part of the C standard. int is a useless ambiguous term.

I've ummed and arred about switching to the uint8_t stuff, but i hate, with a vengeance, the _t suffix. I've even thought about using a mapping file, for portability (you'll like that) between compiler/arch and internal code types. Right now, though, it's not a priority :-)

Fred.
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
jonr
QFP80 - Contributor
Posts: 46
Joined: Mon Aug 29, 2011 12:59 pm

Re: Misc code questions

Post by jonr »

OK, I agree on the way you use chars/shorts/longs (although short could be larger) and I see that you avoid ints.
Open5xxxECU.org
jonr
QFP80 - Contributor
Posts: 46
Joined: Mon Aug 29, 2011 12:59 pm

Re: Misc code questions

Post by jonr »

I should mention floating point constant to bin point conversions look slightly different:

#define PI (unsigned long) (3.1415926 * (1<<16)) // PI bin 16

No speed hit because the compiler does the work and it doesn't trigger any floating point work during run time.
Open5xxxECU.org
User avatar
Fred
Moderator
Posts: 15431
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: Misc code questions

Post by Fred »

Yep, compile time floating point is in use in the code base :-)
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
Post Reply