Sign in

Code that relies on wrapping

As Apple M1 launches, there comes a tendency to port codes originally run on x86 machines to ARM. For C code, porting can be easy if the code only relies on well-defined behaviors — you just need to recompile it. But things can get complicated if your code relies on undefined behaviors — they might differ on these two architectures.

Days ago I was porting otfcc, an optimized OpenType builder and inspector, to ARM platform. I was expecting all I needed to do was a simple recompilation and everything would run out-of-the-box. But I got sucked into segmentation error and…

TL;DR: Modern compilers feature very strong capability of auto-vectorization. So just write loops and let the compiler optimize them! (with an appropriate optimization level)

Why We Need to Vectorize Our Code

In many cases, we need to perform the same operations on a huge amount of data. For example, if I need to calculate a single value of the probability density function of the Cauchy distribution, I will write:

float dcauchy_single(float x, float location, float scale) {
const float fct = M_1_PI / scale;
const float frc = (x - location) / scale;
const float y = fct / (1.0f + frc * frc);
return y;
The PDF of the Cauchy distribution

Misaki Kasumi

A programming enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store