<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Math Features</title>
		<link>http://forums.leaflabs.com/topic.php?id=351</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:18:38 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=351" rel="self" type="application/rss+xml" />

		<item>
			<title>snigelen on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=4#post-2637</link>
			<pubDate>Tue, 16 Nov 2010 15:54:57 +0000</pubDate>
			<dc:creator>snigelen</dc:creator>
			<guid isPermaLink="false">2637@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks again mbolivar! You're very helpful.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2636</link>
			<pubDate>Tue, 16 Nov 2010 15:51:55 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">2636@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;mbolivar &#38;lt;&#38;gt;... if you modify syscalls.c's implementation of putch to do what you want, then printf should Just Work&#38;lt;/&#38;gt;&#60;/p&#62;
&#60;p&#62;Isn't that how operating systems get written?&#60;br /&#62;
They are their to make &#60;code&#62;int main() { printf(&#38;quot;Hello World!\n&#38;quot;); }&#60;/code&#62; work :-)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2622</link>
			<pubDate>Tue, 16 Nov 2010 14:46:07 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">2622@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;printf update: the word from perry is that if you modify syscalls.c's implementation of putch to do what you want, then printf should Just Work:&#60;br /&#62;
&#60;a href=&#34;https://github.com/leaflabs/libmaple/blob/master/libmaple/syscalls.c#L94&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/libmaple/blob/master/libmaple/syscalls.c#L94&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>larryang on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2620</link>
			<pubDate>Tue, 16 Nov 2010 14:36:03 +0000</pubDate>
			<dc:creator>larryang</dc:creator>
			<guid isPermaLink="false">2620@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Complimentary code inspection is what we have here.&#60;/p&#62;
&#60;p&#62;You guys are right about the volatile; it's a lazy way of fixing the issue.  The problem is a side effect of taking advantage of the C &#34;feature&#34; of conflating arrays and pointers, plus knowledge of the bit width of the underlying data types.  It's a lot cleaner, conceptually, to use union to &#34;unpack&#34; the data.  &#60;/p&#62;
&#60;p&#62;It also just struck me that the exact bit width is specified for the loop variables (i,j), but not for the values (t,b) that will be sent externally.  The opposite is more desirable.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2615</link>
			<pubDate>Tue, 16 Nov 2010 06:45:28 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">2615@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;snigelen: entered a ticket for the uint64 issue:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://code.google.com/p/leaflabs/issues/detail?id=43&#34; rel=&#34;nofollow&#34;&#62;http://code.google.com/p/leaflabs/issues/detail?id=43&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2584</link>
			<pubDate>Mon, 15 Nov 2010 18:26:30 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">2584@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;sniglen - you beat me to it. &#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;You're not supposed to throw in a volatile here and there just in case.&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;I agree.&#60;/p&#62;
&#60;p&#62;You probably don't need any support, but ... :-)&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;The volatile keyword is your friend. Basically, if any value can be changed in another thread/process/task, via an interrupt, is a memory mapped peripheral, etc., use volatile.&#60;/p&#62;&#60;/blockquote&#62;
&#60;p&#62;In those cases I completely agree, but this is not one of those case. &#60;/p&#62;
&#60;p&#62;The fact that volatile may help doesn't feel like enough reason to do it that way.&#60;br /&#62;
After all calling an external function like &#60;code&#62;dubious(&#38;amp;t);&#60;/code&#62; might also have the side effect of causing the compiler to update t, but that isn't a good fix.&#60;/p&#62;
&#60;p&#62;union is intended to support punning of memory (though it can be messy, with big endian and little endian being mixable on some ARM architectures).&#60;/p&#62;
&#60;p&#62;Also using volatile should cause a bunch of optimisations to be switched off for t and b which could still be applied for the union, and IMHO, that seems like a helpful test of 'what is right'. The compiler might use registers for the union, and maybe even do some loop unrolling on b[...]. The compiler's 'understanding' of union is different from the use of volatile.&#60;/p&#62;
&#60;p&#62;IMHO replacing the 'broken code' with a union makes the programmer's intent much clearer to programmers and the compiler than using volatile to make 'broken code' work.&#60;/p&#62;
&#60;p&#62;(sniglen - the 'broken code' was just an &#34;off few minutes&#34;, I mean no disrespect :-)&#60;/p&#62;
&#60;p&#62;My $0.02
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Silntknight on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2578</link>
			<pubDate>Mon, 15 Nov 2010 17:04:08 +0000</pubDate>
			<dc:creator>Silntknight</dc:creator>
			<guid isPermaLink="false">2578@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Good call Snigelen, but go ahead and use this thread if you'd all like. For future readers looking for this kind of discussion (can't think of anyone actually) you might want to move it so that the title makes it a little more obvious.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>snigelen on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2577</link>
			<pubDate>Mon, 15 Nov 2010 17:01:56 +0000</pubDate>
			<dc:creator>snigelen</dc:creator>
			<guid isPermaLink="false">2577@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Well the doubts Silntknight had at the beginning of this thread are probably sorted out by now, so this is kind of getting of topic. Anyway.&#60;/p&#62;
&#60;p&#62;I understand what you are talking about, but I can't see how it's (more than remotely) related to this thread.&#60;/p&#62;
&#60;p&#62;Isn't volatile supposed to be used to tell the compiler that a variable can be changed at any time (by hardware or an interrupt routine for example)? It isn't anything like that in this thread.&#60;/p&#62;
&#60;p&#62;In this case there was a variable (t) that was read through a pointer (b) and the optimizer could have missed that and thrown out the calculation of t. But in this case it didn't. But if it had, one could probably prevent that by putting a volatile in front of t. But that's not what volatile is for. You're not supposed to throw in a volatile here and there just in case. So the &#34;hidden pointer&#34; approach is not the way to go.&#60;/p&#62;
&#60;p&#62;So the correct way to do it (in this case), is to tell the compiler that I want to use this variable in two ways, one for reading and one for writing, and that's what a union is for and no volatile is needed.&#60;/p&#62;
&#60;p&#62;(as I understand it, or IMHO, or something like that)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2546</link>
			<pubDate>Mon, 15 Nov 2010 15:07:11 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">2546@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Definitely true. I hit this bug all the time. Best to use volatile for any registers, or really anything thats not plain old RAM/local variables - unless youre using DMA, in which case your buffer should be declared volatile as well.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>larryang on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2541</link>
			<pubDate>Mon, 15 Nov 2010 11:30:54 +0000</pubDate>
			<dc:creator>larryang</dc:creator>
			<guid isPermaLink="false">2541@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;A few C programming tips on the code:&#60;/p&#62;
&#60;p&#62;The volatile keyword is your friend.  Basically, if any value can be changed in another thread/process/task, via an interrupt, is a memory mapped peripheral, etc., use volatile.  &#60;/p&#62;
&#60;p&#62;In the &#34;for&#34; loops, the loop variable should be native data type (int or unsigned int), then cast it to the appropriate type, checking for overflows if appropiate.  I don't know much about the compiler, so it may not make much of a difference.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2540</link>
			<pubDate>Mon, 15 Nov 2010 09:41:07 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">2540@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;by the way, perry says printf is available through newlib. don't know what stdout points to by default, will update this thread later on that.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2539</link>
			<pubDate>Mon, 15 Nov 2010 09:35:26 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">2539@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;will look into the uint64 situation; thanks for the report. &#60;/p&#62;
&#60;p&#62;as to SerialUSB.print((double)12, 5), there's no Print method with arguments double and int; there's only USBSerial::print(long n, int base); inherited from Print (see Print.cpp):&#60;br /&#62;
&#60;a href=&#34;https://github.com/leaflabs/libmaple/blob/master/wirish/Print.cpp#L72&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/libmaple/blob/master/wirish/Print.cpp#L72&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;so that double is getting converted to a long, then printed in base 5.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>snigelen on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2538</link>
			<pubDate>Mon, 15 Nov 2010 06:32:33 +0000</pubDate>
			<dc:creator>snigelen</dc:creator>
			<guid isPermaLink="false">2538@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for the explanation mbolivar.&#60;/p&#62;
&#60;p&#62;I know the standard way is to use a union, I don't know why I did as I did. It worked anyway.&#60;br /&#62;
But I discovered some other things. For example&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;union {
    double t;
    uint64 b;
  } u;

  u.t = ...;
  SerialUSB.println(u.b, HEX);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;prints only the lower 32 bits. But&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;union {
    double t;
    uint32 b[2];
  } u;
  u.t = ...;
  SerialUSB.print(u.b[1], HEX);
  SerialUSB.print(u.b[0], HEX);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;works as expected.&#60;/p&#62;
&#60;p&#62;Another thing: &#34;SerialUSB.print((double) 12);&#34;&#60;br /&#62;
prints 12.00 as expected, but I thought that &#34;SerialUSB.print((double) 12, 5);&#34; would print 12.00000, but instead it prints 22 (12 in base 5).
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2529</link>
			<pubDate>Sun, 14 Nov 2010 23:36:32 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">2529@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;snigelen -- thanks for looking that up.&#60;/p&#62;
&#60;p&#62;by the way, while it seems like your code worked, you should know that the default optimization flags we use mean that your code (reproduced below) might cause the writes to &#34;t&#34; to get optimized out:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;void loop() {
      double t;
      unsigned char *b;

      // Let b point to the bytes in t
      b = (unsigned char*)&#38;amp;t;

      if(SerialUSB.available()) {
        SerialUSB.read();
        for (uint16 i = 0; i &#38;lt; 256; i++) {
          t = sin(2*M_PI/255.0 * (double)i);

          // print t in hexadecimal form
          for (int8 j = 7; j &#38;gt;= 0; j--) {
            if(b[j] &#38;lt; 16)
              SerialUSB.print(&#38;#39;0&#38;#39;);
            SerialUSB.print(b[j],HEX);
          }
          SerialUSB.println();
        }
      }
    }&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;in particular, we use -Os to suggest to GCC to optimize for size. this enables -fstrict-aliasing:&#60;br /&#62;
&#60;a href=&#34;http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Optimize-Options.html#index-fstrict_002daliasing-542&#34; rel=&#34;nofollow&#34;&#62;http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Optimize-Options.html#index-fstrict_002daliasing-542&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;-fstrict-aliasing tells the compiler that the same address won't hold objects of two different types.  so since t is a double and b is a pointer to unsigned char, it's possible that GCC might decide that since the variable t is only written to, and never read from, it could emit smaller code by removing the line where t = sin(...).&#60;/p&#62;
&#60;p&#62;the standard &#34;type punning&#34; trick to get at the bits of a non-integral value is to use a union. for example, since &#34;unsigned long&#34; and &#34;double&#34; both take 8 bytes on the Maple, you could say&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;union ul_d_t {
        unsigned long bits;
        double d;
    };

    ul_d_t u;
    u.d = sin(...);
    SerialUSB.println(u.bits, HEX);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;as a special case, GCC permits this &#34;type punning&#34; trick, even when -fstrict-aliasing is enabled.&#60;/p&#62;
&#60;p&#62;edit:formatting
&#60;/p&#62;</description>
		</item>
		<item>
			<title>larryang on "Math Features"</title>
			<link>http://forums.leaflabs.com/topic.php?id=351&amp;page=3#post-2522</link>
			<pubDate>Sun, 14 Nov 2010 18:31:08 +0000</pubDate>
			<dc:creator>larryang</dc:creator>
			<guid isPermaLink="false">2522@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I learned a lot.  Now, only if I can convince more people to do fixed point...
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
