Няшная / Говнокод #25013 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 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;
}

https://wandbox.org/permlink/TwbKHE8l7ZJc6PNI
https://govnokod.ru/25005#comment436646
https://i.imgur.com/yFYfuED.jpg

Запостил: j123123 j123123, (Updated )

Комментарии (457) RSS

Добавить комментарий

Из-за тебя ушел bormand, guest!

    А не использовать ли нам bbcode?


    8