<?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>&#60;&#60; shiftedbits &#187; Programming</title>
	<atom:link href="http://shiftedbits.org/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://shiftedbits.org</link>
	<description>Shifty shifty shifty. Shifty.</description>
	<lastBuildDate>Sun, 29 Jan 2012 23:40:48 +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>When you need std::remove_const</title>
		<link>http://shiftedbits.org/2012/01/29/when-you-need-stdremove_const/</link>
		<comments>http://shiftedbits.org/2012/01/29/when-you-need-stdremove_const/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 01:38:10 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=274</guid>
		<description><![CDATA[I ran across an interesting case where I needed to use std::remove_const to ensure a template parameter wasn&#8217;t const by default. I had something like this: const Matrix&#60;4&#62; m4 = Identity; Vector&#60;3&#62; v3 = project(m4[3]); With the library I was using, TooN 2.0.0 beta8, this resulted in a compile error. The error stated that when the vector returned from project() was being created, the compiler couldn&#8217;t invoke assign to a read only location. A simplified declaration of project() looks as &#8230; <a href="http://shiftedbits.org/2012/01/29/when-you-need-stdremove_const/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I ran across an interesting case where I needed to use <code>std::remove_const</code> to ensure a template parameter wasn&#8217;t const by default. I had something like this:</p>
<p><pre>
const Matrix&lt;4&gt; m4 = Identity;
Vector&lt;3&gt; v3 = project(m4[3]);
</pre></p>
<p>With the library I was using, <a href="http://www.edwardrosten.com/cvd/toon/html-user/index.html">TooN 2.0.0 beta8</a>, this resulted in a compile error. The error stated that when the vector returned from <code>project()</code> was being created, the compiler couldn&#8217;t invoke assign to a read only location. A simplified declaration of <code>project()</code> looks as follows:</p>
<p><pre>
template &lt;int Size, typename Precision, typename Base&gt;
Vector&lt;Size-1,Precision&gt; project(const Vector&lt;Size, Precision, Base&gt;&amp; v);
</pre></p>
<p>What the <code>project()</code> function returns is not important; what is important is the use of the template parameter <code>Precision</code> in both the argument type and the return type. When I invoke <code>operator[]</code> on a const Matrix object, the returned vector&#8217;s precision inherits the const.</p>
<p><pre>
template &lt;int Rows, int Cols, class Precision&gt;
class Matrix {
...
Vector&lt;Cols, const Precision, Slice&gt; Matrix::operator[](int row) const;
};
</pre></p>
<p>When this vector is passed to the <code>project()</code> function above, the function tries to create and return a <code>Vector&lt;3, const double&gt;</code> which can only be initialized with a constexpr. Assigning to it via operator= or a constructor will not work to copy the data from the other vector.</p>
<p>There are several ways one can fix this, all of them involving removing the const qualifier from the Precision type on the returned vector.</p>
<p>It may seem like a simple solution would work:</p>
<p><pre>
template &lt;int Size, typename Precision, typename Base, typename P2&gt;
Vector&lt;Size-1, P2&gt; project(const Vector&lt;Size, Precision, Base&gt;&amp; v);
</pre></p>
<p>Unfortunately, this requires the function be invoked as <code>project&lt;Size, Precision, Base, P2&gt;(v);</code> which is undesirable.</p>
<p><strong>Solution #1</strong></p>
<p>The first solution is simple, but tedious if you have several functions with similar signatures:<br />
<pre>
template &lt;int Size, typename Precision, typename Base&gt;
Vector&lt;Size-1, typename std::remove_const&lt;Precision&gt;::type &gt; 
project(const Vector&lt;Size, Precision, Base&gt;&amp; v);
</pre></p>
<p><code>std::remove_const&lt;T&gt;::type</code> is a type expression equal to the original type <code>T</code>, but with any const qualifiers removed. In this way, <code>const double</code> becomes simply <code>double</code>.</p>
<p><strong>Solution #2</strong></p>
<p>Another solution is to prevent the creation of a Vector object with a const precision, the idea that a non-slice vector with const data isn&#8217;t very useful &#8212; you can&#8217;t even construct one properly. Generally the only time you would actually want a vector with const data is when that data is pointing to some already existing area in memory (a slice).</p>
<p>Consider the following toy code for a vector class:</p>
<p><pre>
#define DECLARE_TYPES(T) \
    typedef typename std::remove_const&lt;T&gt;::type PlainType; \
    typedef const T ConstPlainType; \
    typedef typename std::remove_const&lt;T&gt;::type&amp; ReferenceType; \
    typedef const T&amp; ConstReferenceType

struct StackBase {
template &lt;int Size, typename P&gt;
class Layout { 
public:
    DECLARE_TYPES(P);
    PlainType data[Size];
};
};

struct SliceBase {
template &lt;int Size, typename P&gt;
class Layout {
public:
    P* data;
};
};

template &lt;int Size, typename P = double, typename B = StackBase &gt;
class Vector : public B::template Layout&lt;Size, P&gt; {
public:
    DECLARE_TYPES(P);
    using B::template Layout&lt;Size, P&gt;::data;

    ConstReferenceType operator[](int i) const { return data[i]; }
    ReferenceType operator[](int i) { return data[i]; }
};
</pre></p>
<p>When the Vector object is backed by owned storage (StackBase), the data is declared explicitly to be of a non-const type. Similarly, the return types of operator[] are defined to be the const and non-const reference types. The SliceBase, which handles references to un-owned data, retains the original templated type to allow pointing to constant data.</p>
<p>While this method prevents the creation of Vector objects with const owned data, it also means you don&#8217;t have to augment functions that interface with these objects to correct the return type.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2012/01/29/when-you-need-stdremove_const/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cubic Spline Interpolation</title>
		<link>http://shiftedbits.org/2011/01/30/cubic-spline-interpolation/</link>
		<comments>http://shiftedbits.org/2011/01/30/cubic-spline-interpolation/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 18:11:35 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[interpolation]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[spline]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=191</guid>
		<description><![CDATA[Cubic spline interpolation is a simple way to obtain a smooth curve from a set of discrete points (knots). It has both C1 (first derivative) and C2 (second derivative) continuity, enabling it to produce a continuous piecewise function given a set of data points. From the algorithm detailed here I have implemented a clamped cubic spline class in C++. It is templated on the type of X and Y, allowing for use of scalar or vector types. It requires only &#8230; <a href="http://shiftedbits.org/2011/01/30/cubic-spline-interpolation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Cubic spline interpolation is a simple way to obtain a smooth curve from a set of discrete points (knots). It has both C1 (first derivative) and C2 (second derivative) continuity, enabling it to produce a continuous piecewise function given a set of data points.</p>
<p>From the algorithm detailed <a href="/spline-interpolation">here</a> I have implemented a clamped cubic spline class in C++. It is templated on the type of X and Y, allowing for use of scalar or vector types. It requires only that X and Y define the operators +, -, *, and /, and that Y have a constructor that takes a single scalar argument, initializing all elements to the supplied value. </p>
<p>Usage is very simple. For example, to interpolate a 2D location over time, try this:<br/><br/></p>
<p><pre>
#import &quot;glm.hpp&quot;
#import &lt;vector&gt;

std::vector&lt;float&gt; times;
std::vector&lt;glm::vec2&gt; points;
times.push_back(0);
points.push_back(glm::vec2(0));
times.push_back(.5f);
points.push_back(glm::vec2(-303, -572));
times.push_back(1);
points.push_back(glm::vec2(-250, -980));

/* Create the spline interpolating the position over time */
Spline&lt;float, glm::vec2&gt; sp(times, points);

/* Compute the interpolated position at time 0.75f */
glm::vec2 value(sp[.75f]);
</pre></p>
<p>The code for the spline class is below:</p>
<p><span id="more-191"></span></p>
<p><pre>
/* &quot;THE BEER-WARE LICENSE&quot; (Revision 42): Devin Lane wrote this file. As long as you retain 
 * this notice you can do whatever you want with this stuff. If we meet some day, and you
 * think this stuff is worth it, you can buy me a beer in return. */

#include &lt;vector&gt;
#include &lt;iostream&gt;

/** Templated on type of X, Y. X and Y must have operator +, -, *, /. Y must have defined
 * a constructor that takes a scalar. */
template &lt;typename X, typename Y&gt;
class Spline {
public:
    /** An empty, invalid spline */
    Spline() {}
    
    /** A spline with x and y values */
    Spline(const std::vector&lt;X&gt;&amp; x, const std::vector&lt;Y&gt;&amp; y) {
        if (x.size() != y.size()) {
            std::cerr &lt;&lt; &quot;X and Y must be the same size &quot; &lt;&lt; std::endl;
            return;
        }
        
        if (x.size() &lt; 3) {
            std::cerr &lt;&lt; &quot;Must have at least three points for interpolation&quot; &lt;&lt; std::endl;
            return;
        }
        
        typedef typename std::vector&lt;X&gt;::difference_type size_type;
        
        size_type n = y.size() - 1;
        
        std::vector&lt;Y&gt; b(n), d(n), a(n), c(n+1), l(n+1), u(n+1), z(n+1);
        std::vector&lt;X&gt; h(n+1);

        l[0] = Y(1);
        u[0] = Y(0);
        z[0] = Y(0);
        h[0] = x[1] - x[0];
            
        for (size_type i = 1; i &lt; n; i++) {
            h[i] = x[i+1] - x[i];
            l[i] = Y(2 * (x[i+1] - x[i-1])) - Y(h[i-1]) * u[i-1];
            u[i] = Y(h[i]) / l[i];
            a[i] = (Y(3) / Y(h[i])) * (y[i+1] - y[i]) - (Y(3) / Y(h[i-1])) * (y[i] - y[i-1]);
            z[i] = (a[i] - Y(h[i-1]) * z[i-1]) / l[i];
        }
            
        l[n] = Y(1);
        z[n] = c[n] = Y(0);
        
        for (size_type j = n-1; j &gt;= 0; j--) {
            c[j] = z[j] - u[j] * c[j+1];
            b[j] = (y[j+1] - y[j]) / Y(h[j]) - (Y(h[j]) * (c[j+1] + Y(2) * c[j])) / Y(3);
            d[j] = (c[j+1] - c[j]) / Y(3 * h[j]);
        }
        
        for (size_type i = 0; i &lt; n; i++) {
            mElements.push_back(Element(x[i], y[i], b[i], c[i], d[i]));
        }        
    }
    virtual ~Spline() {}
    
    Y operator[](const X&amp; x) const {
        return interpolate(x);
    }
    
    Y interpolate(const X&amp;x) const {
        if (mElements.size() == 0) return Y();
        
        typename std::vector&lt;element_type&gt;::const_iterator it;
        it = std::lower_bound(mElements.begin(), mElements.end(), element_type(x));
        if (it != mElements.begin()) {
            it--;
        }   
            
        return it-&gt;eval(x);
    }
    
    std::vector&lt;Y&gt; operator[](const std::vector&lt;X&gt;&amp; xx) const {
        return interpolate(xx);
    }
    
    /* Evaluate at multiple locations, assuming xx is sorted ascending */
    std::vector&lt;Y&gt; interpolate(const std::vector&lt;X&gt;&amp; xx) const {
        if (mElements.size() == 0) return std::vector&lt;Y&gt;(xx.size());
        
        typename std::vector&lt;X&gt;::const_iterator it;
        typename std::vector&lt;element_type&gt;::const_iterator it2;
        it2 = mElements.begin();
        std::vector&lt;Y&gt; ys;
        for (it = xx.begin(); it != xx.end(); it++) {
            it2 = std::lower_bound(it2, mElements.end(), element_type(*it));
            if (it2 != mElements.begin()) {
                it2--;
            }
                
            ys.push_back(it2-&gt;eval(*it));
        }

        return ys;
    }

protected:
    
    class Element {
    public:
        Element(X _x) : x(_x) {}
        Element(X _x, Y _a, Y _b, Y _c, Y _d)
        : x(_x), a(_a), b(_b), c(_c), d(_d) {}
        
        Y eval(const X&amp; xx) const {
            X xix(xx - x);
            return a + b * xix + c * (xix * xix) + d * (xix * xix * xix);
        }
        
        bool operator&lt;(const Element&amp; e) const {
            return x &lt; e.x;
        }
        bool operator&lt;(const X&amp; xx) const {
            return x &lt; xx;
        }
        
        X x;
        Y a, b, c, d;
    };
            
    typedef Element element_type;
    std::vector&lt;element_type&gt; mElements;
};
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2011/01/30/cubic-spline-interpolation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Houses in Minecraft</title>
		<link>http://shiftedbits.org/2011/01/02/houses-in-minecraft/</link>
		<comments>http://shiftedbits.org/2011/01/02/houses-in-minecraft/#comments</comments>
		<pubDate>Sun, 02 Jan 2011 04:11:56 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[minecraft]]></category>
		<category><![CDATA[ocd]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=154</guid>
		<description><![CDATA[So I&#8217;ve been playing Minecraft, focusing mainly on the creative aspect, although I play the new Beta client and run my own server locally so I get the features added post-classic. I&#8217;ve turned off monster spawning and set the difficulty to Peaceful, so I can focus on bringing creations to life instead of running from scary monsters in the night. The first time I played with monsters, I found the sound of a zombie&#8217;s &#8220;murrrrrrrr&#8221; in a dark, endless cavern &#8230; <a href="http://shiftedbits.org/2011/01/02/houses-in-minecraft/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been playing <a href="http://minecraft.net">Minecraft</a>, focusing mainly on the creative aspect, although I play the new Beta client and run my own server locally so I get the features added post-classic. I&#8217;ve turned off monster spawning and set the difficulty to Peaceful, so I can focus on bringing creations to life instead of running from scary monsters in the night.</p>
<p>The first time I played with monsters, I found the sound of a zombie&#8217;s &#8220;murrrrrrrr&#8221; in a dark, endless cavern to be frightening enough that I never wanted to hear it again. /shivers.</p>
<p>I find the first-person building experience to be very satisfying to the OCD urge within me that wants to make everything <em>right</em>. As all blocks are destroyable (with the exception of Bedrock), any reality can be created given enough time. I&#8217;m fond of using the different kinds of blocks to vary the aesthetics &#8212; even if the blocks type is not practical.</p>
<p>My favorite activity is to visualize my buildings at night, as I find the lighting of torches, lava, and glowstone to create a very pleasing effect. I also make frequent use of glass to provide a sense of openness and to allow light to mix between areas.</p>
<p>Here&#8217;s some screenshots of a large house on top of a hill that I&#8217;ve been working on.<br />
<div id="attachment_166" class="wp-caption alignnone" style="width: 610px"><a href="http://shiftedbits.org/wp-content/uploads/2011/01/house_masterbed.png"><img src="http://shiftedbits.org/wp-content/uploads/2011/01/house_masterbed-600x357.png" alt="The master bedroom" title="Master bedroom" width="600" height="357" class="size-large wp-image-166" /></a><p class="wp-caption-text">The spacious master bedroom.</p></div></p>
<p><span id="more-154"></span></p>
<div id="attachment_162" class="wp-caption alignnone" style="width: 610px"><a href="http://shiftedbits.org/wp-content/uploads/2011/01/house_kitchen.png"><img src="http://shiftedbits.org/wp-content/uploads/2011/01/house_kitchen-600x357.png" alt="View of the kitchen" title="Kitchen" width="600" height="357" class="size-large wp-image-162" /></a><p class="wp-caption-text">The kitchen has a large worktable, oven/stove, sink, and storage.</p></div>
<div id="attachment_164" class="wp-caption alignnone" style="width: 610px"><a href="http://shiftedbits.org/wp-content/uploads/2011/01/house_sunset.png"><img src="http://shiftedbits.org/wp-content/uploads/2011/01/house_sunset-600x357.png" alt="Sunset seen from one of the western bedrooms" title="Sunset" width="600" height="357" class="size-large wp-image-164" /></a><p class="wp-caption-text">Sunset seen from one of the western bedrooms.</p></div>
<div id="attachment_160" class="wp-caption alignnone" style="width: 610px"><a href="http://shiftedbits.org/wp-content/uploads/2011/01/house_front.png"><img src="http://shiftedbits.org/wp-content/uploads/2011/01/house_front-600x357.png" alt="Front of house." title="House Front" width="600" height="357" class="size-large wp-image-160" /></a><p class="wp-caption-text">Front view of the house, looking down the walkway at the main entrance.</p></div>
<div id="attachment_169" class="wp-caption alignnone" style="width: 610px"><a href="http://shiftedbits.org/wp-content/uploads/2011/01/house_lava_livingroom.png"><img src="http://shiftedbits.org/wp-content/uploads/2011/01/house_lava_livingroom-600x357.png" alt="" title="Lava in the living room" width="600" height="357" class="size-large wp-image-169" /></a><p class="wp-caption-text">Lava provides heat and light to a central table in the living room, <a href='http://en.wikipedia.org/wiki/Kotatsu'>Kotatsu</a> style.</p></div>
<div id="attachment_181" class="wp-caption alignnone" style="width: 610px"><a href="http://shiftedbits.org/wp-content/uploads/2011/01/house_backside.png"><img src="http://shiftedbits.org/wp-content/uploads/2011/01/house_backside-600x357.png" alt="" title="Unfinished backside of the house" width="600" height="357" class="size-large wp-image-181" /></a><p class="wp-caption-text">The back side of the house is still in progress!</p></div>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2011/01/02/houses-in-minecraft/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Units of mach_absolute_time()</title>
		<link>http://shiftedbits.org/2008/10/01/mach_absolute_time-on-the-iphone/</link>
		<comments>http://shiftedbits.org/2008/10/01/mach_absolute_time-on-the-iphone/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 16:22:47 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=17</guid>
		<description><![CDATA[As programmers on OS X are well aware, the units of mach_absolute_time() have been nanoseconds since 10.2, although this was only documented since 10.5. As the iPhone runs a slimmed down version of OS X, I thought it reasonable that the units would remain the same on the device. [Update] As Justin points out below, this was never the case. A complete misread of the documentation on my part. I managed to work on only machines for which the units &#8230; <a href="http://shiftedbits.org/2008/10/01/mach_absolute_time-on-the-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><del datetime="2010-08-20T03:04:47+00:00">As programmers on OS X are well aware, the units of <code>mach_absolute_time()</code> have been nanoseconds since 10.2, although this was only documented since 10.5. As the iPhone runs a slimmed down version of OS X, I thought it reasonable that the units would remain the same on the device.</del></p>
<p><strong>[Update]</strong> As Justin points out below, this was never the case. A complete misread of the documentation on my part. I managed to work on only machines for which the units of <code>mach_absolute_time()</code> were nanoseconds.</p>
<p>So if you&#8217;re trying to get some timing from the iPhone using <code>mach_absolute_time()</code> and seeing times that indicate <strong>faster</strong> results on the iPhone, (not that this happened to me&#8230;), make sure to account for the difference in timebase.</p>
<p>Here&#8217;s the code for conversion between the <code>mach_absolute_time()</code> units and nanoseconds:</p>
<p><pre>
#include &amp;lt;mach/mach_time.h&amp;gt;

/* Get the timebase info */
mach_timebase_info_data_t info;
mach_timebase_info(&amp;info);

uint64_t start = mach_absolute_time();

/* Do some code */

uint64_t duration = mach_absolute_time() - start;

/* Convert to nanoseconds */
duration *= info.numer;
duration /= info.denom;

/* Log the time */
NSLog(@&quot;My amazing code took %lld nanoseconds!&quot;, duration);
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2008/10/01/mach_absolute_time-on-the-iphone/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Key Value Observing Improvements v1.2.2</title>
		<link>http://shiftedbits.org/2008/09/13/key-value-observing-improvements-v122/</link>
		<comments>http://shiftedbits.org/2008/09/13/key-value-observing-improvements-v122/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 23:50:29 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=50</guid>
		<description><![CDATA[Quick bug fix update: 1.2.2 Fixes a crash due to a missing implementation of __KVOAdditions__dealloc__original__ on NSObject. 1.2.1 now runs on the iPhone. The new updates are in SVN at http://svn.shiftedbits.org/public/KVOAdditions/trunk and tagged at http://svn.shiftedbits.org/public/KVOAdditions/tags/1.2.2]]></description>
			<content:encoded><![CDATA[<p>Quick bug fix update:</p>
<p>1.2.2 Fixes a crash due to a missing implementation of __KVOAdditions__dealloc__original__ on NSObject.</p>
<p>1.2.1 now runs on the iPhone.</p>
<p>The new updates are in SVN at <code>http://svn.shiftedbits.org/public/KVOAdditions/trunk</code> and tagged at <code>http://svn.shiftedbits.org/public/KVOAdditions/tags/1.2.2</code></p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2008/09/13/key-value-observing-improvements-v122/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Key Value Observing Improvements v1.2</title>
		<link>http://shiftedbits.org/2008/09/03/key-value-observing-improvements-v12/</link>
		<comments>http://shiftedbits.org/2008/09/03/key-value-observing-improvements-v12/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 05:35:57 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Leopard]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=46</guid>
		<description><![CDATA[Hot on the heels of the 1.1 bug fix release comes version a freshly rewritten 1.2 with API cleanup, bug fixes, and a great new feature. Lately, I&#8217;ve been using observers to allow an object to observe changes to its own properties. This means I don&#8217;t have to override the setter method (I&#8217;m using synthesized properties) and the observation is managed the same way that it would be externally. The current KVO implementation (at least, in non-GC land,) however, doesn&#8217;t &#8230; <a href="http://shiftedbits.org/2008/09/03/key-value-observing-improvements-v12/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hot on the heels of the 1.1 bug fix release comes version a freshly rewritten 1.2 with API cleanup, bug fixes, and a great new feature.</p>
<p>Lately, I&#8217;ve been using observers to allow an object to observe changes to its own properties. This means I don&#8217;t have to override the setter method (I&#8217;m using synthesized properties) and the observation is managed the same way that it would be externally. The current KVO implementation (at least, in non-GC land,) however, doesn&#8217;t allow you to remove observations from yourself in your -dealloc method. This is due to the way KVO works, by interposing a KVODeallocate function before your -dealloc method. It expects that all observers of the object have been removed at this point, and warns if they are not. This is, of course, blindingly annoying &#8212; you now have to create a method that gets called on your objects by their owner before they dealloc so that they can remove their observers.</p>
<p>To fix this, I&#8217;ve done a little interposing of my own, putting in another dealloc method before KVODealloc that will remove self observers and call a -KVODealloc method on the deallocating observer object before KVODeallocate is called. If you don&#8217;t want automatic removal, simply return NO from +automaticallyRemoveSelfObservations on the class of your choice.</p>
<p>The new updates are in SVN at <code>http://svn.shiftedbits.org/public/KVOAdditions/trunk</code> and tagged at <code>http://svn.shiftedbits.org/public/KVOAdditions/tags/1.2</code><br />
<span id="more-46"></span><br />
Version 1.2 also adds new methods for removing an observer that include a selector parameter.</p>
<p><code>NSObject:</code></p>
<p><pre>- (void)removeObserver:(NSObject *)observer 
            forKeyPath:(NSString *)keyPath 
              selector:(SEL)selector;
</pre></p>
<p><code>NSArray:</code></p>
<p><pre>- (void)removeObserver:(NSObject *)observer
  fromObjectsAtIndexes:(NSIndexSet *)indexes
            forKeyPath:(NSString *)keyPath 
              selector:(SEL)selector;
</pre></p>
<p>The selector is now considered a unique part of the observation, and so a class can observe a single property on a single object using multiple selectors. This allows a class and its subclass to avoid collisions when observing a single object.</p>
<p>The <code>-removeObserver:forKeyPath:</code> method now no longer removes selector-based observers.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2008/09/03/key-value-observing-improvements-v12/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Key Value Observing Improvements</title>
		<link>http://shiftedbits.org/2008/07/24/key-value-observing-improvements/</link>
		<comments>http://shiftedbits.org/2008/07/24/key-value-observing-improvements/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 08:26:28 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=23</guid>
		<description><![CDATA[Key value observing is quite a useful tool, no doubt about it. But it has a singularly annoying manner of informing the observer of a change. The -[NSObject observeValueForKeyPath:ofObject:change:context:] method is sent to the observer when a change occurs. It&#8217;s up to the implementor to parse the `change&#8217; dictionary to figure out what changed. When I use observers, I usually want a method called on my class when a change occurs, similar to target/action with UI elements. So my -[NSObject &#8230; <a href="http://shiftedbits.org/2008/07/24/key-value-observing-improvements/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Key value observing is quite a useful tool, no doubt about it. But it has a singularly annoying manner of informing the observer of a change. The <code>-[NSObject observeValueForKeyPath:ofObject:change:context:]</code> method is sent to the observer when a change occurs. It&#8217;s up to the implementor to parse the `change&#8217; dictionary to figure out what changed.</p>
<p>When I use observers, I usually want a method called on my class when a change occurs, similar to target/action with UI elements. So my <code>-[NSObject observeValueForKeyPath:ofObject:change:context:]</code> is basically a set of if&#8217;s for each key I&#8217;m observing that calls the appropriate method.</p>
<p>This turns out to be pretty nasty, especially when you have multiple classes in a hierarchy implementing the method. It wasn&#8217;t obvious to me that I needed to call super in this method. Thirty minutes of debugging later, and I was convinced there had to be a better way of doing it.</p>
<p>To this effort, I present two new APIs. To NSObject, I add <code>-[NSObject addObserver:forKeyPath:options:selector:]</code> and to NSArray, <code>-[NSArray addObserver:toObjectsAtIndexes:forKeyPath:options:selector]</code>. The code is <a href="/static/KVOAdditions.zip">available here</a>, or keep reading for the details.</p>
<p><strong>Update 1.1: Two critical bug fixes</strong>. Please update to the new version if you have the old one. Also, anonymous svn is now available if you want to use an external. http://svn.shiftedbits.org/public/KVOAdditions/trunk.<span id="more-23"></span></p>
<p><code>NSObject:</code></p>
<p><pre>@interface NSObject (KVOAdditions)

- (void)addObserver:(NSObject *)observer
         forKeyPath:(NSString *)keyPath
            options:(NSKeyValueObservingOptions)options
           selector:(SEL)aSelector;

@end
</pre></p>
<p><code>NSArray:</code></p>
<p><pre>@interface NSArray (KVOAdditions)

- (void)addObserver:(NSObject *)observer
 toObjectsAtIndexes:(NSIndexSet *)indexes
         forKeyPath:(NSString *)keyPath
            options:(NSKeyValueObservingOptions)options
           selector:(SEL)aSelector;

@end
</pre></p>
<p>These allow you to observe a keyPath on an object and receive a message to the designated selector when the change occurs. Furthermore, the selector can have four different signatures that allow for different levels of information to be received. These selector formats are as follows:</p>
<p><pre>/* Receive no information about the change */
- (void)valueDidChange;

/* Receive the raw change dictionary */
- (void)valueDidChange:(NSDictionary *)change;

/* If the observing options include either
 * NSKeyValueObservingOptionOld or NSKeyValueObservingOptionNew,
 * receive the old and new values */
- (void)valueDidChange:(id)old newValue:(id)new;

/* If the observing options include either
 * NSKeyValueObservingOptionOld or NSKeyValueObservingOptionNew,
 * receive the old and new values */
- (void)valueDidChange:(id)old newValue:(id)new prior:(BOOL)isPrior;
</pre></p>
<p>Feedback on these APIs, including suggestions and criticism (and even bugs!) are of course welcome. </p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2008/07/24/key-value-observing-improvements/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SSE Optimized Compositing</title>
		<link>http://shiftedbits.org/2008/01/29/sse-optimized-compositing/</link>
		<comments>http://shiftedbits.org/2008/01/29/sse-optimized-compositing/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 06:08:46 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/2008/01/29/sse-optimized-compositing/</guid>
		<description><![CDATA[As part of a graphics API I&#8217;ve been working on (for my own use, it&#8217;s hardly ready for production,) I decided to try learning SSE optimization by making the compositing routine faster. I came up with an implementation which, according to my tests, is 3-5X faster than the non-optimized version. The optimized version below composites a single color starting at a destination pixel buffer for a specified run of pixels. It uses source over compositing on a RGBA, 8bpc, integer &#8230; <a href="http://shiftedbits.org/2008/01/29/sse-optimized-compositing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As part of a graphics API I&#8217;ve been working on (for my own use, it&#8217;s hardly ready for production,) I decided to try learning SSE optimization by making the compositing routine faster.</p>
<p>I came up with an implementation which, according to my tests, is 3-5X faster than the non-optimized version. The optimized version below composites a single color starting at a destination pixel buffer for a specified run of pixels. It uses source over compositing on a RGBA, 8bpc, integer pixel buffer.</p>
<p>The optimized version is presented below:</p>
<p><span id="more-14"></span></p>
<p><pre>
static __m128i zero = 
        _mm_set_epi32(0x0, 0x0, 0x0, 0x0);
static __m128i one = 
        _mm_set_epi32(0x00010001U, 0x00010001U, 0x00010001U, 0x00010001U);
static __m128i mask1 = 
        _mm_set_epi32(0x00FF00FFU, 0x00FF00FFU, 0x00FF00FFU, 0x00FF00FFU);
static __m128i mask2 = 
        _mm_set_epi32(0xFF00FF00U, 0xFF00FF00U, 0xFF00FF00U, 0xFF00FF00U);

if (_comp[3] == 255) {
    /* We&#039;re compositing a fully opaque pixel, just copy onto the destination */
    
    unsigned int *dst = (unsigned int *)_dst;
    
    /* The pixel offset from dst */
    size_t i = 0;
    
    /* The number of quad-pixels processed */
    size_t j = 0;
    
    /* Leading 0..3 pixels */
    while (((intptr_t)(dst+i) &amp; (intptr_t)0xF) != 0) {
        *(dst + i) = *(unsigned int *)_comp;
        i++; run--;
    }
    
    /* Start at the pixel i */
    __m128i *mdst = (__m128i *)(dst + i);
    
    /* Set 4 pixel chunks */
    for (j = 0; j &lt; (run&gt;&gt;2); j++) {
        _mm_prefetch(mdst+1, _MM_HINT_T0);
        _mm_store_si128(mdst++, _src);
    }
    
    /* If we couldn&#039;t get all of the run in 4 pixel chunks, get them now */
    for (size_t k = 0; k &lt; run % 4; k++) {
        *(dst + i + k + (j&lt;&amp;lt;2)) = *(unsigned int *)_comp;
    }
} else {
    /* We need full composition as this pixel has transparency */
    
    unsigned char *dst = (unsigned char *)_dst;
    
    /* char offset from dst */
    size_t i = 0;
    
    /* Quad-pixels processed */
    size_t j = 0;
    
    /* get the non-aligned starting 0..3 pixels */
    while (((intptr_t)(dst+i) &amp; (intptr_t)0xF) != 0) {
        dst[0+i] = _comp[0] + dst[0+i] - 
            (dst[0+i] &gt; 0? ((((unsigned short)dst[0+i] * _comp[3]) &gt;&gt; 8) + 1) : 0); //R
        dst[1+i] = _comp[1] + dst[1+i] - 
            (dst[1+i] &gt; 0? ((((unsigned short)dst[1+i] * _comp[3]) &gt;&gt; 8) + 1) : 0); //G
        dst[2+i] = _comp[2] + dst[2+i] - 
            (dst[2+i] &gt; 0? ((((unsigned short)dst[2+i] * _comp[3]) &gt;&gt; 8) + 1) : 0); //B
        dst[3+i] = _comp[3] + dst[3+i] - 
            (dst[3+i] &gt; 0? ((((unsigned short)dst[3+i] * _comp[3]) &gt;&gt; 8) + 1) : 0); //A
        i += 4; run--;
    }
    
    /* Load in 2 pixels */
    __m128i *mdst = (__m128i *)(dst+i);
    __m128i d, d1, d2, res;
    for (j; j &lt; (run&gt;&gt;2); j++) {
        /* Load 4 pixels */
        d = _mm_load_si128(mdst);
        
        /* Take chars 0,2,4,6,8,10,12,14 and composite */
        d1 = _mm_and_si128(d, mask1);
        d1 = _mm_add_epi16(_mm_subs_epi16(d1, _mm_add_epi16(
                    _mm_srli_epi16(_mm_mullo_epi16(d1, _c3), 8), 
                    _mm_and_si128(_mm_cmpgt_epi16(d1, zero), one))), _ce);
        
        /* Take chars 1,3,5,7,9,11,13,15 and composite */
        d2 = _mm_srli_epi16(_mm_and_si128(d, mask2), 8);
        
        /* prefetch the next 4 pixels */
        _mm_prefetch(mdst+1, _MM_HINT_T0);
        
        d2 = _mm_add_epi16(_mm_subs_epi16(d2, _mm_add_epi16(
                    _mm_srli_epi16(_mm_mullo_epi16(d2, _c3), 8),
                    _mm_and_si128(_mm_cmpgt_epi16(d2, zero), one))), _co);
        
        /* shift odd chars (d2) to high 8 bits and or with d1 */
        res = _mm_or_si128(d1, _mm_slli_epi16(d2, 8));
        
        /* Store 4 pixels */
        _mm_store_si128(mdst, res);
        
        mdst++;
    }
    
    // Get the trailing 0..3 pixels
    dst += i + (j&lt; &amp;lt;4);
    for (size_t k = 0; k &lt; (run % 4) * 4; k+=4) {
        dst[0+k] = _comp[0] + dst[0+k] - 
            (dst[0+k] &gt; 0? ((((unsigned short)dst[0+k] * _comp[3]) &gt;&gt; 8) + 1) : 0); //R
        dst[1+k] = _comp[1] + dst[1+k] - 
            (dst[1+k] &gt; 0? ((((unsigned short)dst[1+k] * _comp[3]) &gt;&gt; 8) + 1) : 0); //G
        dst[2+k] = _comp[2] + dst[2+k] - 
            (dst[2+k] &gt; 0? ((((unsigned short)dst[2+k] * _comp[3]) &gt;&gt; 8) + 1) : 0); //B
        dst[3+k] = _comp[3] + dst[3+k] - 
            (dst[3+k] &gt; 0? ((((unsigned short)dst[3+k] * _comp[3]) &gt;&gt; 8) + 1) : 0); //A
    }
}
</pre></p>
<p>_c3 is a vector composed solely of the alpha value of the color.<br />
_ce is a vector composed of pairs of the red and blue components.<br />
_co is a vector composed of pairs of the green and alpha components.<br />
_src is the start address of the pixel buffer.</p>
<p>_c3, _ce, _co, and _src are set from the following function:</p>
<p><pre>
unsigned short r = _comp[0], g = _comp[1], b = _comp[2], a = _comp[3];
_c3 = _mm_setr_epi16(a, a, a, a, a, a, a, a);
_ce = _mm_setr_epi16(r, b, r, b, r, b, r, b);
_co = _mm_setr_epi16(g, a, g, a, g, a, g, a);
      
_src = _mm_set_epi32(*(int*)_comp, *(int*)_comp, *(int*)_comp, *(int*)_comp);
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2008/01/29/sse-optimized-compositing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transparency Layer Slowdowns</title>
		<link>http://shiftedbits.org/2008/01/22/transparency-layer-slowdowns/</link>
		<comments>http://shiftedbits.org/2008/01/22/transparency-layer-slowdowns/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 19:38:00 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/2008/01/22/transparency-layer-slowdowns/</guid>
		<description><![CDATA[As the documentation mentions, a transparency layer allows subsequent drawing to be rendered in a separate, fully transparent buffer before being composited to the destination context. In the absence of a clipping region, this buffer is the same size as the destination context, requiring a context-sized buffer regardless of the actual drawing bounds. Creating a transparency layer for a small section of content, then drawing this layer in a window, for example, results in a window-sized buffer for the layer. &#8230; <a href="http://shiftedbits.org/2008/01/22/transparency-layer-slowdowns/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As the documentation mentions, a transparency layer allows subsequent drawing to be rendered in a separate, fully transparent buffer before being composited to the destination context. In the absence of a clipping region, this buffer is the same size as the destination context, requiring a context-sized buffer regardless of the actual drawing bounds. Creating a transparency layer for a small section of content, then drawing this layer in a window, for example, results in a window-sized buffer for the layer. When the layer is composited into the destination, the entire buffer must be composited &#8212; CoreGraphics doesn&#8217;t keep track of where the actual drawing is.</p>
<p>This, of course, is slow, especially for several transparency layers.</p>
<p>To help speed things up, clip the context to the bounds of any drawing in a transparency layer before beginning the transparency layer, such as below:</p>
<p><pre>CGRect artRect = CGRectMake(50,50,100,50);
CGContextClipToRect(context, artRect);

CGContextBeginTransparencyLayer(context, NULL);

/* Draw layer content here */

CGContextEndTransparencyLayer(context);
</pre></p>
<p>I&#8217;ve created a <a href="/static/transparency_layer.zip">demonstration program</a> to illustrate the effects of clipped vs. unclipped transparency layers. To see the effects, open Quartz Debug (/Developer/Applications/Graphics Tools/Quartz Debug.app) and turn on &#8220;Flash screen updates.&#8221; Then, open the Transparency Layer program. There are two windows, the left showing the drawing without clipping and the right clipping to the approximate art bounds. The views are redrawing four times per second. Notice that the unclipped view redraws its entire content while the clipped view redraws only the actual art.</p>
<p>Finally, using <code>-[NSView setNeedsDisplayInRect:]</code> clips the drawing to the specified rect.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2008/01/22/transparency-layer-slowdowns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leopard CGWindowList* APIs</title>
		<link>http://shiftedbits.org/2007/11/08/leopard-cgwindowlist-apis/</link>
		<comments>http://shiftedbits.org/2007/11/08/leopard-cgwindowlist-apis/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 05:07:39 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Leopard]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/2007/11/08/leopard-cgwindowlist-apis/</guid>
		<description><![CDATA[Leopard brings new APIs at the CoreGraphics layer to gather information about windows on the system. These functions let you find windows relative to a known window, find all windows on the system, including those offscreen, and obtain images of one or more windows as composited by the Window Server. However, these API&#8217;s have an interesting peculiarity: when the documentation says &#8220;a CFArray of CGWindowID values&#8221; they mean a CFArray of uint32s, not CFNumbers as programmers familiar with CoreFoundation might &#8230; <a href="http://shiftedbits.org/2007/11/08/leopard-cgwindowlist-apis/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Leopard brings new APIs at the CoreGraphics layer to gather information about windows on the system. These functions let you find windows relative to a known window, find all windows on the system, including those offscreen, and obtain images of one or more windows as composited by the Window Server.</p>
<p>However, these API&#8217;s have an interesting peculiarity: when the documentation says &#8220;a CFArray of CGWindowID values&#8221; they mean a CFArray of uint32s, not CFNumbers as programmers familiar with CoreFoundation might expect. This holds true for the <code>CGWindowListCreate()</code>, <code>CGWindowListCreateDescriptionFromArray()</code>, and <code>CGWindowListCreateImageFromArray</code> functions, but the two functions that return window information (<code>CGWindowListCopyWindowInfo()</code> and <code>CGWindowListCreateDescriptionFromArray()</code>) wrap all numeric values in CFNumbers.</p>
<p>To create such an array, you&#8217;ll have to create it with CFArrayCreateMutable(), such as the following:<br />
<pre>/* Make an array with no callbacks */
CFMutableArrayRef windows = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);

/* myWindow is a NSWindow */
CFArrayAppendValue(windows, (void *)[myWindow windowNumber]);
</pre></p>
<p>To get the <code>CGWindowID</code> of a <code>NSWindow</code>, you&#8217;ll want to use <code>-[NSWindow windowNumber]</code>. Even though the documentation specifies that this isn&#8217;t the same number as assigned by the WindowServer, it turns out that it works fine with the CGWindow* APIs.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2007/11/08/leopard-cgwindowlist-apis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.311 seconds -->

