Alignment of double values on ARM architectures

Consider the following code:

class C {
...
uint8_t *data;
size_t size;
...

void writeDouble(double v)
{
...
reinterpret_cast<double*>(this->data)[this->size] = v;
this->size += sizeof(double);
...
}

};

Looks harmless enough, doesn’t it? But here is the problem: It’s not necessarily portable or well behaving code. After debugging for about three hours, I found out something interesting. Namely that on ARM platforms, such as the iPhone, double values on the stack need to be stored at 8 byte aligned memory addresses. This was some existing code I was using, so it took me a while to get to this function and to its problem. Single precision float values have no such restriction, by the way. There are two workarounds in the above case. 1) Write a small for-loop that casts the double value to a uint8_t * and copy the double value byte-wise, or 2) use malloc to copy the value. I did the for-loop, since I thought that maybe the malloc call has too much overhead. I guess you could also cast the double to a uint32_t * pointing to two 32 bit words. Anyway, take care when doing such drastic casts. Some platforms might want alignment for some datatypes!

Valgrind checking of iOS programs

Well, this is awesome news. First, valgrind has been available for OS X for some time now. And second, you can use it to check your iOS programs on the simulator with it.

The idea here is to let your program spawn valgrind itself. Because you cannot tell the simulator to run the program through valgrind. Well, maybe you could build a funky bundle, but I think this works just fine. So here is the  code, taken from the above link:

#define VALGRIND "/usr/local/valgrind/bin/valgrind"

int main(int argc, char *argv[]) {
#ifdef VALGRIND_REXEC
/* Using the valgrind build config, rexec ourself
* in valgrind */
if (argc < 2 || (argc >= 2 && strcmp(argv[1], "-valgrind") != 0)) {
execl(VALGRIND, VALGRIND, "--leak-check=full", "--dsymutil=yes", argv[0], "-valgrind",
NULL);
}
#endif

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"PeepsAppDelegate");
[pool release];
return retVal;
}

You will obviously want to define the VALGRIND_REXEC macro, if you need valgrind output. You can also pass different command line arguments to valgrind. E.g. you can switch to different valgrind tools this way, or pipe everything to a log-file.
Update: I finally got around to trying out this method. One problem here is that valgrind will fail to run, since it tries to open /dev/random, which I guess is not allowed for sandboxed applications. But one can fix this by patching and recompiling valgrind, which is not too hard. Especially when using MacPorts. Furthermore, I needed to add –dsymutil=yes to the valgrind options, or else the program would just crash.

Python class attributes versus instance attributes

Today I finally found out the difference between class and instance attributes in Python. In C++, this is done by putting the static modifier in front of the declaration. Consider the following code:

#!/usr/bin/env python

class B:
b = 2

class C:
a = B()

def __init__(self):
self.a.b = 1

c = C()
c.a.b = 3
b = C()

print c.a.b, b.a.b

Here, a is a class attribute of class C. That is, there exists only one such attribute for all objects of kind C. So the output of the print statement will be “1 1”. This is the same as a static attribute in C++. But often I want an instance attribute. The correct way to do this would have been:

#!/usr/bin/env python

class B:
b = 2

class C:
def __init__(self):
self.a = B()
self.a.b = 1

c = C()
c.a.b = 3
b = C()

print c.a.b, b.a.b

Now the output is “3 1”, just as expected. I guess this all somehow makes sense in the Python world, but I tripped over this, and worst of all: Sometimes you don’t even notice. If the class attribute is a simple type, like int, the first solution would have worked. However, I have not yet understood why that is the case. One more Python semantic that eluded me so far.

Setting the sender of git post-receive hooks

There is a nice stackoverflow posting about how to set the sender of git post-receive hooks. I used this, slightly augmented:

#!/bin/sh

# Use the name and email address of the author of the last commit.
USER_EMAIL=$(git log -1 --format=format:%ae HEAD)
USER_NAME=$(git log -1 --format=format:%an HEAD)
. $(dirname $0)/post-receive-email

I then also changed the default post-receive-email script, to look like this in the send_mail() function:

send_mail()
{
if [ -n "$envelopesender" ]; then
/usr/sbin/sendmail -t -f "$envelopesender"
else
/usr/sbin/sendmail -t -F "$USER_NAME" -f "$USER_EMAIL"
fi
}
This will let the emails come from the last user in the git log. This is only a hack, and might break or not make sense under certain circumstances, but is good enough for most of my needs.

Spherical Harmonics Explorer

I have written a neat little Spherical Harmonics explorer. Right now, the projection function has some weird bug still, but the basis functions, and arbitrary combinations of them can be viewed nicely. The tool is written in Python, using PyQt4 and PyOpenGL. Python is excellent for prototyping such a tool. It might not be as fast as C++, but the rendering speed using PyOpenGL is more than enough and very smooth. I might put the small 3D viewer widget online later, because it might be useful for many people. Here’s some eyecandy:

Update: Now the projection also works fine. See the example screenshot of an order 5 approximation. This still needs some more work, still using a very basic Monte Carlo sampling approach, which converges terribly slow. But I can already produce some results.

Selecting between overloaded signals in PyQt

The signal mechanism in Qt allows for overloading. E.g. the QSpinBox comes with two signals called valueChanged(int) and valueChanged(QString). However, since Python is dynamically typed, such overloading gives rise to problems. Here is how you can select which signal you want to connect to your slot:

from PyQt4 import QtCore, QtGui, uic

class MyMainWindow(QtGui.QMainWindow):

def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = uic.loadUi("MyMainWindow.ui", self)

# Slot connections
self.ui.spinBox.valueChanged.connect(self.spinBoxChanged)
self.ui.spinBox.setValue(4)

@QtCore.pyqtSlot(int)
def spinBoxChanged(self, i):
# do something...
pass

Notice the (int) in the decorator of the slot. This has to match the signature of the signal. I haven’t dug into more complex signals, passing around arbitrary objects, but I think that is not a problem: Qt datatypes have a Python wrapper, and functions in Python cannot be overloaded anyway, so there won’t be clashes with pure Python signals and slots.

Redefining quit-char in Emacs 23

The quit command C-g is pretty much standard in Emacs. It runs the interactive function keyboard-quit and also serves as the default quit-char. It aborts basically every running function, also it allows you to cancel operations in the minibuffer. The Emacs documentation describes current-input-mode, set-input-mode and set-quit-char for getting and setting the quit-char value. Also, global-set-key allows you to rebind keyboard-quit to another character. However, this does not work very well. The quit-char is not settable for non-tty Emacs versions, e.g. on OS X using Cocoa Emacs you get this:

So, basically nothing changes. On a tty it looks more promising:

After rebinding the keyboard quit as well, using (global-set-key (kbd “C-q”) ‘keyboard-quit), not much happens either. It seems the minibuffer assumes C-g to be the choice of the day. Running find-file with C-x C-f or interactively via M-x find-file, and then hitting C-q does nothing except printing “Quit”. Hitting C-g however quits the minibuffer. This seems to be documented in Bug #1218 of Emacs.

Evoke 2010 wrapup

On Friday and Saturday I went to the Evoke 2010 in Cologne together with 0xtob. Exactly 25 hours before the 4k Intro compo deadline we started coding… Well, I haven’t coded an intro ever before, 0xtob has some more experience. Plus the two other, very brilliant entries in the competition had much more time to prepare. But nevertheless we managed to submit an entry for Linux, which took about 3.2KiB.

0xtob even managed to code a minimalistic sound engine in less than an hour, so that we had at least some noise playing in the background. The whole intro is just one scene without any fading in or out so far. It’s basically a raytraced implicit, morphing function (interpolating between two animated implicit functions). So we actually do not render any explicit geometry. The raytracer is written in GLSL, and the textures are a port of Ken Perlin’s original improved Perlin noise to GLSL, which I somehow managed to do. The intro runs on both Linux and OS X, but on OS X it is currently about 7KiB, so not quite 4k. A Windows port will follow at some time, I guess. We are planning to clean up and beautify the intro somewhat and put it on pouet in the coming weeks.
All in all it was a lot of fun, and we got a lot of help: jix borrowed us his MBP running Linux, because the orga did not want to accept OS X as the host OS, and 0xtob’s Quadro NVS was not up to the task rendering the whole thing in 720p. 🙂 Some unknown guy borrowed us the MDP to DVI adapter. Robert helped to crunch the binary to minimal size, and also made the shader more colourful than ever. Roman gave helpful hints and was another pair of eyes watching over the code.