11/03/2014
The strangest aspect of the Ancient Egyptian’s limited mathematics is how they wrote fractions. For example, they could not write
outright, they wrote
,
and this method of writing fractions lead to absurdly complicated solutions to trivial problems—at least, trivial when you can write a vulgar fraction such as
. But how much more complicated is it to write
rather than
?
Read the rest of this entry »
2 Comments |
algorithms, Mathematics | Tagged: base 2, binary representation, egyptian fraction, Egyptian fractions, fractions, greedy algorithms, unit fraction, unit fractions |
Permalink
Posted by Steven Pigeon
04/03/2014
In a previous post, we started discussing the subject of encoding values using fractional bits. This is quite counter-intuitive as we usually think that encoding asks for whole bits, furthermore that we think of bits as being essentially atomic—unsplittable.

However, bits can be fractioned, helping us devising efficient encodings, something not unlike a primitive form of arithmetic coding, for a variety of data. Let’s consider a simple case that illustrates the principle: encoding the date.
Read the rest of this entry »
3 Comments |
algorithms, data compression | Tagged: and, bit shift, bit twiddling, fractional bits, optimization, or, shift |
Permalink
Posted by Steven Pigeon
18/02/2014
In Universal Coding (part II), we explored the idea of variable length codes that encode integers of any magnitude in
bits, but the encoding requires (somewhat) complex arithmetic, and we concluded that sometimes, it’s better to use a simple code that sacrifices some coding-efficiency in exchange for machine-efficiency. Let’s now look at one of those, MIDI VLQ and how we can make it a bit better.

Read the rest of this entry »
Leave a Comment » |
algorithms, data compression, Mathematics | Tagged: MIDI, MIDI VLQ, redundancy, variable length quantity, VLQ |
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