About The Contest
The first ever Olympiad of Misguided Geeks contest at Worse Than Failure (or OMGWTF for short) is a new kind of programming contest. Readers are invited to be creative with devising a calculator with the craziest code they can write. One lucky and potentially insane winner will get either a brand new MacBook Pro or comparable Sony VAIO laptop.
Entry #100190: Don't Trust The Hardware
by Jeffery C. May
As a professional electrical engineer (Florida PE License number 64025), I'm familiar with how computers work at the hardware level-- so this entry performs all its arithmetic using logical operators and bitshifts, just like the hardware underneath the (buggy-- think of the Pentium FDIV) +, -, * and / CPU instructions. This would have been a natural to do in VHDL (something I'm aspiring to learn) or Verilog (with its C-like syntax), but the rules stipulated C or C++, so used ANSI C (I never learned C++).
Just like old-school processors, I built a 32-bit ripple adder out of four eight-bit (unsigned byte) adders, each bit of which was a full-adder constructed from two half-adders.
With that adder alone I can do addition and two's compliment subtraction.
I think my most clever part is how I handle division- The DivideLong.cpp file specifically. It doesn't really work for general-purpose division, but for 24-bit floating point mantissas it seems to be pretty speedy. It's a divide-by-subtraction loop (not a repetative subtraction, however), but only requires 24 loops at most-- usually much less. I'm sure I didn't invent this technique, but I've not seen it before in my (limited) research.
A lot of the WTFs in my code end up being style inconsistancies. I have a function that finds the two's compliment of a 32-bit number (TwosCompliment.cpp), but I only some times use it. Other times I use the "~" bit-flip operator, and then set the Carry bit to "1" to make my Add*.cpp functions perform subtraction.
I also abuse the compiler somewhat, having it compute two's compliment negative values for me and assigning the result to unsigned integers.
What WTF program would be complete without dead code. Got that, too.
I considered having a "Brilant" function, but figured that enough people were going to do that that *not* having one would be a discriminator :)
Thanks for the contest-- this has been fun!