<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 2009-October-10, at 8:19 PM, Aaron J. Grier wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>On Tue, Oct 06, 2009 at 11:05:21AM +0200, Thomas Doerfler wrote:<br><blockquote type="cite">But in various applications size matters. So if you have let's say<br></blockquote><blockquote type="cite">128KByte of RAM in your (space?) system and you need an answer to the<br></blockquote><blockquote type="cite">question "which data types might be appropriate if I want to save an<br></blockquote><blockquote type="cite">array of 16000 measured data samples" then getting this information<br></blockquote><blockquote type="cite">may be quite important. ok?<br></blockquote><br>isn't this what the stdint.h types are for?  uint32_t, int8_t, etc.<br></div></blockquote><div><br></div>Sorry to chime in, but ABSOLUTELY! One of the basic MISRA guidelines for the use of the C language in safety critical systems is that you never use the base types of the C language. The size is processor and compiler implementation dependant.</div><div><br></div><div>The standard trick question is "How many bits is an 'int' in C?" Answer: an int is the natural size of the processor. So on a Microchip PIC I found out, the hard way, that an int is 8 bits, but that depends on which family of PIC. On an HC12 it is 16 bits. A CDC Cyber is 60 bits. Etcetera. Only the compiler writers know how big an int might be on an Intel core duo, 32 or 64.</div><div><br></div><div>One of my standard things is to define, and check the size of, CHAR, BOOLEAN, INT8U, INT8S, INT16U, INT16S, INT32U, INT32S, INT64U, INT64S, FP32 and FP64, with code like:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(162, 0, 104); ">typedef<span style="color: #000000"> </span>unsigned<span style="color: #000000"> </span>char<span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">    </span></span><span style="color: #006541">BOOLEAN</span><span style="color: #000000">;</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">unsigned</span><span style="color: #000000"> </span><span style="color: #a20068">char</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">  </span></span><span style="color: #006541">INT8U</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                  </span></span>/* unsigned 8 bit quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">signed</span><span style="color: #000000"> </span><span style="color: #a20068">char</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">   </span></span><span style="color: #006541">INT8S</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                  </span></span>/* signed 8 bit quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">unsigned</span><span style="color: #000000"> </span><span style="color: #a20068">short</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">  </span></span><span style="color: #006541">INT16U</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                 </span></span>/* unsigned 16 bit quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">signed</span><span style="color: #000000"> </span><span style="color: #a20068">short</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre"> </span></span><span style="color: #006541">INT16S</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                 </span></span>/* signed 16 bit quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">unsigned</span><span style="color: #000000"> </span><span style="color: #a20068">int</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">   </span></span><span style="color: #006541">INT32U</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                 </span></span>/* unsigned 32 bit quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">signed</span><span style="color: #000000"> </span><span style="color: #a20068">int</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">   </span></span><span style="color: #006541">INT32S</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                 </span></span>/* signed 32 bit quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">float</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">              </span></span><span style="color: #006541">FP32</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                   </span></span>/* Single precision floating point quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(45, 148, 114); "><span style="color: #a20068">typedef</span><span style="color: #000000"> </span><span style="color: #a20068">double</span><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">           </span></span><span style="color: #006541">FP64</span><span style="color: #000000">;<span class="Apple-tab-span" style="white-space:pre">                   </span></span>/* Double precision floating point quantity */</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(162, 0, 104); ">typedef<span style="color: #000000"> </span>char<span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">           </span></span><span style="color: #006541">CHAR</span><span style="color: #000000">;</span></div><div><font class="Apple-style-span" face="Monaco" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div></div><div>that way there is no question about size. And to check:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(162, 0, 104); ">static<span style="color: #000000"> </span>const<span style="color: #000000"> </span>union<span style="color: #000000"> {</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">    </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>int8u_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">INT8U</span><span style="color: #000000">) == 1];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre"> </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>int8s_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">INT8S</span><span style="color: #000000">) == 1];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">  </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>int16u_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">INT16U</span><span style="color: #000000">) == 2];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">       </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>int16s_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">INT16S</span><span style="color: #000000">) == 2];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>int32u_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) == 4];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">       </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>int32s_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">INT32S</span><span style="color: #000000">) == 4];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>fp32_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">FP32</span><span style="color: #000000">) == 4];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(0, 0, 207); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">   </span></span><span style="color: #006541">INT8U</span><span style="color: #000000"> </span>fp64_t_incorrect<span style="color: #000000">[(</span><span style="color: #006541">INT32U</span><span style="color: #000000">) </span><span style="color: #a20068">sizeof</span><span style="color: #000000">(</span><span style="color: #006541">FP64</span><span style="color: #000000">) == 8];</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; ">} typeChecker;</div><div><font class="Apple-style-span" face="Monaco" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div></div><div><br></div><div>This will cause a compile time error if the sizes are not as expected.</div><div><br></div><div><br></div><div>uint32_t and such only came around in C99 and still aren't being adopted by the industry. C# actually discourages such things right in their style guidelines, preferring to use ints (but then again they also prefer not to put braces around single line ifs...dangerous habits).</div><div><br></div><div>Also, int8_t doesn't explicitly indicate its signedness, it is still open to interpretation. I can never remember the signedness of an int, which also determines their range. I think it is best to be explicit.</div><div><br></div><div><br></div><div>I was doing some timing tests earlier. It was quite amazing how slow software floating point is. It takes about 24 times as much time as a scaled integer equivalent (MCF5282). On the other hand there wasn't a huge difference between multiply and addition times on the same type. 32 bit ops were a bit quicker than 16 bit and quite a bit faster than 8 bit. You could gain a bunch of performance by using a data type that is favoured by your processor.</div><div><br></div><div>You might not want to store 16000 32 bit numbers because they are faster in calculations, even though you only need 16 bits, but you may want to investigate using 32s in your calculations.</div><div><br></div><div><br></div><div>Also watch for padding when dealing with structs and simple variables, put your largest data elements (INT32[US]) first, leaving the byte size stuff till the end. Compilers will throw in extra bytes that never get used to get the alignment correct. </div><div><br></div><div>32s usually have to be longword aligned (address % 4 == 0), 16s word aligned (even address only), and 8s can be anywhere. An 8 followed by a 32 may have 3 bytes of filler so that the 32 will be aligned properly. Without keeping this in mind, your structs may be larger than the sum of their parts, your heaps fragmented, and stacks may take up more space than you think. Check your map file if space is tight.</div><div><br></div><div>Basically you have to watch the size and signedness of your data, match them to the processor, and arrange them so they don't make filler holes.</div><div><br></div><div>Andrei</div><div><span class="Apple-style-span" style="font-size: 12px; ">---------------------</span></div><div><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><span class="Apple-style-span" style="font-size: 12px; "><div>Andrei Chichak</div><div><br></div><div>Systems Developer</div><div>CBF Systems Inc.</div><div>4-038 NINT Innovation Centre</div><div>11421 Saskatchewan Drive</div><div>Edmonton, Alberta</div><div>Canada</div><div>T6G 2M9</div><div><br></div><div>Phone: 780-628-2072</div><div>Skype: andrei.chichak</div></span></div></div></span></div></span></div></div>
</div>

<br></body></html>