Skip to content

Instantly share code, notes, and snippets.

@phargogh
Last active May 10, 2024 23:07
Show Gist options
  • Save phargogh/c4264b37e7f0beed31661eacce53d14a to your computer and use it in GitHub Desktop.
Save phargogh/c4264b37e7f0beed31661eacce53d14a to your computer and use it in GitHub Desktop.
Clang/ARM64 compilation issue

Compilation issue with clang 14 on ARM64

To reproduce:

  • Use an ARM64 computer (tested on M1 mac and on Raspberry Pi model 4B)

    • On Raspberry pi, I used Raspbian:bookworm
    • On M1 mac, I used docker run --rm -ti -v $(pwd):/demo -w /demo debian:bookworm /bin/bash (which shares the local directory into the container, assumes you are running this from the cloned gist)
  • On your computer of choice, run apt update && apt install -y build-essential clang-13 clang

  • Observe gcc produces correct results: gcc ./min-repro-sample.c && ./a.out

    The number printed here is expected to be 0.0.
    GCC (all versions tested) produces the correct result.
    Clang 14.0.6 introduces small numerical error, producing a value > 0
    The latest version of clang tested producing the correct result is clang 13.0.1 (from debian:bookwork clang-13)
    result: 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000
    
    On clang 14.0.6, the calculated result below is a workaround that produces the expected 0.0
    result: 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000
    
  • Observe clang-13 produces correct results: clang-13 ./min-repro-sample.c && ./a.out

    The number printed here is expected to be 0.0.
    GCC (all versions tested) produces the correct result.
    Clang 14.0.6 introduces small numerical error, producing a value > 0
    The latest version of clang tested producing the correct result is clang 13.0.1 (from debian:bookwork clang-13)
    result: 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000
    
    On clang 14.0.6, the calculated result below is a workaround that produces the expected 0.0
    result: 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000
    
  • Observe clang-14 produces unexpected results: clang-14 ./min-repro-sample.c && ./a.out

    The number printed here is expected to be 0.0.
    GCC (all versions tested) produces the correct result.
    Clang 14.0.6 introduces small numerical error, producing a value > 0
    The latest version of clang tested producing the correct result is clang 13.0.1 (from debian:bookwork clang-13)
    result: 0.00000000000000004058614543027810527553055411362964847252091191848188955759724195
    
    On clang 14.0.6, the calculated result below is a workaround that produces the expected 0.0
    result: 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000
    
#include <stdio.h>
int main() {
double previous_height = 1.0;
double r_v = 2.0;
double slope_distance = 1.4142135623730951;
double target_distance = 2.8284271247461903;
printf("The number printed here is expected to be 0.0.\n");
printf("GCC (all versions tested) produces the correct result.\n");
printf("Clang 14.0.6 introduces small numerical error, producing a value > 0\n");
printf("The latest version of clang tested producing the correct result is clang 13.0.1 (from debian:bookworm clang-13)\n");
printf("result: %.80f\n", ((((previous_height-r_v)/slope_distance) * target_distance) + r_v));
printf("\n");
printf("On clang 14.0.6, the calculated result below is a workaround that produces the expected 0.0\n");
double mult = (((previous_height-r_v)/slope_distance) * target_distance);
double added = mult + r_v;
printf("result: %.80f\n", added);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment