- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
#include <stdio.h>
#include <stdlib.h>
#include <stdalign.h>
#include <inttypes.h>
#include <string.h>
float sum_f(const float arr[], const size_t len);
int32_t sum_i32t(const int32_t arr[], const size_t len);
#define sum(a, b) _Generic((a), float*: sum_f, \
const float*: sum_f, \
int32_t*: sum_i32t,\
const int32_t*: sum_i32t)(a, b)
// foldl (+) 0 arr
float sum_f(const float arr[], const size_t len)
{
return (len != 0) ? ( sum(arr+1, len-1) + arr[0] ) : 0;
}
int32_t sum_i32t(const int32_t arr[], const size_t len)
{
return (len != 0) ? ( sum(arr+1, len-1) + arr[0] ) : 0;
}
enum { we_want_int, we_want_float } what_we_want;
void test(int www)
{
void *a;
if (www == we_want_int)
{
uint8_t buf[sizeof(int32_t[10])] __attribute__ ((aligned (alignof(int32_t[10]))));
a = (void *) buf;
memcpy ( a, (int32_t[10]){1,2,3,4,5,6,7,8,9,10},
sizeof((int32_t[10]){1,2,3,4,5,6,7,8,9,10})
);
printf("%" PRIi32 "\n", sum((int32_t *)a, 10));
}
else if (www == we_want_float)
{
uint8_t buf[sizeof(float[10])] __attribute__ ((aligned (alignof(float[10]))));
a = (void *) buf;
memcpy ( a, (float[10]){1,2,3,4,5,6,7,8,9,10},
sizeof((float[10]){1,2,3,4,5,6,7,8,9,10})
);
printf("%f\n", sum((float *)a, 10));
}
}
int main(void)
{
test(we_want_int);
test(we_want_float);
return EXIT_SUCCESS;
}