<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: compiler math - or type cast 32 bit math to 16 bit constant. in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135336#M1700</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Perhaps I was not clear.&lt;BR /&gt;&lt;BR /&gt;For the example I gave you I agree with your statement.&lt;BR /&gt;&lt;BR /&gt;But lets say later in the code I have&lt;BR /&gt;&lt;BR /&gt;unsigned short GLOBALVAR&lt;BR /&gt;unsigned short ANOUTHERVAR&lt;BR /&gt;GLOBALVAR = ANOUTHERVAR / V_HOLDING&lt;BR /&gt;&lt;BR /&gt;If V_HOLDING is only 18 then it should get assigned a byte size and the compiler can hopefully use the divide 16 bit by 8 bit op code that the S08 has. However if V_HOLDING is a type long my math just got way more complex.&amp;nbsp; This is why I think I would need two types of #defines for V_Holding if I use that approach.&lt;BR /&gt;&lt;BR /&gt;Am I missing something? Will the compiler optimize that out?&lt;BR /&gt;&lt;BR /&gt;Rick&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;Message Edited by RShaich on &lt;SPAN class="date_text"&gt;2007-05-30&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;09:50 PM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 31 May 2007 03:50:05 GMT</pubDate>
    <dc:creator>RShaich</dc:creator>
    <dc:date>2007-05-31T03:50:05Z</dc:date>
    <item>
      <title>compiler math - or type cast 32 bit math to 16 bit constant.</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135332#M1696</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;SPAN&gt;I am looking for the best way to do math at the compiler level in 32 bit (unsigned long) using unsigned short #defines and assign to unsigned short constants.&amp;nbsp; I have lots of formulas that define constants that are based on values that we keep changing as the clients needs change.&amp;nbsp; I don't want to have to keep changing my code so I want formulas in the constant declarations.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The issue seems to be that if the formula, in any step exceeds the variable definition it craps out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I guess I should think that the compiler could say do the calcs in floating point and then assign the int result to a 16 bit constant.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Or at least do it in 32bit int and assign the result to a 16 bit unsigned int constant. So for example I have this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#define PWM_MODULUS 0x0400&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#define V_PULLIN&amp;nbsp; 100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#define V_HOLDING 18&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;const unsigned short PWM_HOLDING&amp;nbsp; =&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (PWM_MODULUS *&amp;nbsp; V_HOLDING / 120)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;const unsigned short PWM_PULLIN = ((PWM_MODULUS *&amp;nbsp; V_PULLIN) / 120)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PWM_HOLDING works fine and gets assigned 153. The 0.6 is lost. (I know I should do ( (PWM_MODULUS *&amp;nbsp; V_HOLDING / 125) +1) so it rounds correctly)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However even though PWM_PULLIN&amp;nbsp; should be 853, a legal value for an unsigned short, it does not work since PWM_MODULUS *&amp;nbsp; V_PULLIN is &amp;gt; 0xFFFF.&amp;nbsp;&amp;nbsp; It seems this should work since the compiler should be able to handle the math, but it does not.&amp;nbsp; Since I keep changing V_Pullin, V_Holding, and PWM_Modulus it would be nice to get this working. How should this be done?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I assume I could make my #defines 32 bit (how do I do that can I say #define V_PULLIN&amp;nbsp; 100L) but in other places in the code I need them to be just byte (char) size so I want to leave it alone.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So the question is how can I tell the compiler that for constants I want to have mixed types do the math at long (32 bit) and then save the result to a short (16 bit) constant?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is there a compiler directive or way to state to do the math as unsigned long and assign the result as unsigned short?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also I need to do lots of 24 bit math. I know long is 32 bit, short is 16 bit but is there a definition for a 24 bit word? I don't have that much memory so a 3 byte word would be nice over a 4 byte.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanx&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;Rick&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Oh this is with CW 5.1 on a 9S08AW part if that matters.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Message Edited by RShaich on &lt;/SPAN&gt;&lt;SPAN class="date_text"&gt;2007-05-30&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN class="time_text"&gt;06:01 PM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 May 2007 23:59:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135332#M1696</guid>
      <dc:creator>RShaich</dc:creator>
      <dc:date>2007-05-30T23:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: compiler math - or type cast 32 bit math to 16 bit constant.</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135333#M1697</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello Rick,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;I found that using&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;#define V_PULLIN 100L&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;seems to work OK for me.&amp;nbsp; This eliminates the "loss of data" warning.&amp;nbsp; This would force the result of the multiplication to be a 32-bit value, which is what you require.&amp;nbsp; However, PWM_PULLIN appears to have the correct 16-bit value assigned.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 May 2007 01:00:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135333#M1697</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-05-31T01:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: compiler math - or type cast 32 bit math to 16 bit constant.</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135334#M1698</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thanx.&lt;BR /&gt;That is a start I guess.&lt;BR /&gt;The problem is that there are other places in the code where the #define needs to be only 8 bits as defined. I had hoped I could do this in the constant declaration and not the #define macro declaration. Perhaps I am going to have to have two sets of # defines for this constant?&lt;BR /&gt;&lt;BR /&gt;Interestingly enough I was not getting a loss of data error on that one.&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 May 2007 01:34:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135334#M1698</guid>
      <dc:creator>RShaich</dc:creator>
      <dc:date>2007-05-31T01:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: compiler math - or type cast 32 bit math to 16 bit constant.</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135335#M1699</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello Rick,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;The fact that the intermediate calculations use 32-bit values shouldn't matter, provided the final result fits within the constant declaration, whether it be a 16-bit or an 8-bit value.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;For example,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;#define V_HOLDING 18L&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;will also work correctly, even though the intermediate calculations actually do not need to be 32-bit.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;On this basis, I don't think you will need two sets of #define's.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 May 2007 03:37:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135335#M1699</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-05-31T03:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: compiler math - or type cast 32 bit math to 16 bit constant.</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135336#M1700</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Perhaps I was not clear.&lt;BR /&gt;&lt;BR /&gt;For the example I gave you I agree with your statement.&lt;BR /&gt;&lt;BR /&gt;But lets say later in the code I have&lt;BR /&gt;&lt;BR /&gt;unsigned short GLOBALVAR&lt;BR /&gt;unsigned short ANOUTHERVAR&lt;BR /&gt;GLOBALVAR = ANOUTHERVAR / V_HOLDING&lt;BR /&gt;&lt;BR /&gt;If V_HOLDING is only 18 then it should get assigned a byte size and the compiler can hopefully use the divide 16 bit by 8 bit op code that the S08 has. However if V_HOLDING is a type long my math just got way more complex.&amp;nbsp; This is why I think I would need two types of #defines for V_Holding if I use that approach.&lt;BR /&gt;&lt;BR /&gt;Am I missing something? Will the compiler optimize that out?&lt;BR /&gt;&lt;BR /&gt;Rick&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;Message Edited by RShaich on &lt;SPAN class="date_text"&gt;2007-05-30&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;09:50 PM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 May 2007 03:50:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135336#M1700</guid>
      <dc:creator>RShaich</dc:creator>
      <dc:date>2007-05-31T03:50:05Z</dc:date>
    </item>
    <item>
      <title>Re: compiler math - or type cast 32 bit math to 16 bit constant.</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135337#M1701</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;There are a couple of possibilities that&amp;nbsp;seem to give the desired results -&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;#define PWM_MODULUS 0x0400&lt;BR /&gt;#define V_PULLIN&amp;nbsp; 100L&lt;BR /&gt;#define V_HOLDING 18L&lt;BR /&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;const unsigned short PWM_HOLDING&amp;nbsp;=&amp;nbsp;(PWM_MODULUS *&amp;nbsp;V_HOLDING) / 120;&lt;BR /&gt;const unsigned short PWM_PULLIN = (PWM_MODULUS *&amp;nbsp;V_PULLIN) / 120;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;GLOBALVAR = ANOUTHERVAR / (char)V_HOLDING;&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="Arial"&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;The second possibility is as follows, where PWM_MODULUS is forced to be a 32-bit value,&amp;nbsp;giving the result of the multiplication also 32-bit.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;#define PWM_MODULUS 0x0400L&lt;BR /&gt;#define V_PULLIN&amp;nbsp; 100&lt;BR /&gt;#define V_HOLDING 18&lt;BR /&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;FONT face="Courier New"&gt;const unsigned short PWM_HOLDING&amp;nbsp;=&amp;nbsp;(PWM_MODULUS *&amp;nbsp;V_HOLDING) / 120;&lt;BR /&gt;const unsigned short PWM_PULLIN = (PWM_MODULUS *&amp;nbsp;V_PULLIN) / 120;&lt;BR /&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;FONT face="Courier New"&gt;GLOBALVAR = ANOUTHERVAR / V_HOLDING;&lt;BR /&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;The final statement for either method takes the same number of bus&amp;nbsp;cycles to execute (115 cycles when simulating a HC908 device).&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 May 2007 11:57:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135337#M1701</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-05-31T11:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: compiler math - or type cast 32 bit math to 16 bit constant.</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135338#M1702</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thank you for your help.&lt;BR /&gt;It is much clearer now.&lt;BR /&gt;&lt;BR /&gt;While my example was over simplified I believe you have given me enough information to get this to work. I will be back on this contract next week and will let you know if I have any more issues with this.&lt;BR /&gt;&lt;BR /&gt;Thanx again for your help.&lt;BR /&gt;&lt;BR /&gt;Rick&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 May 2007 20:55:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/compiler-math-or-type-cast-32-bit-math-to-16-bit-constant/m-p/135338#M1702</guid>
      <dc:creator>RShaich</dc:creator>
      <dc:date>2007-05-31T20:55:02Z</dc:date>
    </item>
  </channel>
</rss>

