gen-vector-variants.sh 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh -e
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # Copyright © 2018-2019 Ariadne Devos
  4. set -e
  5. LC_ALL=C.UTF-8
  6. export LC_ALL
  7. type="$1"
  8. func="$2"
  9. source="$3"
  10. IFS=' '
  11. sizes='2 4 8 16 32 64'
  12. cat <<EOF
  13. #include <sHT/vector.h>
  14. EOF
  15. for size in $sizes; do
  16. cat <<EOF
  17. #if defined(sHT_target_${size}) && (${size} >= sHT_${type}_bytes)
  18. # define sHT_size ${size}
  19. # define sHT_target sHT_target_${size}
  20. # define ${func} ${func}_${size}
  21. # include "${source}"
  22. # undef ${func}
  23. # undef sHT_target
  24. # undef sHT_size
  25. #endif
  26. EOF
  27. done
  28. cat <<EOF
  29. /* musl doesn't allow GNU indirect functions.
  30. Do the portable thing: save a pointer. */
  31. #if 0
  32. EOF
  33. for size in $sizes; do
  34. cat <<EOF
  35. #elif defined(sHT_target_${size}) && (${size} >= sHT_${type}_bytes)
  36. typedef __typeof__(&${func}_${size}) ${func}_type;
  37. EOF
  38. done
  39. cat <<EOF
  40. #else
  41. # error impossible
  42. #endif
  43. static const ${func}_type ${func}_variants[] = {
  44. EOF
  45. for size in $sizes; do
  46. cat <<EOF
  47. #if defined(sHT_target_${size}) && (${size} >= sHT_${type}_bytes)
  48. ${func}_${size},
  49. #endif
  50. EOF
  51. done
  52. cat <<EOF
  53. };
  54. EOF