Skip to content

Instantly share code, notes, and snippets.

@lucasdemarchi
Created November 30, 2015 13:54
Show Gist options
  • Select an option

  • Save lucasdemarchi/b887a1cb12c36200a1e3 to your computer and use it in GitHub Desktop.

Select an option

Save lucasdemarchi/b887a1cb12c36200a1e3 to your computer and use it in GitHub Desktop.
#include <inttypes.h>
#include <typeinfo>
#include <stdio.h>
#define XCONCATENATE(x, y) x ## y
#define CONCATENATE(x, y) XCONCATENATE(x, y)
#define __MIN(a, b, uniq_a, uniq_b) ({ \
const typeof(a) uniq_a = (a); \
const typeof(b) uniq_b = (b); \
uniq_a < uniq_b ? uniq_a : uniq_b; \
})
#define MIN(a, b) \
__MIN((a), (b), \
CONCATENATE(__uniq_a_, __COUNTER__), \
CONCATENATE(__uniq_b_, __COUNTER__))
volatile int a = 20;
template<typename A, typename B>
static inline auto MIN1(const A &one, const B &two) -> decltype(one + two) {
auto res = one < two ? one : two;
return res;
}
template<typename A, typename B>
static inline auto MIN2(const A &one, const B &two) -> decltype(one + two) {
return one < two ? one : two;
}
__attribute__((noinline)) void min()
{
unsigned int b = 30;
printf("%d\n", (int) MIN(a, b));
}
__attribute__((noinline)) void min1()
{
unsigned int b = 30;
printf("%d\n", (int) MIN1(a, b));
}
__attribute__((noinline)) void min2()
{
unsigned int b = 30;
printf("%d\n", (int) MIN2(a, b));
}
int main()
{
min();
min1();
min2();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment