GLIBC_TUNABLES

2022-05-07

Many string and math functions come in multiple versions in glibc (known as “multiarch”). One of them is chosen at run time based on features supported by CPU.

Today I was trying to find a case wherein a math function returns different results on different processors, so I wanted to intentionally choose a less optimized version. There appeared to be no obvious way how this can be done. Finally I found that GLBC_TUNABLES could help me.

#include <math.h>
#include <stdio.h>

int main() {
  double x = 11.525775909423828;
  printf("exp(%.18g) = %.18g\n", x, exp(x));
  return 0;
}

Compile it:

gcc test.c -lm

Run it normally:

% ./a.out
exp(11.5257759094238281) = 101293.336622812101

Run it pretending my CPU doesn’t support FMA:

% GLIBC_TUNABLES=glibc.cpu.hwcaps=-FMA ./a.out
exp(11.5257759094238281) = 101293.336622812087