<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vincent Gable's Blog &#187; x86</title>
	<atom:link href="http://vgable.com/blog/tag/x86/feed/" rel="self" type="application/rss+xml" />
	<link>http://vgable.com/blog</link>
	<description>my weblog.</description>
	<lastBuildDate>Tue, 29 Nov 2011 22:20:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>What Am I About To Call?</title>
		<link>http://vgable.com/blog/2010/04/29/what-am-i-about-to-call/</link>
		<comments>http://vgable.com/blog/2010/04/29/what-am-i-about-to-call/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 02:40:10 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=597</guid>
		<description><![CDATA[Say you&#8217;re in gdb, and about to execute a call instruction for dyld_stub_objc_msgSend, how do you know what&#8217;s about to happen? On i386 (gdb) x/s *(SEL*)($esp+4) tells you the message that&#8217;s about to be sent. (gdb) po *(id*)$esp tells you the target object that&#8217;s about to get the message.]]></description>
			<content:encoded><![CDATA[<p>Say you&#8217;re in gdb, and about to execute a <code>call</code> instruction for <code>dyld_stub_objc_msgSend</code>, how do you know what&#8217;s about to happen?</p>
<h3>On i386</h3>
<pre>(gdb) x/s *(SEL*)($esp+4)</pre>
<p>tells you the <strong>message</strong> that&#8217;s about to be sent.</p>
<pre>(gdb) po *(id*)$esp</pre>
<p>tells you the <strong>target</strong> object that&#8217;s about to get the message.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2010/04/29/what-am-i-about-to-call/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Just Look at it, Man!</title>
		<link>http://vgable.com/blog/2009/11/11/just-look-at-it-man/</link>
		<comments>http://vgable.com/blog/2009/11/11/just-look-at-it-man/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 07:46:06 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Analysis]]></category>
		<category><![CDATA[Color]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Graphing]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[SSE]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[War Story]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=495</guid>
		<description><![CDATA[You&#8217;re looking at Anscombe&#8217;s quartet: 4 datasets with identical simple statistical properties (mean, variance, correlation, linear regression); but obvious differences when graphed. (via Best of Wikipedia) Graphs aren&#8217;t a substitute for numerical analysis. Graphs are not a panacea. But they&#8217;re excellent for discovering patterns, outliers, and getting intuition about a dataset. If you never graph [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re looking at <a href="http://en.wikipedia.org/wiki/Anscombe%27s_quartet">Anscombe&#8217;s quartet</a>: 4 datasets with identical simple statistical properties (mean, variance, correlation, linear regression); but obvious differences when graphed.<br />
<a href="http://en.wikipedia.org/wiki/File:Anscombe.svg" class="no-border">
<div style="text-align:center;"><img src="http://vgable.com/blog/wp-content/uploads/2009/11/325px-Anscombe.svg.png" alt="325px-Anscombe.svg.png" border="0" width="325" height="222" /></div>
<p> </a><br />
(via <a href="http://bestofwikipedia.tumblr.com/">Best of Wikipedia</a>)</p>
<p>Graphs aren&#8217;t a substitute for numerical analysis. Graphs are not a panacea. But they&#8217;re excellent for discovering patterns, outliers, and getting <em>intuition</em> about a dataset. If you never graph your data, then you&#8217;ve never really <em>looked at it</em>.</p>
<h3>War Story</h3>
<p>I was working on optimizing color correction, using SSE (high performance x86 instructions). One operation required division &#8212; an expensive operation for a computer. The hardware had a <code>divide</code> instruction, but sometimes using the <a href="http://www.sosmath.com/calculus/diff/der07/der07.html">Newton-Raphson method</a> to <a href="http://en.wikipedia.org/wiki/Division_(digital)#Newton.E2.80.93Raphson_division">do the division in software</a> is faster. You never know until you measure.</p>
<p>While doing the measurement, I somehow got the crazy idea to try both: I&#8217;d already unrolled the inner loop so instead of repeating the <code>divide</code> or Newton&#8217;s Method twice, I&#8217;d do a <code>divide</code> and then use Newton&#8217;s Method for the next value. Strangely enough, this was faster on the hardware I was benchmarking than either method individually. Modern hardware is a complex and scary beast.</p>
<p>I was fortunate enough to have a suite of very good unit tests to run against my optimized code. But there was a caveat to testing correctness. Because <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html">computers don&#8217;t have infinitely precise arithmetic</a>, two correct algorithms might give different answers &#8212; but if the numbers they gave were <em>close enough</em> to the infinitely precise answer (say a couple <a href="http://en.wikipedia.org/wiki/Unit_in_the_last_place">ulps</a> apart)  it was good enough. (<a href="http://vgable.com/blog/2009/11/04/tolerance/">We can only be exact within some Tolerance</a>!) The tests cleared my hybrid <code>divide</code>/Newton-Raphson function: <em>but we couldn&#8217;t use it, because it was fundamentally broken</em>.</p>
<p>Even though the error was acceptably small, it had a <em>nasty</em> distribution. Using <code>divide</code> gave color values that were a bit too light. Doing a divide in software gave values that were a bit too dark. Individually these errors were fine. Randomly <a href="http://en.wikipedia.org/wiki/Dither">spread over the image</a> they would have been fine. But processing every other pixel differently had the effect of adding alternating light/dark stripes! <a href="http://www.uiandus.com/2009/06/25/cognitive-science/perception-vs-reality-perception-wins/">We see contrast, not absolute color</a>, so the numerically insignificant error was quite visible. Worse still, bands of 1 pixel stripes combined to form a shimmering <a href="http://en.wikipedia.org/wiki/Moiré_pattern">Moiré pattern</a>. It was totally busted. Unusable.</p>
<p>This was all immediately obvious when the results of the color correction were &#8220;graphed&#8221;. Actually <em>looking</em> at the answer caught a subtle error that our suite of unit tests missed.</p>
<p>To be clear, more subjective graphical analysis is <em>not</em> a substitute for numerical analysis and data mining. But I believe in actually <em>looking at your data</em> at least once. A graph is a kind of <a href="http://vgable.com/blog/2009/03/03/vincents-notes-end-to-end-arguments-in-system-design/">end-to-end</a> visualization of everything, and that has value. Graphs are a cheap sanity check &#8212; does everything look right? And sometimes, they can give you real insight into a problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/11/11/just-look-at-it-man/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Beware rangeOf NSString Operations</title>
		<link>http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/</link>
		<comments>http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 15:42:05 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[NSRange]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Objective-C 2.0]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/</guid>
		<description><![CDATA[I have repeatedly had trouble with the rageOf&#8230; NSString methods, because they return a struct. Going forward I will do more to avoid them, here are some ways I plan to do it. Sending a message that returns a struct to nil can &#8220;return&#8221; undefined values. With small structs like NSRange, you are more likely [...]]]></description>
			<content:encoded><![CDATA[<p>I have repeatedly had trouble with the <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/rangeOfCharacterFromSet:"><code>rageOf</code>&#8230; <code>NSString</code> methods</a>, because they return a <code>struct</code>. Going forward I will do more to avoid them, here are some ways I plan to do it.</p>
<p><a href="http://vgable.com/blog/2008/05/31/messages-to-nowhere/">Sending a message that returns a <code>struct</code> to <code>nil</code> can &#8220;return&#8221; <em>undefined</em> values</a>. With small <code>struct</code>s like <code>NSRange</code>, you are more likely to get <code>{0}</code> on Intel, compared to PowerPC and iPhone/ARM. Unfortunately, this makes <code>nil</code>-messaging bugs hard to detect.  In my experience <strong>you will miss them when running on the simulator, even if they are 100% reproducible on an actual iPhone</strong>.</p>
<p>This category method has helped me avoid using <code>-rangeOfString:</code> dangerously,</p>
<pre>
@implementation NSString  (HasSubstring)
- (BOOL) hasSubstring:(NSString*)substring;
{
	if(<a href="http://vgable.com/blog/2008/12/16/isempty/">IsEmpty</a>(substring))
		return NO;
	NSRange substringRange = [self rangeOfString:substring];
	return substringRange.location != NSNotFound &#038;&#038; substringRange.length &gt; 0;
}
@end
</pre>
<p>I choose to define <code>[aString hasSubstring:@""]</code> as <code>NO</code>. You might prefer to throw an exception, or differentiate between <code>@""</code> and <code>nil</code>. But I don&#8217;t think a <code>nil</code> string is enough error to throw an exception. And even though technically any string contains the empty string, I generally treat <code>@""</code> as semantically equivalent to <code>nil</code>.</p>
<p>As <a href="http://vgable.com/blog/2008/05/31/messages-to-nowhere/">I&#8217;ve said before</a>,</p>
<blockquote><p>A few simple guidelines can help you avoid my misfortune:</p>
<ul>
<li> Be especially careful using of any objective-C method that returns a <code>double, struct,</code> or <code>long long</code>
<li> Don&#8217;t write methods that return a <code>double</code>, <code>struct</code>, or<code>long long</code>.  Return an object instead of a <code>struct</code>; an <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html"><code>NSNumber*</code></a> or <code>float</code> instead of a <code>double</code> or <code>long long</code>.  If you must return a dangerous data type, then see if you can avoid it. There really isn&#8217;t a good reason to return a <code>struct</code>, except for efficiency. And when micro-optimizations like that matter, it makes more sense to write that procedure in straight-C, which avoids the overhead of Objective-C message-passing, and solves the undefined-return-value problem.
<li> But if you absolutely must return a dangerous data type, then return it in a parameter. That way you can give it a default value of your choice, and won&#8217;t have undefined values if an object is unexpectedly <code>nil</code>.<br />
Bad:<br />
<code>- (struct CStructure) evaluateThings;</code><br />
Good:<br />
<code>- (void) resultOfEvaluatingThings:(struct CStructure*)result;</code>.
</ul>
</blockquote>
<p>It&#8217;s not a bad idea to wrap up all the <code>rangeOf</code> methods in functions or categories that play safer with <code>nil</code>.</p>
<p><small>Thanks to <a href="http://www.sealiesoftware.com/">Greg Parker</a> for corrections!</small></p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Printing a FourCharCode</title>
		<link>http://vgable.com/blog/2008/04/23/printing-a-fourcharcode/</link>
		<comments>http://vgable.com/blog/2008/04/23/printing-a-fourcharcode/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 07:21:37 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[big endian]]></category>
		<category><![CDATA[Endian]]></category>
		<category><![CDATA[little endian]]></category>
		<category><![CDATA[PowerPC]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/04/23/printing-a-fourcharcode/</guid>
		<description><![CDATA[A lot of Macintosh APIs take or return a 32-bit value that is supposed to be interpreted as a four character long string. (Anything using one of the types FourCharCode, OSStatus, OSType, ResType, ScriptCode, UInt32, CodecType probably does this.) The idea is that the value 1952999795 tells you less, and is harder to remember, then [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of Macintosh APIs take or return a 32-bit value that is supposed to be interpreted as a four character long string.  (Anything using one of the types <code> FourCharCode, OSStatus, OSType, ResType, ScriptCode, UInt32, CodecType</code> probably does this.)  The idea is that the value <code>1952999795</code> tells you less, and is harder to remember, then <code>'this'</code>.</p>
<p>It&#8217;s a good idea, unfortunately there isn&#8217;t a simple way to print integers as character strings.  So you have to write your own code to print them out correctly, and since it has to print correctly on both big endian and little endian machines this is tricker then it should be.  I&#8217;ve done it incorrectly before, but more importantly so has <em>Apple&#8217;s own sample code</em>!</p>
<p>Here are the best solutions I&#8217;ve found.  Please let me know if you have a better way, or if you find any bugs.  (The code is public-domain, so use it any way you please.)</p>
<p><strong><code>FourCharCode</code> to <code>NSString</code>:</strong><br />
use <code>NSString *NSFileTypeForHFSTypeCode(OSType code);</code><br />
Results are developer-readable and look like: <code>@"'this'"</code>, but Apple says &#8220;The format of the string is a private implementation detail&#8221;, so don&#8217;t write code that depends on the format.</p>
<p><strong><code>FourCharCode</code> to a C-string (<code>char*</code>):</strong><br />
use the C-macro<br /> <br />
<code>#define FourCC2Str(code) (char[5]){(code &gt;&gt; 24) &amp; 0xFF, (code &gt;&gt; 16) &amp; 0xFF, (code &gt;&gt; 8) &amp; 0xFF, code &amp; 0xFF, 0}</code><br />
(requires -std=c99; credit to <a href="http://thirdcog.eu/">Joachim Bengtsson</a>).  Note that the resulting C-string is statically allocated on the stack, not dynamically allocated on the heap; do not return it from a function.<br />
  If you want a value on the heap, try:<br />
<br /><code><br />
void fourByteCodeString (UInt32 code, char* str){<br />
&nbsp;&nbsp;sprintf(str, "'%c%c%c%c'",<br />
&nbsp;&nbsp;&nbsp;&nbsp;(code &gt;&gt; 24) &amp; 0xFF, (code &gt;&gt; 16) &amp; 0xFF,<br />
&nbsp;&nbsp;&nbsp;&nbsp;(code &gt;&gt; 8) &amp; 0xFF, code &amp; 0xFF);<br />
}<br />
</code></p>
<p><strong>In GDB</strong>, you can print a number as characters like so:<br />
<br /><code>(gdb) print/T 1936746868<br />
$4 = 'spit'</code><br />
(<a href="http://www.borkware.com/quickies/one?topic=gdb">via Borkware&#8217;s GDB tips</a>)</p>
<p>If you do end up rolling your own <code>FourCharCode</code> printer (and I don&#8217;t recommend it!), then be sure to use arithmetic/shifting to extract character values from the integer.  You can not rely on the layout of the integer in memory, because it will change on different machines.  Even if you are <em>certain</em> that you&#8217;re code will only run on an x86 machine, it&#8217;s still a bad idea.  It limits the robustness and reusability of your code.  It sets you up for an unexpected bug if you ever do port your code.  And <a href="http://daringfireball.net/2005/05/intelmania">things change unexpectedly</a> in this business.</p>
<p>Printing a <code>FourCharCode</code> is harder then it should be.  Experienced developers who should know better have been bitten by it (and so have I).  The best solution is probably a <code>%format</code> for <code>printf/NSLog</code> that prints integers as character strings.  Unfortunately, it doesn&#8217;t look like we&#8217;ll be seeing that anytime soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/04/23/printing-a-fourcharcode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

