Connecting Tech Pros Worldwide Forums | Help | Site Map

Portability: Harmony between PC and microcontroller

=?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#1: Jun 27 '08

I'll try to summarise this as best I can, as my last thread wasn't
very to-the-point:

The C Standard says the following two things:

* int is the natural integer type for the system.
* int must be at least 16-Bit.

Now the problem here is that these two criteria conflict if the
natural type for the system is in fact 8-Bit, which is the case with
many microcontrollers today.

As an example, let's take the following code:

char unsigned x, y;
...
x = y;

On my microcontroller compiler, this produces different assembler
depending on whether 'x' is an "unsigned int" or an "unsigned char".
If it's an "unsigned char", then the assembler is:

MOVF y, W /* Copy y to the accumulator */
MOVWF x /* Copy the accumulator to x */

However, if 'x' is an "unsigned int", then the assembler is:

MOVF y, W /* Copy y to the accumulator */
MOVWF x /* Copy the accumulator to x */
MOVF y+1, W /* Copy the next byte of y to the acc */
MOVWF x+1 /* Copy the acc to the next byte of x */

Now quite plainly to see, the "int" version takes twice as many
instructions in this case, and will therefore take exactly twice as
long to execute, and so will be twice as slow. In other situations,
the difference is far worse; let's take for example the following
code:

if (condition) x = y;

Depending on the type of x and y, this produces either:

MOVF y, W /* Copy y to the accumulator */
BTFSC condition /* If condition is false, skip the next
instruction */
MOVWF x /* Copy the accumulator to x */

or:

BTFSS condition /* Skip the next instruction if condition is true
*/
GOTO There
MOVF y, W /* Copy y to the accumulator */
MOVWF x /* Copy the accumulator to x */
MOVF y+1, W /* Copy the next byte of y to the acc */
MOVWF x+1 /* Copy the acc to the next byte of x */
There:

Not only does the int version consist of more instructions, but it
also involves a goto which will take up even more time. So basically
if your microcontroller is running at 8 MHz, then you may aswell
pretend it's running at 4 MHz or 2 MHz if you're going to be using int
for doing everyday arithmetic.

Now we could go down the road of discussing how C is inadequate in
terms of its accommodation of microcontrollers, but I'd rather discuss
ways of "making it right". The reason I'm so eager to bridge the gap
is that, other than the "int" situation, C is actually great for
programming an embedded system. I used it in my college project this
year to program a portable electronic Connect4 game, and it worked
great!

One way of making things right is to stop using int for arbitrarily
storing numbers, and instead use something like ufast8 (the fastest
integer type that's at least 8-Bit). In this way, neither the
microcontrollers nor the PC's suffer.

Examples of a piece of code that could be brought between PC's and
microcontrollers is something like a look-up table as follows:

ufast8 days_in_month[12] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

To those people out there who are enthusiastic about writing portable
code, how do you feel about using types such as ufast8 instead of int?

stdint.h is great and all, but I find the type names to be too long
winded. For instance I'd rather write "ufast8" instead of
"uint_fast8_t".

Jack Klein
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On Sun, 4 May 2008 18:16:43 -0700 (PDT), Tomás Ó hÉilidhe
<toe@lavabit.comwrote in comp.lang.c:
Quote:
>
I'll try to summarise this as best I can, as my last thread wasn't
very to-the-point:
>
The C Standard says the following two things:
>
* int is the natural integer type for the system.
* int must be at least 16-Bit.
You are, perhaps unintentionally, paraphrasing the standard in a way
that appears to change the meaning. These are not two isolated
statements, but are in fact combined in one sentence.

"A ‘‘plain’’ int object has the natural size suggested by the
architecture of the execution environment (large enough to contain any
value in the range INT_MIN to INT_MAX as defined in the header
<limits.h>)."

If you think about it for a bit, the standard is highlighting the fact
that the single most important requirement for the int data type is
meeting or exceeding the range of values specified by those macros.
The first part of the sentence exists in the context of the
requirement in parentheses.

So an int is not just the natural size suggested by the architecture,
and just also, separately, must be at least 16 bits to meet the range
requirements.

The meaning of the standard is that, above all other considerations,
int must represent a minimum range of values. The type that an
implementation uses is the natural size, that is perhaps, the "best"
or "most efficient" type for meeting that primary requirement.
Quote:
Now the problem here is that these two criteria conflict if the
natural type for the system is in fact 8-Bit, which is the case with
many microcontrollers today.
One of the things that you might not realize is that the C programming
language was developed originally on a 16-bit minicomputer. At the
time Dennis was developing C, there was no such thing as an 8-bit
microprocessor in existence. Let alone 8-bit microcontrollers which
evolved after the microprocessor.

At the time of the first C compilers, I don't think there was an
architecture, and certainly not a common one, where the natural size
for an integer type was less than 16 bits.

Remember as well that the C language was not designed for embedded
systems in general, let alone for 8-bit devices in particular.

C was designed as a system programming language for minicomputers on
up through mainframes and super computers, where 16-bit processors
where the smallest kids on the block.

It was a decision by the embedded programming community to adopt C as
an embedded programming language, not any conspiracy by the "C Mafia"
or the UNIX zealots to push it on us.

C is not perfect for embedded use in many ways, even on 16- and 32-bit
hardware, but it is so vastly superior to all the alternatives that
most of us have ever seen, there is no contest.

So we make do with "ANSI standard" compilers that are missing required
data types and other parts of the language, to get what C does
provide, namely a simple and standard core language that allows
efficient code generation for the architectures that we use. Of
course "efficient" is a relative term.

If you want to know why we took to C so readily, take a look at some
of the alternatives. If I never see or write code in PL/M as long as
I live, it will be too soon. Spend a little time looking into Forth,
while you're at it.
Quote:
As an example, let's take the following code:
>
char unsigned x, y;
...
x = y;
>
On my microcontroller compiler, this produces different assembler
depending on whether 'x' is an "unsigned int" or an "unsigned char".
If it's an "unsigned char", then the assembler is:
I don't really want to be antagonistic or overly patronizing, but I
would like you to consider a few other points.

The first is that you are doing a large amount of preaching based on a
little experience with exactly one 8-bit architecture, and a severely
brain-dead one at that. Long before the PIC was developed, there were
8-bit processors such as the Z80 or 6809 which could handle 16-bit
ints much better than PIC can.

But there is another important fact that you seem to be overlooking.
It is rare, even in 8-bit embedded systems, for an entire application
to not use any values too large to fit in an unsigned char.

You have put together a small embedded system as an educational
experience, either as part of a course or on your own initiative. From
your description of the hardware on another group, it is a relatively
simple project that reads a few switches and drives a few LEDs.

In the real world, most embedded systems have more complex jobs to do,
even 8-bit ones.

You are not going to execute a PID control loop using only values in
the range 0 to 255. Nor would you be able to execute much in the way
of complex timing calculations.

The fact that, in your particular 8-bit project, you could do
everything using only the unsigned char data type is actually quite
artificial. I may have worked on a few embedded systems over the
years that never needed anything larger than 16-bit values, but I'm
fairly sure I never worked on a real world embedded system that never
needed anything larger than 8 bits.
Quote:
MOVF y, W /* Copy y to the accumulator */
MOVWF x /* Copy the accumulator to x */
>
However, if 'x' is an "unsigned int", then the assembler is:
>
MOVF y, W /* Copy y to the accumulator */
MOVWF x /* Copy the accumulator to x */
MOVF y+1, W /* Copy the next byte of y to the acc */
MOVWF x+1 /* Copy the acc to the next byte of x */
>
Now quite plainly to see, the "int" version takes twice as many
instructions in this case, and will therefore take exactly twice as
long to execute, and so will be twice as slow. In other situations,
the difference is far worse; let's take for example the following
code:
Happily, there are 8-bit architectures that have been around more than
30 years that can handle 16-bit data more efficiently.
Quote:
if (condition) x = y;
>
Depending on the type of x and y, this produces either:
>
MOVF y, W /* Copy y to the accumulator */
BTFSC condition /* If condition is false, skip the next
instruction */
MOVWF x /* Copy the accumulator to x */
>
or:
>
BTFSS condition /* Skip the next instruction if condition is true
*/
GOTO There
MOVF y, W /* Copy y to the accumulator */
MOVWF x /* Copy the accumulator to x */
MOVF y+1, W /* Copy the next byte of y to the acc */
MOVWF x+1 /* Copy the acc to the next byte of x */
There:
>
Not only does the int version consist of more instructions, but it
also involves a goto which will take up even more time. So basically
if your microcontroller is running at 8 MHz, then you may aswell
pretend it's running at 4 MHz or 2 MHz if you're going to be using int
for doing everyday arithmetic.
Now we're getting somewhere. "Everyday" arithmetic, indeed. You have
just discovered that there is often nothing "everyday" about embedded
systems.
Quote:
Now we could go down the road of discussing how C is inadequate in
terms of its accommodation of microcontrollers, but I'd rather discuss
ways of "making it right". The reason I'm so eager to bridge the gap
is that, other than the "int" situation, C is actually great for
programming an embedded system. I used it in my college project this
year to program a portable electronic Connect4 game, and it worked
great!
Here's where there's a point of view difference between you and many
in this group, not limited to me. C is not "inadequate" in
accommodating microcontrollers. No accommodation is required for
architectures like ARM and PowerPC, and even some of the 16-bit
architectures.

On the other hand, C's accommodation for 8-bit hardware can't be
described as either adequate or inadequate, because there is no C
accommodation at all for such platforms. Remember again that it was
the embedded community that decided to adopt C, not the developers of
C who targeted them.
Quote:
One way of making things right is to stop using int for arbitrarily
storing numbers, and instead use something like ufast8 (the fastest
integer type that's at least 8-Bit). In this way, neither the
microcontrollers nor the PC's suffer.
Actually, you're wrong about the fact that there is no downside to
such code on a PC.

To explain what that is, I have to lecture a bit.

You express a certain amount of indifference, if not disdain, to
professional programming. I have a feeling that you have little
understanding of professional programming in particular, and perhaps
engineering disciplines in general. Especially when it comes to what
it takes to develop safety critical and other high reliability
applications.

Who is going to do the analysis on every integer value used in a
complex operation to determine whether it will or will not ever exceed
8 bits? Who is going to write the additional test cases to prove it?
Quote:
Examples of a piece of code that could be brought between PC's and
microcontrollers is something like a look-up table as follows:
>
ufast8 days_in_month[12] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
>
To those people out there who are enthusiastic about writing portable
code, how do you feel about using types such as ufast8 instead of int?
The problems is that you are not encouraging portable programming so
much as you are encouraging people who have no relationship to 8- and
16-bit platforms to write code that might happen to more efficient
when compiled for small platforms.

I've never been a programming manager, nor do I want to be, but if I
were and I saw a programmer making the extra effort to do the analysis
and testing to do this on desktop code, Windows or Linux or Mac, I
would correct him for wasting time. And it he was doing it without
performing the extra analysis and testing, I'd correct him even more
severely. He'd be wasting time and effort on detail that would have
absolutely no benefit to the final program.

Try doing another embedded project, this time with an ARM. ST just
announced some ARM parts with up to 2MB of flash and 96KB of RAM.

Or even develop a more realistic and complex application on a PIC.
Quote:
stdint.h is great and all, but I find the type names to be too long
winded. For instance I'd rather write "ufast8" instead of
"uint_fast8_t".
That is an unfortunate attitude. The extended types defined in
stdint.h are indeed not particularly attractive, but they have the
advantage of being part of the language standard. They replace the
great balkanization of type names invented by many others in the years
before C added them.

unit_fast8_t is absolutely 100% standard. Every conforming C99
compiler must define this type in stdint.h. By now, the latest
versions of most embedded compilers include a stdint.h that defines
this alias.

If you were truly interested in portability you would use the types
that the language instead of inventing your own to suit your
esthetics.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
CBFalconer
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Jack Klein wrote:
Quote:
>
.... snip ...
Quote:
>
One of the things that you might not realize is that the C
programming language was developed originally on a 16-bit
minicomputer. At the time Dennis was developing C, there was no
such thing as an 8-bit microprocessor in existence. Let alone
8-bit microcontrollers which evolved after the microprocessor.
Cough, hack. Remember the PDP8? :-) Also the Microdata 800
series, which were less common, and 8 bit oriented. The
microprogrammed Microdata 810 was a 16 bit (and byte) oriented
machine. In 1968 as I recall.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
Martin Ambuhl
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


CBFalconer wrote:
Quote:
Jack Klein wrote:
.... snip ...
Quote:
>One of the things that you might not realize is that the C
>programming language was developed originally on a 16-bit
>minicomputer. At the time Dennis was developing C, there was no
>such thing as an 8-bit microprocessor in existence. Let alone
>8-bit microcontrollers which evolved after the microprocessor.
>
Cough, hack. Remember the PDP8? :-)
Cough, hack. The PDP-8 and PDP-5 were 12 bit machines.
Bartc
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Portability: Harmony between PC and microcontroller



"Tomás Ó hÉilidhe" <toe@lavabit.comwrote in message
news:582c24fa-1cc4-49f0-8ca7-171928a1a38e@8g2000hse.googlegroups.com...
Quote:
>
I'll try to summarise this as best I can, as my last thread wasn't
very to-the-point:
>
The C Standard says the following two things:
>
* int is the natural integer type for the system.
* int must be at least 16-Bit.
>
Now the problem here is that these two criteria conflict if the
natural type for the system is in fact 8-Bit, which is the case with
many microcontrollers today.
>
As an example, let's take the following code:
>
char unsigned x, y;
...
x = y;
It doesn't seem an insurmountable problem.

If you want a default int size that is best for your cpu, try something
like:

typedef unsigned char uint; /* Or uint_fast8_t etc. */
typedef signed char sint;

Then use uint and sint everywhere in place of unsigned/signed int.

When moving to a bigger processor, you need to change those two lines or use
some conditional compilation tricks.

--
Bartc


=?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Portability: Harmony between PC and microcontroller



Thanks for the reply.


On May 5, 3:42*am, Jack Klein <jackkl...@spamcop.netwrote:
Quote:
Try doing another embedded project, this time with an ARM. *ST just
announced some ARM parts with up to 2MB of flash and 96KB of RAM.

For my next hobby project, I want to make a very simple two-port
router. When the router receives a packet, it will look up the IP
address in its routing table, and then decide what port to forward it
out on and what destination MAC address to use. That's pretty much all
it will do. Of course I'll have to make it do a few other things, like
send and receive ARP requests, but nothing too complicated.

I started throwing some code together in notepad, just to see how I'd
make it work. Now the thing is, I see no reason why I shouldn't be
able to move this code over to a PC. Here's the beginnings of it:

typedef uint_fast32_t IPv4addr;
typedef uint_fast64_t MACaddr;

typedef struct RoutingTableEntry {
IPv4addr addr;
IPv4addr mask;
IPv4addr router_addr;
uint_fast8_t port; /* Here's a great example of where I'd
normally use "unsigned int" */
} RoutingTableEntry;


typedef struct InfoForForwarding {
uint_fast8_t port;
MACaddr dst_mac;
} InfoForForwarding;

#define LEN_ROUTING_TABLE 16u
RoutingTableEntry routing_table[LEN_ROUTING_TABLE]; /* Hold 16
routes max in the table */
#define pend_routing_table (routing_table + LEN_ROUTING_TABLE)

InfoForForwarding GetInfoForForwarding(IPv4addr const dst_ip)
{
InfoForForwarding iff = { 0 };

RoutingTableEntry const *p = routing_table;

do
{
if ((dst_ip & p->mask) == p->addr)
{
iff.port = p->port;

/* Now consult ARP table to get MAC address of router */
iff.dst_mac = GetMACaddr(iff.port,p->router_addr);

return iff;
}

} while (pend_routing_table != ++p);

return iff;
}

As I hope you'll agree from looking at this code, there's nothing
microcontroller-specific or PC-specific about it. There's no reason
why the code couldn't be used to make a PC program that would
implement a "virtual router" between two network cards.

It appears that quite a few people think that PC programming and
embedded programming are quite separate from each other, but I hope my
code example above shows why there's no reason why code can't migrate
and be portable between the two. Many C programmers already are
enthusiastic about their code being portable, but I just hope they'd
consider microcontrollers too.

Slightly off-topically, I don't know if you've been following my
thread entitled "Ethernet in its most basic form". I've been asking
around to see what microcontroller I should use for making my little
two port router. I've been given many suggestions of microcontrollers
that will work with one sole ethernet port, but obviously I'll need a
microcontroller that will work with two. (Or then again I might need
two microcontrollers that will communicate with each other... ?). I
don't suppose you'd have any idea what I should use for that? I want
to work at 100 MBps full-duplex.
Keith Thompson
Guest
 
Posts: n/a
#7: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


"Bartc" <bc@freeuk.comwrites:
Quote:
"Tomás Ó hÉilidhe" <toe@lavabit.comwrote in message
news:582c24fa-1cc4-49f0-8ca7-171928a1a38e@8g2000hse.googlegroups.com...
Quote:
>>
>I'll try to summarise this as best I can, as my last thread wasn't
>very to-the-point:
>>
>The C Standard says the following two things:
>>
>* int is the natural integer type for the system.
>* int must be at least 16-Bit.
>>
>Now the problem here is that these two criteria conflict if the
>natural type for the system is in fact 8-Bit, which is the case with
>many microcontrollers today.
[...]
Quote:
>
It doesn't seem an insurmountable problem.
>
If you want a default int size that is best for your cpu, try something
like:
>
typedef unsigned char uint; /* Or uint_fast8_t etc. */
typedef signed char sint;
>
Then use uint and sint everywhere in place of unsigned/signed int.
>
When moving to a bigger processor, you need to change those two lines or use
some conditional compilation tricks.
*Please* don't call them "uint" and "sint".

What the name "uint" says to me is "unsigned int, but I care more
about saving a few keystrokes than writing clear code"; likewise for
"sint". The only thing worse than typedef'ing "unsigned int" to
"uint" is typedef'ing something else to "uint". I understand that
"uint" is intended to convey "unsigned integer" rather than "unsigned
int", but that's not how it comes across.

If you want to call them, say, "small_signed" and "small_unsigned",
that's fine.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
CBFalconer
Guest
 
Posts: n/a
#8: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Martin Ambuhl wrote:
Quote:
CBFalconer wrote:
Quote:
>Jack Klein wrote:
>.... snip ...
Quote:
>>One of the things that you might not realize is that the C
>>programming language was developed originally on a 16-bit
>>minicomputer. At the time Dennis was developing C, there was no
>>such thing as an 8-bit microprocessor in existence. Let alone
>>8-bit microcontrollers which evolved after the microprocessor.
>>
>Cough, hack. Remember the PDP8? :-)
>
Cough, hack. The PDP-8 and PDP-5 were 12 bit machines.
Exactly. Those are not exactly 16 bit minicomputers.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
CBFalconer
Guest
 
Posts: n/a
#9: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Tomás Ó hÉilidhe wrote:
Quote:
Jack Klein <jackkl...@spamcop.netwrote:
>
Quote:
>Try doing another embedded project, this time with an ARM. ST just
>announced some ARM parts with up to 2MB of flash and 96KB of RAM.
>
.... snip ...
Quote:
>
As I hope you'll agree from looking at this code, there's nothing
microcontroller-specific or PC-specific about it. There's no reason
why the code couldn't be used to make a PC program that would
implement a "virtual router" between two network cards.
I suggest you avoid all those peculiar definitions of integral
objects. The use of char, short, int, long is normally all you
need (unsigned or signed) and doesn't confuse the reader with yards
of definitions. C is based on values, not sizes of operands. See
limits.h.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
Chris Dollin
Guest
 
Posts: n/a
#10: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Tomás Ó hÉilidhe wrote:
Quote:
>
I'll try to summarise this as best I can, as my last thread wasn't
very to-the-point:
>
The C Standard says the following two things:
>
* int is the natural integer type for the system.
* int must be at least 16-Bit.
>
Now the problem here is that these two criteria conflict if the
natural type for the system is in fact 8-Bit, which is the case with
many microcontrollers today.
That just means that those microcontrollers aren't a natural fit
to C, so programmers writing looks-like-C for them need to be
aware that natural-C idioms might not work as nicely.

I don't see a problem here.

--
"It was the first really clever thing the King had /Alice in Wonderland/
said that day."

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

=?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#11: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 6, 12:33*pm, Chris Dollin <chris.dol...@hp.comwrote:
Quote:
That just means that those microcontrollers aren't a natural fit
to C, so programmers writing looks-like-C for them need to be
aware that natural-C idioms might not work as nicely.

This is what I'm against. When I first started programming in C for
embedded systems, I was weary of the compiler's compliance to the
Standard. I was hesitant to rely on rules from the Standard when it
came to things like:
* Minimum size of integer types
* Behaviour of overflow
* Existance and usage of a stack

Having written a fully working program though in C for embedded
systems, and also having looked at the assembler produced to check
what it's actually doing, I've seen that my embedded compile is
extremely compliant. I defined an object as "long unsigned", and lo
and behold the assembler produced used four bytes to store it (even
though it can only do arithmetic on 8-Bit numbers).

Quote:
I don't see a problem here.

The problem comes with writing portable code. For instance, I'm
currently writing code to implement an internet protocol router. The
code show be able to run on both a microcontroller and on a PC.
However, if the code uses "int" then the code will be less efficient
on a microcontroller. And if it uses "char" then the code will be less
efficient on a PC. Usage of uint_fast8_t would produce optimal
assembler for both systems.

There's no reason why there has to be an "embedded version of C"
distinct from "Standard C".
Chris Dollin
Guest
 
Posts: n/a
#12: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Tomás Ó hÉilidhe wrote:
Quote:
On May 6, 12:33Â*pm, Chris Dollin <chris.dol...@hp.comwrote:
>
Quote:
>That just means that those microcontrollers aren't a natural fit
>to C, so programmers writing looks-like-C for them need to be
>aware that natural-C idioms might not work as nicely.
>
>
This is what I'm against. When I first started programming in C for
embedded systems, I was weary of the compiler's compliance to the
Standard. I was hesitant to rely on rules from the Standard when it
came to things like:
* Minimum size of integer types
* Behaviour of overflow
* Existance and usage of a stack
>
Having written a fully working program though in C for embedded
systems, and also having looked at the assembler produced to check
what it's actually doing, I've seen that my embedded compile is
extremely compliant. I defined an object as "long unsigned", and lo
and behold the assembler produced used four bytes to store it (even
though it can only do arithmetic on 8-Bit numbers).
>
>
Quote:
>I don't see a problem here.
>
>
The problem comes with writing portable code.
No, it doesn't. Not if the compiler conforms to the standard.
Quote:
For instance, I'm
currently writing code to implement an internet protocol router. The
code show be able to run on both a microcontroller and on a PC.
However, if the code uses "int" then the code will be less efficient
on a microcontroller.
Your problem is not with portability; it's with performance.
Different platforms can have differing performance profiles
at whim, and portable code may need tweaking for best performance
on /any/ of them. Singling out performance issues on 8-bit
micros and thinking everyone should write code so that it
(by /hypothesis/) performs equally (well, badly) on those
implementations is, I think, obsession over microefficiency.

--
"The one you're playing, I think." /The Lion Game/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

=?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#13: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 6, 1:40*pm, Chris Dollin <chris.dol...@hp.comwrote:
Quote:
Your problem is not with portability; it's with performance.

They can be one in the same thing if the performance affects the
usability of the product. If I port a web browser to a Nintendo gaming
console, is it really a succesful port if it takes seven minutes to
load a webpage? I don't think it is.

Quote:
Different platforms can have differing performance profiles
at whim, and portable code may need tweaking for best performance
on /any/ of them.

Yes but there are more fundamental concepts here, that is, the choice
of integer types, whether to use 1, 2, or 4 bytes to store a number.

You say that different platforms have differing performance profiles,
and you're right. What gets better performance on one system might
result in poorer performance on another. But let me draw a crude
analogy:

Let's say you have a farm, and you want to get the best performance
out of all your animals. For the sheep, you might give them a field of
nice thick grass. For the young chicks, you might keep them in a
heated enclosure. For the horses, you might give them a vast open
space to run around. But there'a more fundamental way of getting
better performance out of all your animals -- give them water.

Just as water is a common thing to all animals, integer types are
common to all computers. Before you bother doing specialised things
for each animal such as giving them more grass or more space, do the
universal thing first: water.

And for computers, this universal thing is the choice of integer
types. You can have the best optimiser in the world, but it can only
do so good if you're using sub-optimal integer types.

Quote:
on 8-bit
micros and thinking everyone should write code so that it
(by /hypothesis/) performs equally (well, badly) on those
implementations is, I think, obsession over microefficiency.

Firstly, zero effort would go into making it perform equally well on
both systems. It's just a matter of getting into the habit of using
uint_fast8_t instead of unsigned int where possible.

For my current embedded project, if I were to change "uint_fast8_t"
from "unsigned char" to "unsigned int", then I bet I'd see flicker in
my display (because the microcontroller can't flash the display fast
enough so that the human eye can't see the flashing). I've already
submitted my project board to my college to be graded but I should be
getting it back tomorrow. I'll try it out changing the integer types
and see if the display flickers. If it does, then I'll have to reduce
the duration of each flash, which will result in a dimmer display.
soscpd@terra.com.br
Guest
 
Posts: n/a
#14: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 6, 9:06*am, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
Quote:
They can be one in the same thing if the performance affects the
usability of the product. If I port a web browser to a Nintendo gaming
console, is it really a succesful port if it takes seven minutes to
load a webpage? I don't think it is.
Did it load? Yes? Then, it is. Don't think a nintendo (hey... some
nintendo can work very fine here... :) hardware was designed to work
as a http daemon (maybe that is not the product!). Performance isn't
100% hardware and, isn't too, 100% code. Run a vanilla 2.4 kernel in a
dual xeon with 1Tb memory, and run the same in a 286 with 1Mb RAM. Do
you really expect code tuning, best practices or hacks to make both
run, at least close to each other?

Did you pick the wrong language or the wrong hardware (haven't you
picked C? haven't you picked the 8 bit platform? Why mix them if you
think that will not work?)? That is the question I think you must
answer before post C standards or limits as the source of your
problems. The right tools Tomás. The right tools.


Regards
Rafael
=?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#15: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 6, 3:32*pm, "sos...@terra.com.br" <sos...@terra.com.brwrote:
Quote:
On May 6, 9:06*am, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
>
Quote:
They can be one in the same thing if the performance affects the
usability of the product. If I port a web browser to a Nintendo gaming
console, is it really a succesful port if it takes seven minutes to
load a webpage? I don't think it is.
>
Did it load? Yes? Then, it is.

I'd love to actually have to take a trial of it. I'd love to have you
sit down in a room with a desk and a chair and my machine running the
ported webbrowser. I'd love to see you type in "google.ie" and wait 7
minutes for it to load.

I'd lose the plot if I had to wait one minute for a page to load, let
alone seven.

Quote:
Don't think a nintendo (hey... some
nintendo can work very fine here... :) hardware was designed to work
as a http daemon (maybe that is not the product!).

Nintendo was an arbitrary choice on my part by the way.
(Also, the "daemon" is the program that runs on the server, not the
client. The daemon listens on a port number, e.g. port 80 for HTTP,
and processes requests to that port number).

Quote:
Performance isn't
100% hardware and, isn't too, 100% code. Run a vanilla 2.4 kernel in a
dual xeon with 1Tb memory, and run the same in a 286 with 1Mb RAM. Do
you really expect code tuning, best practices or hacks to make both
run, at least close to each other?

I'm talking about getting optimal performance out of every system,
whether it runs at 31 kHz or 3.6 Ghz.

Quote:
Did you pick the wrong language or the wrong hardware (haven't you
picked C? haven't you picked the 8 bit platform? Why mix them if you
think that will not work?)?

They work great if used properly.

Quote:
That is the question I think you must
answer before post C standards or limits as the source of your
problems. The right tools Tomás. The right tools.

I'm not talking about changing tools, or even about critiquing the C
language. I'm talking about adopting a habit of using the likes of
uint_fast8_t instead of int, because it will lead to faster code on
every conceivable platform.
Eric Sosman
Guest
 
Posts: n/a
#16: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Tomás Ó hÉilidhe wrote:
Quote:
[...] When I first started programming in C for
embedded systems, I was weary of the compiler's compliance to the
Standard. [...]
Quote:
The problem comes with writing portable code. [...]
There seems to be a contradiction here. If Compiler X and
Compiler Y are in effect their own Standards, how is writing
portable code made easier?

Perhaps you don't recall the state of affairs before 1989,
but I do. A degree of portability was possible, but even with
much labor one could not reached the levels of portability that
are achievable (with far less effort) today. Some sprintf()
implementations returned a count, others returned a pointer.
Some compilers used <string.h>, others used <strings.h>. Some
supported struct-valued functions, some did not. Some promoted
`unsigned char' to `int', some to `unsigned int'. Some could
do token-pasting by eliminating /**/ in macro expansions, some
did not. And so on, and so on -- a "portable" program in those
days was really a whole suite of related programs packed into
one set of source files and de-multiplexed with #ifdef.

I for one found those days far more wearying than any of
the compromises a Standard requires of us.
Quote:
There's no reason why there has to be an "embedded version of C"
distinct from "Standard C".
It seems to me the distinction between "hosted" and
"free-standing" implementations is useful for the developers
of the latter. Your toaster probably doesn't need complex
numbers, nor wcstomb(), nor setlocale(), nor exit(), and it
would be a hardship for implementors if they had to provide
all this stuff that would never be used.

--
Eric.Sosman@sun.com
Chris Dollin
Guest
 
Posts: n/a
#17: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Tomás Ó hÉilidhe wrote:
Quote:
On May 6, 1:40Â*pm, Chris Dollin <chris.dol...@hp.comwrote:
>
Quote:
>Your problem is not with portability; it's with performance.
>
They can be one in the same thing if the performance affects the
usability of the product. If I port a web browser to a Nintendo gaming
console, is it really a succesful port if it takes seven minutes to
load a webpage? I don't think it is.
It's a nice hypothetical example, but that's all it is -- hypothetical.
If it actually happened that way, then one would profile the browser
and find out where the time went, and address the problem. I very
much suspect that sizes of integers would be a non-issue in this
case.
Quote:
Firstly, zero effort would go into making it perform equally well on
both systems. It's just a matter of getting into the habit of using
uint_fast8_t instead of unsigned int where possible.
In the kind of C code I've written, I think that would be
almost nowhere.

--
"The next threat we face may be something quite different." /Buffy/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

soscpd@terra.com.br
Guest
 
Posts: n/a
#18: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 6, 10:49*am, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
Quote:
I'd love to actually have to take a trial of it. I'd love to have you
sit down in a room with a desk and a chair and my machine running the
ported webbrowser. I'd love to see you type in "google.ie" and wait 7
minutes for it to load.
Pointless.
Quote:
Nintendo was an arbitrary choice on my part by the way.
(Also, the "daemon" is the program that runs on the server, not the
client. The daemon listens on a port number, e.g. port 80 for HTTP,
and processes requests to that port number).
Sorry. Didn't get the browser in your text, and didn't realize someone
typing a url with a joystick. I am not a game player.
Quote:
I'm talking about getting optimal performance out of every system,
whether it runs at 31 kHz or 3.6 Ghz.
Nonsense. Aren't you the guy who "...lose the plot if had to wait one
minute for a page to load, let alone seven...."? Can you build the
same browser, with the same code, to this 2 machines? Do you think
that is the best product? Shouldnt the 31khz guy use (sample) linx and
the 3Ghz one use whatever he like (including linx)?

If performance was the only issue, why shold one have to use something
portable or standard? Or either keep the 8 bit platform? Hack around
my friend. You cannot answer all the worlds questions with a single
answer. Each system demand his own way to deal with the same problems.
If you try to build a web browser to run in a (sample) nintendo and in
a PC, you will need to "port" something. Maybe everything.
Quote:
They work great if used properly.
Where? For what? By who? When? How? Answer that to have a percent of
the meaning of "properly".

Regards
Rafael


=?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#19: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 6, 5:40*pm, "sos...@terra.com.br" <sos...@terra.com.brwrote:
Quote:
Quote:
I'd love to actually have to take a trial of it. I'd love to have you
sit down in a room with a desk and a chair and my machine running the
ported webbrowser. I'd love to see you type in "google.ie" and wait 7
minutes for it to load.
>
Pointless.

The point is to show you that a person is dissatisfied with it. This
means that the port was a failure.

Quote:
Sorry. Didn't get the browser in your text, and didn't realize someone
typing a url with a joystick. I am not a game player.

Again my choice of Nintendo and also of a webbrowser were arbitrary.

A better example would be an instant messenger client running on a
small handheld device using an LCD display. If the algorithmic code is
too slow, then the display will suffer. Using int instead of
uint_fast8_t will lead to code which is about 2 to 4 times slower.

Quote:
Quote:
I'm talking about getting optimal performance out of every system,
whether it runs at 31 kHz or 3.6 Ghz.
>
Nonsense. Aren't you the guy who "...lose the plot if had to wait one
minute for a page to load, let alone seven...."?

Yes, I am that guy -- I'd probably smash the keyboard off the ground
if it took seven minutes to load a webpage. I'd then probably launch
the monitor out a window (a closed one, preferably).

Quote:
If performance was the only issue, why shold one have to use something
portable or standard?

I'm talking about portable performance, not just performance.

Quote:
Quote:
(In regard to using C with embedded systems)
They work great if used properly.
>
Where? For what? By who? When? How? Answer that to have a percent of
the meaning of "properly".

Countless programs have been written in C for embedded systems. I've
written one full one myself.

All I'm talking about here is using types such as uint_fast8_t instead
of it. I think int should be abandoned altogether.
Keith Thompson
Guest
 
Posts: n/a
#20: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Tomás Ó hÉilidhe <toe@lavabit.comwrites:
Quote:
On May 6, 12:33*pm, Chris Dollin <chris.dol...@hp.comwrote:
Quote:
>That just means that those microcontrollers aren't a natural fit
>to C, so programmers writing looks-like-C for them need to be
>aware that natural-C idioms might not work as nicely.
>
>
This is what I'm against. When I first started programming in C for
embedded systems, I was weary of the compiler's compliance to the
(I presume you mean "wary", not "weary".)
Quote:
Standard. I was hesitant to rely on rules from the Standard when it
came to things like:
* Minimum size of integer types
Nitpick: The standard defines minimum ranges for integer types, but
the effect is the same.

I've heard of C-like compilers that provide a type "int" with a range
narrower than -32767 .. +32767. That's ok, as long as there's no
claim that it conforms to the C standard. But I would expect the
compiler's documentation to *clearly* state the ways in which it fails
to conform.
Quote:
* Behaviour of overflow
For floating-point and signed integers, the standard doesn't define
the behavior of overflow. For unsigned integers, the required
behavior is well defined; it wraps around modulo 2**N. I'd be
surprised if even a non-conforming C-like compiler didn't do this (it
should be straightforward to implement in hardware). But again, if
the behavior differs from what the standard specifies, I'd expect it
to be clearly stated in the documentation.
Quote:
* Existance and usage of a stack
The C standard doesn't even use the word "stack" (and *please* let's
not re-open the argument about what "stack" means). It does require
that functions can be called recursively. If the implementation can't
support that, then again, I'd expect that to be clearly documented.

[snip]

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
soscpd
Guest
 
Posts: n/a
#21: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 6, 5:40 pm, "sos...@terra.com.br" <sos...@terra.com.brwrote:

Quote:
The point is to show you that a person is dissatisfied with it. This
means that the port was a failure.
The port is a hack. If someone is "dissatisfied with it", buy or build
something better.
Quote:
Quote:
Sorry. Didn't get the browser in your text, and didn't realize someone
typing a url with a joystick. I am not a game player.
Quote:
Again my choice of Nintendo and also of a web browser were arbitrary.
That was your arbitrary choice to explain your subjects.
I can just assume you know what you are talking about.
Quote:
A better example would be an instant messenger client running on a
small handheld device using an LCD display. If the algorithmic code is
too slow, then the display will suffer. Using int instead of
uint_fast8_t will lead to code which is about 2 to 4 times slower.
Have you EVER wrote a instant messenger to ANY embedded device?
The embedded device use a PIC? Or either a Nintendo processor (if yes,
again, its a hack)?
Quote:
Quote:
Yes, I am that guy -- I'd probably smash the keyboard off the ground
if it took seven minutes to load a webpage. I'd then probably launch
the monitor out a window (a closed one, preferably).
As more you tell me about yourself, less I am interested in you as a
personal or professional contact.
Keep that to you, or cut the coffee before answer back. I am kind of
taking your thread seriously, so far.
Quote:
If performance was the only issue, why should one have to use something
portable or standard?
Quote:
I'm talking about portable performance, not just performance.
So, try to plan where you will use your code. The nintendo choice
fails here.
Quote:
Quote:
(In regard to using C with embedded systems)
They work great if used properly.
Where? For what? By who? When? How? Answer that to have a percent of
the meaning of "properly".
How many embedded systems exists?
They are all the same?
Do you plan to reuse all the code of embedded systems again on, say,
PC?
Do you know what a project is? Solution, maybe?
Have you ever designed some hardware to a specific job
(power contactors/airplane CDU's/Black Box Firmware/Radar/Wireless
AP... something?)?
Do you know how reliable this need (demand) to be?
Think I should work with this kind of projects using portable code?
Quote:
Countless programs have been written in C for embedded systems. I've
written one full one myself.
Congrats! If you write some 362 more (just embedded... lets keep the
other domains out here, but ASM is included), we will have the same
experience!!
Quote:
All I'm talking about here is using types such as uint_fast8_t instead
of it. I think int should be abandoned altogether.
Ok. Do it.

Look Tomàs... I am really trying to get the "where we are going"
here.
The whole point is to convince people to code just as you think they
should?


Regards
Rafael
Bart van Ingen Schenau
Guest
 
Posts: n/a
#22: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On 6 mei, 19:19, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
Quote:
A better example would be an instant messenger client running on a
small handheld device using an LCD display. If the algorithmic code is
too slow, then the display will suffer. Using int instead of
uint_fast8_t will lead to code which is about 2 to 4 times slower.
This is where your reasoning starts to break down.
A small, handheld device is likely to be using an ARM-based processor.
These processors are nowadays almost universally 32-bitters. (A really
old ARM processof might be 16 bit).
Therefore, the difference between using 'int' and 'uint_fast8_t' is
expected to be zero.

The problem with your reasoning is that you try to extrapolate from a
single experience with one particular embedded system. Your
extrapolation just does not work.
I would strongly advise you to gather data-points on about a dozen
different processors (with as much difference between them as you can)
before making sweeping recommendations like you do in this thread.

<snip>
Quote:
I'm talking about portable performance, not just performance.
Then tell me, what is the performance of that webbrowser on your PIC?

Bart v Ingen Schenau
robertwessel2@yahoo.com
Guest
 
Posts: n/a
#23: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 7, 8:05*am, Bart van Ingen Schenau
<Bart.van.Ingen.Sche...@ict.nlwrote:
Quote:
A small, handheld device is likely to be using an ARM-based processor.
These processors are nowadays almost universally 32-bitters. (A really
old ARM processof might be 16 bit).

There were never any 16 bit ARMs, assuming you mean "16 bit" to be
descriptive of the ISA. ARM1 and ARM2 supported only a 26 bit address
space, but are pretty obsolete at this point.
Flash Gordon
Guest
 
Posts: n/a
#24: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Bart van Ingen Schenau wrote, On 07/05/08 14:05:
Quote:
On 6 mei, 19:19, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
>
Quote:
>A better example would be an instant messenger client running on a
>small handheld device using an LCD display. If the algorithmic code is
>too slow, then the display will suffer. Using int instead of
>uint_fast8_t will lead to code which is about 2 to 4 times slower.
Personally I don't see anything wrong with using the fast types when not
dealing with arrays. With arrays sometimes on some architectures the
fast type will be slower. Of course, you need to know the range you
require, but you need to know that anyway to decide if int is large
enough or if you need long.
Quote:
This is where your reasoning starts to break down.
A small, handheld device is likely to be using an ARM-based processor.
These processors are nowadays almost universally 32-bitters. (A really
old ARM processof might be 16 bit).
Therefore, the difference between using 'int' and 'uint_fast8_t' is
expected to be zero.
That does not show that uint_fast8_t is wrong.
Quote:
The problem with your reasoning is that you try to extrapolate from a
single experience with one particular embedded system. Your
extrapolation just does not work.
I would strongly advise you to gather data-points on about a dozen
different processors (with as much difference between them as you can)
before making sweeping recommendations like you do in this thread.
I agree. Now I'll give a reason for *not* using the fast types.
Sometimes you want to be able to test your code on another platform
because you have better tools and you want to know that the variable
ranges will be the same as on your final target even if it means running
more slowly!
Quote:
Quote:
>I'm talking about portable performance, not just performance.
>
Then tell me, what is the performance of that webbrowser on your PIC?
An algorithm that you might want running on either a small embedded
processor or a high powered graphics workstation is one to work out the
optimum brightness/contrast setting for an image based on some
statistics. I know you might want it on a small processor because I had
to implement it on one and then later port it to another smaller
processor (in both cases HW collected the statistics, I only had to
process them in real time), and the use on a high powered graphics
workstation should be obvious. The same can apply to some other image
processing algorithms, so it does happen.
--
Flash Gordon
Bart van Ingen Schenau
Guest
 
Posts: n/a
#25: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 7, 8:45 pm, "robertwess...@yahoo.com" <robertwess...@yahoo.com>
wrote:
Quote:
On May 7, 8:05 am, Bart van Ingen Schenau
>
<Bart.van.Ingen.Sche...@ict.nlwrote:
Quote:
A small, handheld device is likely to be using an ARM-based processor.
These processors are nowadays almost universally 32-bitters. (A really
old ARM processof might be 16 bit).
>
There were never any 16 bit ARMs, assuming you mean "16 bit" to be
descriptive of the ISA. ARM1 and ARM2 supported only a 26 bit address
space, but are pretty obsolete at this point.
Thanks. I am not familiar with the old ARMs, so my comment was purely
based on a guess.

Bart v Ingen Schenau
Bart van Ingen Schenau
Guest
 
Posts: n/a
#26: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 7, 9:01 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
Quote:
Bart van Ingen Schenau wrote, On 07/05/08 14:05:
>
Quote:
This is where your reasoning starts to break down.
A small, handheld device is likely to be using an ARM-based processor.
These processors are nowadays almost universally 32-bitters. (A really
old ARM processof might be 16 bit).
Therefore, the difference between using 'int' and 'uint_fast8_t' is
expected to be zero.
>
That does not show that uint_fast8_t is wrong.
But is also does not show that 'int' is wrong. That is my whole point.

<snip>
Quote:
An algorithm that you might want running on either a small embedded
processor or a high powered graphics workstation is one to work out the
optimum brightness/contrast setting for an image based on some
statistics. I know you might want it on a small processor because I had
to implement it on one and then later port it to another smaller
processor (in both cases HW collected the statistics, I only had to
process them in real time), and the use on a high powered graphics
workstation should be obvious. The same can apply to some other image
processing algorithms, so it does happen.
Yes, it does happen.
But do you use identical implementations of the algorithm for both the
low-powered and the high-powered device, or do you use different
implementations that make the best use of the capabilities of the
processor?

When implementing an algorithm for a high-powered architecture, do you
take it into account that the same algorithm might one day be useful
on a low-powered device (possibly without efficient floating point
support)? Or do you do it only if you already know that the code will
be ported to such a device?

My point is that supporting low-powered devices takes a lot more than
just selecting the right integer types.

Bart v Ingen Schenau
Quote:
--
Flash Gordon
santosh
Guest
 
Posts: n/a
#27: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Tomás Ó hÉilidhe wrote:
Quote:
On May 6, 8:01*pm, "Herbert Rosenau" <os2...@pc-rosenau.dewrote:
<snip>
Quote:
Quote:
>A programmer with brain
>for only 1 penny will decide to use the right data type that fits the
>needs for the value it has to fit in. This will be char if the range
>of the value fits in, it will be int, long or long long otherwise.
>
A common novice mistake. If you use char to store numbers on a PC,
then you'll end up with code that is both slower and consumes more
memory. Slower because more instructions are needed to deal with
integer types that are smaller than the registers. Consume more memory
because you have to store those extra instructions somewhere.
This isn't necessarily true. An optimising compiler will most likely
store the char object in a system register and manipulate it as such.
So the object code is likely to use the same instructions that would've
been used for an int object.

Of course what you say could also be the case. One can't really say
without observing the actual object code.

I use int for all values below INT_MAX (or UINT_MAX) and long and long
long for bigger values. I generally won't consider short or char unless
the object is to be an array with considerable elements, in which case
the space saving may be worth it.

<snip>

jacob navia
Guest
 
Posts: n/a
#28: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


santosh wrote:
Quote:
>
I use int for all values below INT_MAX (or UINT_MAX) and long and long
long for bigger values. I generally won't consider short or char unless
the object is to be an array with considerable elements, in which case
the space saving may be worth it.
>
That is the ONLY serious reason for using char/short.

If you have a million of objects, then switching from int to char
and packing the structure can save you 3MB of storage. Depending on
the structure, if you have
struct foo {
char a,b,c,d;
int e,f;
};

struct foo {
int a,b,c,d;
int e,f;
};

Packing your structure can save you 100% space: the size is
3 integers in the first example (assuming 32 bit integers)
and 6 integers in the second example. For one million
copies you use 12 MB vs 24MB!

Obviously for just one copy nobody cares...


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Ben Pfaff
Guest
 
Posts: n/a
#29: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


jacob navia <jacob@nospam.orgwrites:
Quote:
Packing your structure can save you 100% space: the size is
3 integers in the first example (assuming 32 bit integers)
and 6 integers in the second example. For one million
copies you use 12 MB vs 24MB!
To save 100% of the space, you'd need to shrink the structure to
0 bytes.
--
Ben Pfaff
http://benpfaff.org
Ian Collins
Guest
 
Posts: n/a
#30: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


jacob navia wrote:
Quote:
santosh wrote:
Quote:
>>
>I use int for all values below INT_MAX (or UINT_MAX) and long and long
>long for bigger values. I generally won't consider short or char unless
>the object is to be an array with considerable elements, in which case
>the space saving may be worth it.
>>
>
That is the ONLY serious reason for using char/short.
>
Ever written a device driver? When interfacing with the physical world,
size matters.

--
Ian Collins.
=?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#31: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


On May 16, 10:28*pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Quote:
That is the ONLY serious reason for using char/short.
>
Ever written a device driver? *When interfacing with the physical world,
size matters.

For exact-size types I'd go with the likes of "uint16" in <stdint.h>.
jacob navia
Guest
 
Posts: n/a
#32: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Ben Pfaff wrote:
Quote:
jacob navia <jacob@nospam.orgwrites:
>
Quote:
>Packing your structure can save you 100% space: the size is
>3 integers in the first example (assuming 32 bit integers)
>and 6 integers in the second example. For one million
>copies you use 12 MB vs 24MB!
>
To save 100% of the space, you'd need to shrink the structure to
0 bytes.
Obviously I mean that an increase of 12 to 24 MB is a 100% increase...



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Chris H
Guest
 
Posts: n/a
#33: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


In message
<4a86c482-fdc2-437a-b297-5c49971f1ac7@8g2000hse.googlegroups.com>, Tomás
Ó hÉilidhe <toe@lavabit.comwrites
Quote:
>On May 16, 10:28*pm, Ian Collins <ian-n...@hotmail.comwrote:
>
Quote:
Quote:
That is the ONLY serious reason for using char/short.
>>
>Ever written a device driver? *When interfacing with the physical world,
>size matters.
>
>
>For exact-size types I'd go with the likes of "uint16" in <stdint.h>.
And SBIT


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



Ian Collins
Guest
 
Posts: n/a
#34: Jun 27 '08

re: Portability: Harmony between PC and microcontroller


Chris H wrote:
Quote:
In message
<4a86c482-fdc2-437a-b297-5c49971f1ac7@8g2000hse.googlegroups.com>, Tomás
Ó hÉilidhe <toe@lavabit.comwrites
Quote:
>On May 16, 10:28 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>
Quote:
>That is the ONLY serious reason for using char/short.
>>>
>>Ever written a device driver? When interfacing with the physical world,
>>size matters.
>>
>>
>For exact-size types I'd go with the likes of "uint16" in <stdint.h>.
>
And SBIT
>
What?

--
Ian Collins.
Closed Thread