linux.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* contrib/mips-msa/linux.c
  2. *
  3. * Copyright (c) 2020-2023 Cosmin Truta
  4. * Copyright (c) 2016 Glenn Randers-Pehrson
  5. * Written by Mandar Sahastrabuddhe, 2016.
  6. * Updated by Sui Jingfeng, 2021.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * On Linux, png_have_msa is implemented by reading the pseudo-file
  13. * "/proc/self/auxv".
  14. *
  15. * See contrib/mips-msa/README before reporting bugs.
  16. *
  17. * STATUS: SUPPORTED
  18. * BUG REPORTS: [email protected]
  19. */
  20. #include <elf.h>
  21. #include <fcntl.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. static int
  25. png_have_msa(png_structp png_ptr)
  26. {
  27. Elf64_auxv_t aux;
  28. int fd;
  29. int has_msa = 0;
  30. fd = open("/proc/self/auxv", O_RDONLY);
  31. if (fd >= 0)
  32. {
  33. while (read(fd, &aux, sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t))
  34. {
  35. if (aux.a_type == AT_HWCAP)
  36. {
  37. uint64_t hwcap = aux.a_un.a_val;
  38. has_msa = (hwcap >> 1) & 1;
  39. break;
  40. }
  41. }
  42. close(fd);
  43. }
  44. #ifdef PNG_WARNINGS_SUPPORTED
  45. else
  46. png_warning(png_ptr, "/proc/self/auxv open failed");
  47. #endif
  48. return has_msa;
  49. }