Saturday, May 30, 2009

LLVM for Beginners (Windows)

Low Level Virtual Machine (LLVM) is, officially, "Compiler Infrastructure". In my own words, it's a platform independent optimizing assembler. That is, you write high level assembly code for a virtual machine, and then LLVM goes through it, optimizes it, and pumps out the native assembly for your target platform (e.g. x86). The best part is that it can do it Just-In-Time (JIT) too!

LLVM and Clang (the llvm c compiler frontend) first gained my interest when I was looking into OpenCL, and heard Apple's technology of choice was LLVM. After that, LLVM replaced GCC for compiling FreeBSD, and more recently, Rapidmind have sung LLVM's praise. Could LLVM be the future for high performance languages? (Goodbye CUDA/Open64?)

So, how do you get started? First, download LLVM, I just grabbed the MinGW builds for Windows. Now, we need a compiler (LLVM is just the 'assembler', Clang is the C front end). LLVM provide an online compiler. Copy past your code in there, for example:

#include
int main() {
printf("hello world\n");
return 0;
}

The online compiler will then spit out the human readable LLVM assembly code, or 'll' code. Copy and paste this to a file, e.g. "hello.ll".
Now we can turn this into a binary bytecode representation using the LLVM assembler, llvm-as:
llvm-as hello.ll
This will generate hello.bc, the byte code version of the file.

We can either run this JIT with lli, (just type lli hello.bc) or create an assembly file with llc the LLVM static compiler. If you wish to compile the program for windows, you need to change the target provided by the online compiler to a windows compatible compiler. Do this by replacing the 'target triple = "i386-pc-linux-gnu"' line in your ll file to 'target triple = "i586-mingw32msvc"'. (Thanks Eli!) Re-run the assmber, Now your ready to compile:
llc hello.bc
Which generates hello.s

At this point, you need to use your platforms compiler, for example MinGW's gcc:
C:\MinGW\bin>gcc hello.s

C:\MinGW\bin>a.exe

hello world


Cool! The LLVM community is very open and friendly so I'm looking foward to trying some nifty things with LLVM!

Crosshatching in VBA for Microsoft Office 2007


The new versions of Microsoft Office don't natively support cross hatching anymore, not in Word or Powerpoint anyway.

So if you want to get lovely crosshatched diagrams and different patterns you can use this script I wrote. Just press view, macro, create a new one, paste in the code, then run it on your selected object. A great way to make venn diagrams with overlapping crosshatched regions in Powerpoint 2007!

Code:
Sub crosshatch()

Dim Sh As Shape
Dim oldFColor As Long

If Not ActiveWindow.Selection.Type = ppSelectionShapes Then
MsgBox "Select something, then try again"
Exit Sub
End If

Set Sh = ActiveWindow.Selection.ShapeRange(1)

strMenu = "Select pattern: 1. Horizontal, 2. Vertical, 3. Upward Diagonal, 4. Downward Diagonal "
rc = InputBox(strMenu, "Menu", 1)
If IsNumeric(rc) Then
Select Case rc
Case 1
p = msoPatternHorizontal
Case 2
p = msoPatternVertical
Case 3
p = msoPatternUpwardDiagonal
Case 4
p = msoPatternDownwardDiagonal
End Select
Else
MsgBox "Invalid Selection"
Exit Sub
End If

With Sh
With .Fill
.Transparency = 0.5
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.BackColor.RGB = RGB(255, 255, 255)
.Patterned p
End With
End With

End Sub

Wednesday, May 27, 2009

Fast division


This is a trick I wish I'd known back in 2000:

Replace:

int exactDiv3(int x) { // given that x is a multiple of 3
return x/3;
}

With:

int exactDiv3(int x) {
return x * 0xaaaaaaab;
}

Wow!

Generate your own optimized magic division numbers!
There is a great blog post detailing how this works here

Wednesday, May 20, 2009

Waterboard


Designer Mike Burton has developed a wall of water, called ‘the Waterboard’. Is is in interactive whiteboard that allows users to manipulate the flow of water. Nifty!

Check out the video on youtube, and some more info here.

Wolfram Alpha

Wolfram Alpha claims to be a computation engine, so it can tell us things about maths, apparently.

I heard it could 'compute' things, based on a huge database it had, so I needed to know the probability of something occuring, I thought I'd try out Wolfram Alpha. Needless to say, it returned nothing. I wasn't really supprised.

I though, ok, thats perhaps a bit obscure, perhaps I'll try a few maths-y terms from computer graphics and see how we go.
spherical harmonics, returned nothing. OK,.. perhaps a bit too obscure? (But Spherical Harmonics are described on their own website..)

How about the bitangent vector.. again, nothing. Wierd, again, there is information on the bitangent vector on the Wolfram website!

Clearly I'm asking for too much, so I tried the simple cross product. OK, I'm pretty sure this was covered in high school..

Exactly how stupid do your questions have to be before Wolfram Alpha gives you some information? dot product? No joy. vector at last! It knows something about maths... .. .

I'm sure it won't be long know before it achieves singularity.

Saturday, May 16, 2009

Zero Moment Point

The Zero tipping Moment Point (ZMP) is useful in determining if a bipedal robot is in a stable configuration or not. It has been used extensively in a number of biped control algorithms, most notably at Honda. The ZMP is the point on the ground where the sum of all the moments of the active forces is equal to zero. It helps to clarify this with equations and diagrams:
Given a robot we can write:
F = m.g - m.a

Where m is the robots mass, g is gravity and a is the acceleration of the center of mass.

The moment on any point is:
Mp = p.COM x m.g - p.COM x m.a - H

Where p is the point in question, COM is the center of mass, and H is the rate of angular momentum at the center of mass.

If we can find a point on the ground that balances the motion of the robot then we have the ZMP.

P.zmp = n x MP / F.n

Where P is the projection of the ankle to the ground, and n is the grounds normal.

For more information refer to Honda's ASIMO history which gives a few diagrams and an overview, and Philippe Sardain and Guy Bessonnet article Forces Acting on a Biped Robot: Center of Pressure, Zero Moment Point.

Friday, May 15, 2009

Very Sleepy Profiler


I stumbled across Richard Mitton's awesome free profiler for MSVC, Very Sleepy.

Install, and compile your program with debug information, run the profiler, select your process and thread, and sample away!

Amazing! The best tool ever, couldn't be easier and does exactly what I want.

As you can see from the screenshot, it will even take you straight to the code you need to spend some time optimizing. Nifty.