15/03/2011
I came across a lovely bug lately. Integer arithmetic, especially in C and C++ it seems, is error-prone. In addition to the risk of having the wrong expressions altogether (a logic error, one could say), integer arithmetic is subject to a number of pitfalls, some I have already discussed here, here, and here. This week, I discuss yet another occasion for error using integer arithmetic.

Consider this piece of code, one that you have seen many times probably, at least as a variation on the theme:
Read the rest of this entry »
8 Comments |
algorithms, C, C-plus-plus, C99 | Tagged: C, int32_t, int64_t, Integer Arithmetic, INT_MAX, Modular Arithmetic, NaN, Overflow, Underflow |
Permalink
Posted by Steven Pigeon
01/03/2011
The game is different whether we consider external data structures—that live partially or totally on disk or over the network—or internal data structures residing entirely in local memory. For one thing, none of the courses I had on data structures I had really prepared me to deal adequately with (large) external data structures; it was always assumed that the computer’s memory was somehow spacious enough to hold whatever data you needed.

However, when you’re dealing with files, you can’t really assume that you can access random locations to read, write, or exchange data at low cost, and you have to rethink your algorithms (or change plans altogether) to take the cost of slow external media into account. Sometimes an algorithm that doesn’t look all that useful in main memory suddenly makes more sense in external memory because of the way it scans its data. One of these algorithms is the Shellsort.
Read the rest of this entry »
18 Comments |
algorithms, C-plus-plus, data structures, Mathematics, programming | Tagged: C, Combinatorial Analysis, External Data Structures, Hoare, QuickSort, shell, Shellsort, sorting |
Permalink
Posted by Steven Pigeon
01/02/2011
There are plenty of web sites and museums dedicated to the computers of yore. While most of them now seems quaint, and delightfully obsolete, there are probably a lot of lessons we could re-learn and apply today, with our modern computers.

If you followed my blog for some time, you know that I am concerned with efficient computation and representation of just about everything, applied to workstation, servers, and embedded systems. I do think that retro-computing (computing using old computers or the techniques of old computer) has a lot to teach us, and not only from an historical perspective.
Read the rest of this entry »
Leave a Comment » |
algorithms, C, CPU Architecture, data structures, Design, embedded programming, hacks, programming | Tagged: 6502, 6809, TRS-80, Z80 |
Permalink
Posted by Steven Pigeon
12/10/2010
Last week, we discussed how we could generate uniform random points in a triangle using a (tiny) bit of linear algebra, mostly the parallelogram rule, and a random variable uniform on
. It required a tiny bit of math and was computationally very simple.

This time, let us see how we can generate uniformly distributed random points on a sphere.
Read the rest of this entry »
20 Comments |
algorithms, Mathematics, programming | Tagged: Calculus, Differential Area, pseudo-random, random, Sphere, Surface of Revolution, uniform random |
Permalink
Posted by Steven Pigeon
05/10/2010
In the first post of this series, I discussed a method to generate a random (guaranteed) permutation. On another occasion, I presented a method to recycle expensive random bits. Today, I will discuss something I had to do recently: generate random points inside a triangle.

Read the rest of this entry »
18 Comments |
algorithms, hacks, Mathematics, programming | Tagged: linear algebra, PRNG, pseudo-random, Rejection Method, triangle, uniform random |
Permalink
Posted by Steven Pigeon
31/08/2010
If you’re doing image processing, you’ve probably had to transform your image from one color space to another. In video coding, for example, the RGB image is transformed into YPrPb or YCrCb so that most of the visually relevant information is packed into the Y component which is essentially brightness. Subsampling the chroma bands (Cr and Cb) provides additional means for compression with little perceptual quality loss. While the human eye is very good at detecting brightness variation, it’s not very good at detecting subtle changes in chroma, either saturation or hue.

The thing is that very often, there are color space transformation matrices found in text book but they’re not, due to rounding (and other possible errors), always exactly inverses of each other. This week, I will discuss how we can use projections onto convex sets (POCS) to make sure that reduced precision matrices are exactly (within a given precision) reversible.
Read the rest of this entry »
1 Comment |
algorithms, Mathematics, programming, Science | Tagged: Color Space, Convex, Convex Projection, Convex Sets, Orthogonal Projection, POCS, precision, Projection, rgb, ycrcb, YPrPb |
Permalink
Posted by Steven Pigeon
17/08/2010
Tomorrow’s flowers are in the seeds of today.
Chinese proverb

Read the rest of this entry »
2 Comments |
algorithms, C99, hacks, wtf, Zen | Tagged: Chinese Proverb, pseudo-random, random, weasel, whale |
Permalink
Posted by Steven Pigeon
10/08/2010
HAMLET:
Do you see yonder cloud that’s almost in shape of a camel?
POLONIUS:
By the mass, and ’tis like a camel, indeed.
HAMLET:
Methinks it is like a weasel.
POLONIUS:
It is backed like a weasel.
HAMLET:
Or like a whale.
POLONIUS:
Very like a whale.
Hamlet, Act III
William Shakespeare

Read the rest of this entry »
7 Comments |
algorithms, C99, hacks, wtf, Zen | Tagged: Hamlet, Polonius, pseudo-random, random, Shakespare, weasel |
Permalink
Posted by Steven Pigeon
27/07/2010
Hacks with integer arithmetic are always fun, especially when they’re not too hard to understand (because some are really strange and make you wonder what the author was thinking when he wrote that). One such simple hack is to replace divisions or multiplies by series of shifts and additions.

However, these hacks make a lot of assumptions that aren’t necessarily verified on the target platform. The assumptions are that complex instructions such as mul and div are very slow compared to adds, right shifts or left shifts, that execution time of shifts only depends very loosely on the number of bit shifted, and that the compiler is not smart enough to generate the appropriate code for you.
Read the rest of this entry »
8 Comments |
algorithms, assembly language, bit twiddling, C, embedded programming, Mathematics, Portable Code, programming | Tagged: assumptions, shifts, shifty, timing |
Permalink
Posted by Steven Pigeon