Daniel Lemire's blog

How big are your SVE registers ? (AWS Graviton)

, 1 min read

Amazon has some neat ARM-based systems based on Amazon’s own chips (Graviton). You can access them through Amazon’s web services (AWS). These processors have advanced vector instructions able to process many values at once. These instructions are part of an instruction sets called SVE for…

Generic number compression (zstd)

, 1 min read

I have done a lot of work that involves compressing and uncompressing data. Most often, I work on data that has specific characteristics, e.g., sorted integers. In such cases, one can do much better than generic compression routines (e.g., zstd, gzip) both in compression ratios and performance. But…

Science and Technology links (November 26 2022)

, 2 min read

In Molière’s famous play, Tartuffe, the main characters is outwardly pious but fundamentally deceitful. Are people who insist on broadcasting their high virtue better people, or are they more like Tartuffe? Dong et al. (2022) conclude that people who say that they have good values are not…

Making all your integers positive with zigzag encoding

, 2 min read

You sometimes feel the need to make all of your integers positive, without losing any information. That is, you want to map all of your integers from ‘signed’ integers (e.g., -1, 1, 3, -3) to ‘unsigned integers’ (e.g., 3,2,6,7). This could be useful if you have a fast function to compress…

What is the size of a byte[] array in Java?

, 1 min read

Java allows you to create an array just big enough to contain 4 bytes, like so: byte[] array = new byte[4]; How much memory does this array take? If you have answered “4 bytes”, you are wrong. A more likely answer is 24 bytes. I wrote a little Java program that relies on the jamm library to…

Rounding modes: std::from_chars versus strtod/strtof

, 1 min read

A recent C++ standard (C++17) introduced new functions to parse floating-point numbers std::from_chars, from strings (e.g., ASCII text) to binary numbers. How should such a function parse values that cannot be represented exactly? The specification states that the resulting value rounded to…

A fast function to check your floating-point rounding mode

, 3 min read

For speed, we use finite-precision number types in software. When doing floating-point computations in software, the results are usually not exact. For example, you can have the number 1.0 and the number 2-1000. They can both be represented exactly using standard double-precision IEEE…

Measuring the memory usage of your C++ program

, 1 min read

In C++, we might implement dynamic lists using the vector template. The int-valued constructor of the vector template allocates at least enough memory to store the provided number of elements in a contiguous manner. How much memory does the following code use? std::vector<uint8_t> v1(10); …

Modern vector programming with masked loads and stores

, 5 min read

When you start a program, it creates a ‘process’ which own its memory. Memory is allocated to a software process in blocks called ‘pages’. These pages might span 4kB, 16kB or more. For a given process, it is safe to read and write within these pages. In your code, you might allocate a…

Book Review : Template Metaprogramming with C++

, 3 min read

I have spent the last few years programming often in C++. The C++ langage is probably one of the hardest to master. I still learn something new every week. Furthermore, C++ is getting upgrades all the time: C++17 was a great step forward and C++20 brings even more exiting improvments. In C++, we…

Science and Technology links (October 16 2022)

, 1 min read

Doctors in Israel are toying with polygenic screening: it is a way to make it more likely that your baby will grow up to be healthy. In 2021, 337 million prescriptions were written for antidepressants in US, according to the New York Times. Students in private schools do better in India than those…

The number of comparisons needed to sort a shuffled array: qsort versus std::sort

, 2 min read

Given an array of N numbers of type double, the standard way to sort it in C is to invoke the qsort function qsort(array, N, sizeof(double), compare); where compare is a function which returns an integer less than, equal to, or greater than zero if the first argument is less than, equal to, or…

A review of elementary data types : numbers and strings

, 23 min read

Computer programming starts with the organization of the data into data structures. In almost all cases, we work with strings or numbers. It is critical to understand these building blocks to become an expert programmer. Words We often organize data using fixed blocks of memory. When these blocks…

Optimizing compilers deduplicate strings and arrays

, 2 min read

When programming, it can be wasteful to store the same constant data again and again. You use more memory, you access more data. Thankfully, your optimizing compiler may help. Consider the following two lines of C code: printf("Good day professor Jones"); printf("Good day…

Science and Technology links (September 16 2022)

, 1 min read

Attractive female students get better grades. They lose this benefit when courses move online. A research paper is much more likely to be highly ranked if the author is famous. The USA has many more prisoners than police officers (three prisoners for every police officer), while every other…

Escaping strings faster with AVX-512

, 2 min read

When programming, we often have to ‘escape’ strings. A standard way to do it is to insert the backslash character () before some characters such as the double quote. For example, the string <tt>my title is "La vie"</tt>``` becomes ```C <tt>my title is \"La…

Science and Technology links (September 12 2022)

, 2 min read

A standard dataset in artificial-intelligence research has ten percent of its images mislabeled. Yet state-of-the-art algorithms achieve better-than-90% classification on the same dataset. (Credit: Leo Boytsov) Despite the reeducation camps and the massive trauma, families that did well before the…

Catching sanitizer errors programmatically

, 1 min read

The C and C++ languages offer little protection against programmer errors. Errors do not always show up where you expect. You can silently corrupt the content of your memory. It can make bugs difficult to track. To solve this problem, I am a big fan of programming in C and C++ using sanitizers.…

“Hello world” is slower in C++ than in C (Linux)

, 4 min read

A simple C program might print ‘hello world’ on screen: #include <stdio.h> #include <stdlib.h> int main() { printf("hello world\n"); return EXIT_SUCCESS; } You can write the equivalent in C++: #include <iostream> #include <stdlib.h> int main() { …