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