09/09/2014
A few days ago, a friend asked me how to write, if even possible, a hash function that had no collisions. Of course, it’s relatively easy to get a reasonably good hash function that will give the expected amount of collisions (especially when used as usual, modulo the size of the table). But what if we do not want collisions at all? What can we do?

There are some hash functions, known as perfect hash function, that will yield a different hash for every key from a a priori known set of keys, say
. Are they hard to build? Fortunately, not really. Can we exploit the uniqueness of the hashed value to speed look-ups up? Yes, but it’s a bit more complicated.
Read the rest of this entry »
Leave a Comment » |
algorithms, C-plus-plus, data structures | Tagged: constant time, hash, hash function, hash functions, hashing, perfect hash, perfect hash function, perfect hashing |
Permalink
Posted by Steven Pigeon
06/05/2014
last week, we saw that we could use a (supposed) efficient machine-specific instruction to derive good bounds (but not very tight) to help binary search-based and Newton-based square root extraction algorithms go faster. Last week, we saw that the technique did lead to fewer iterations in both algorithms.

Now, does the reduced number of iterations translate into actual, machine speed-up?
Read the rest of this entry »
3 Comments |
algorithms, C, C-plus-plus, hacks, Mathematics, programming | Tagged: Newton, Square root, Square Roots |
Permalink
Posted by Steven Pigeon
01/04/2014
If you’re a perfectionist, it’s really hard to limit the efforts you put into developing code. A part of you wants to write the perfect code, while another reminds you that you haven’t time for that, and you will have to settle for good enough code. Today’s entry is exactly this: an ambitious design that was reduced to merely OK code.

I needed to have an exporter (but no importer) to CSV format for C++. One of the first thing that came to mind is to have a variant-like hierarchy that can store arbitrary values, each specific class having its own to_string function, and then have some engine on top that can scan a data structure and spew it to disk as CSV. That’s ridiculously complicated—very general—but ridiculously complicated.
Read the rest of this entry »
Leave a Comment » |
C-plus-plus, programming | Tagged: C, operator overloading, operators, Simplicity, stream |
Permalink
Posted by Steven Pigeon
04/02/2014
One good thing with 64 bits addresses, is that you can, in principle, use essentially as much memory as you want—the address space certainly exceeds today’s computers’ capabilities. One bad thing, especially when you create lots of objects and need plenty of pointers, is that 64 bits pointers are big. They use 8 bytes of memory. One or two pointers aren’t a problem, of course, but what if your data structure is a sparse graph, each node being mostly pointers, and that you need to create a very large graph?

One solution is to use stretch codes, as I proposed a while ago, trading off precision of addressing for shorter pointers. However, unless you rewrite the memory allocator, the technique won’t play well with the default new. Another solution is to store just barely the number of bits (rounded to bytes) necessary to hold an address. Can we do this? If so, how?
Read the rest of this entry »
3 Comments |
C-plus-plus, data compression, data structures, hacks, programming | Tagged: 64 bits, address space, stretch codes, virtual memory |
Permalink
Posted by Steven Pigeon
28/01/2014
Every programming language has its defects; sometimes large, sometimes subtle; often causing irks. One thing that has been bugging me for a while (and many others, as you can see if you use Google) is the impossibility of enumerating enums from basic language constructs. You just can’t. Using the STL (and C++ 2011), however, you can have a somewhat elegant solution.
The solution I propose in this post is robust as it does not depend on the actual values of enums nor on that they can be mapped onto integers. We arrived at this solution (we being me and people on the ##c++ channel on Freenode, especially user “moonchild”) and I think it (almost) solves the problem of enumerating enums.
Read the rest of this entry »
1 Comment |
C-plus-plus, programming | Tagged: C, enum, initializer_list, int, set, stl |
Permalink
Posted by Steven Pigeon
21/01/2014
So last week we saw how to use some of GCC’s built-ins, this week, let’s have a look at how we can create our own, if need be. Say because you need to have access to some instruction and that GCC does not offer the corresponding built-in.

To do so, we’ll use a bit of the C preprocessor and GCC’s inline assembly extension.
Read the rest of this entry »
2 Comments |
assembly language, C, C-plus-plus, programming | Tagged: 32 bits, 64 bits, AMD64, asm, assembly language, intrinsic, intrinsics, x86, x86_64 |
Permalink
Posted by Steven Pigeon
14/01/2014
Last week we discussed GCC intrinsics a bit. This week, let’s have a look at what kind of speed-ups we can get, and how the use of intrinsics affect code generation.

Sometimes, I do strange things. I mean, my experiments aren’t necessarily self-evident, and sometimes, I need performance from primitives that usually are not bottlenecks—such as computing the GCD. This time, I need to get
and
in
as fast as possible. Let’s have a look at how intrinsics help us.
Read the rest of this entry »
4 Comments |
bit twiddling, C, C-plus-plus, hacks, programming | Tagged: clz, code optimization, g++, gcc, Hacker's Delight, intrinsics |
Permalink
Posted by Steven Pigeon
07/01/2014
In the discussion of The Speed of GCD, Daniel Lemire remarked that one could use the compiler-specific intrinsic function __builtin_ctz to get a good speed-up on binary GCD. That remark made me look into all the others intrinsics and built-ins offered by GCC.

Let’s have a look to at least a few of them!
Read the rest of this entry »
3 Comments |
assembly language, C, C-plus-plus, programming | Tagged: built-in, cache, cache management, efficient code, GCD, intrinsic, intrinsic function, optimization, special instructions |
Permalink
Posted by Steven Pigeon
17/12/2013
Let’s consider a rather simple puzzle:
SEND
+ MORE
= MONEY
where each letter is assigned a value from 0 to 9. How would you solve
it?
Read the rest of this entry »
1 Comment |
algorithms, C-plus-plus, Mathematics, programming | Tagged: permutation, permutation generating algorithm, permutations, Prolog, puzzle, shuttle algorithm |
Permalink
Posted by Steven Pigeon
05/11/2013
This week, let’s go back to (low level) programming, with IEEE floats. To unit test a function of float, it does not sound unreasonable to just enumerate them all. But how do we do that efficiently? Clearly f++ will not get us there.

Nor will the machine-epsilon (the std::numeric_limits::epsilon()) because this value works fine around 1, but as the value diverges from 1, the epsilon basically becomes useless. We would either need a magnitude-dependent epsilon (which the standard library does not provide) or a way of enumerating explicitly the floats in increasing or decreasing order (something also not provided by the standard library). Well, let’s see how we can do that
Read the rest of this entry »
Leave a Comment » |
algorithms, C, C-plus-plus, C99, programming | Tagged: epsilon, floats, IEEE 754 |
Permalink
Posted by Steven Pigeon