Skip to content

Instantly share code, notes, and snippets.

@mcspr
Created October 9, 2021 13:20
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double handle_doubles(double input) {
return fabs(input);
}
float handle_floats(float input) {
return fabsf(input);
}
long handle_long(long input) {
return labs(input);
}
#define abs(x)\
__builtin_choose_expr(\
__builtin_types_compatible_p(typeof(x), double),\
handle_doubles(x),\
__builtin_choose_expr(\
__builtin_types_compatible_p(typeof(x), float),\
handle_floats(x),\
handle_long((long)x)))
int main() {
float a = 0.f;
double b = -5.;
int c = 0;
printf("%f\n", abs(a));
printf("%f\n", abs(b));
printf("%ld\n", abs(c));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment