Readit News logoReadit News
strlenf commented on Type-erased generic functions for C: A modest non-proposal   duriansoftware.com/joe/ty... · Posted by u/thunderbong
Someone · a year ago
You can do that since C11 using _Generic. https://en.cppreference.com/w/c/language/generic:

  #include <math.h>
  #include <stdio.h>
 
  // Possible implementation of the tgmath.h macro cbrt
  #define cbrt(X) _Generic((X), \
              long double: cbrtl, \
                  default: cbrt,  \
                    float: cbrtf  \
              )(X)
 
  int main(void)
  {
    double x = 8.0;
    const float y = 3.375;
    printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
    printf("cbrtf(3.375) = %f\n", cbrt(y)); // converts const float to float,
                                            // then selects cbrtf
  }

strlenf · a year ago
True but its very clumsy for large number of parameters. Even more importantly, its not forward compatible. For example, cbrt() cant reliably expanded to support bigints or any private types.
strlenf commented on Type-erased generic functions for C: A modest non-proposal   duriansoftware.com/joe/ty... · Posted by u/thunderbong
strlenf · a year ago
Here is my not-very-novel proposal for function overriding only. Its backward/forward compatible and free of name-mangling.

  void qsort_i8 ( i8 *ptr, int num);
  void qsort_i16(i16 *ptr, int num);
  void qsort_i32(i32 *ptr, int num);
  void qsort_i64(i64 *ptr, int num);
  
  #if __has_builtin(__builtin_overridable)
  __builtin_overridable(qsort, qsort_i8, qsort_i16); // qsort resolves to qsort_i8 qsort_i16
  // qsort() is forward compatible and upgradable with your own types
  __builtin_overridable(qsort, qsort_i32, qsort_i64); // qsort resolves to qsort_i8 qsort_i16 qsort_i32 qsort_i64 
  #endif
  
  i8  *ptr0; int num0;
  i16 *ptr1; int num1;
  i32 *ptr2; int num2;
  i64 *ptr3; int num3;
  
  qsort(ptr0, num0); // call qsort_i8()
  qsort(ptr1, num1); // call qsort_i16()
  qsort(ptr2, num2); // call qsort_i32()
  qsort(ptr3, num3); // call qsort_i64()

strlenf commented on Microui+fenster=Small GUI   bernsteinbear.com/blog/fe... · Posted by u/surprisetalk
strlenf · 2 years ago
You probably want to use simd-optimized pixman[1] for rendering rectangles and glyphs. There is also luigi[2] which draws without opengl and written in C.

[1] https://github.com/freedesktop/pixman [2] https://github.com/nakst/luigi

u/strlenf

KarmaCake day23October 25, 2022View Original