1 /*
2 
3    BLIS
4    An object-based framework for developing high-performance BLAS-like
5    libraries.
6 
7    Copyright (C) 2015, The University of Texas at Austin
8 
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions are
11    met:
12     - Redistributions of source code must retain the above copyright
13       notice, this list of conditions and the following disclaimer.
14     - Redistributions in binary form must reproduce the above copyright
15       notice, this list of conditions and the following disclaimer in the
16       documentation and/or other materials provided with the distribution.
17     - Neither the name(s) of the copyright holder(s) nor the names of its
18       contributors may be used to endorse or promote products derived
19       from this software without specific prior written permission.
20 
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 */
34 
35 #include <stdio.h>
36 #include <string.h>
37 
38 #define CPUNAME_REFERENCE     	0
39 #define CPUNAME_ARMV7       	1
40 #define CPUNAME_CORTEXA9       	2
41 #define CPUNAME_CORTEXA15      	3
42 
43 static char *cpuname[] = {
44   "reference",
45   "armv7a",
46   "cortex-a9",
47   "cortex-a15",
48 };
49 
50 
get_feature(char * search)51 int get_feature(char *search)
52 {
53   FILE *infile;
54   char buffer[2048], *p,*t;
55   p = (char *) NULL;
56 
57   infile = fopen("/proc/cpuinfo", "r");
58   if (infile == NULL) {
59     return 0;
60   }
61 
62   while (fgets(buffer, sizeof(buffer), infile)) {
63     if (!strncmp("Features", buffer, 8)) {
64       p = strchr(buffer, ':') + 2;
65       break;
66     }
67   }
68   fclose(infile);
69 
70   if( p == NULL ) return 0;
71 
72   t = strtok(p," ");
73   if (t != NULL) {
74     if (!strcmp(t, search)) { return 1; }
75   }
76   while( t = strtok(NULL," ")){
77     if (!strcmp(t, search)) { return 1; }
78   }
79 
80   return 0;
81 }
82 
cpu_detect(void)83 int cpu_detect(void)
84 {
85   FILE *infile;
86   char buffer[512], *p;
87   p = (char *) NULL ;
88 
89   infile = fopen("/proc/cpuinfo", "r");
90   if (infile == NULL) {
91     return CPUNAME_REFERENCE;
92   }
93   while (fgets(buffer, sizeof(buffer), infile)) {
94     if (!strncmp("CPU part", buffer, 8)) {
95       p = strchr(buffer, ':') + 2;
96       break;
97     }
98   }
99   fclose(infile);
100 
101   if(p != NULL) {
102     if (strstr(p, "0xc09")) {
103       if(get_feature("neon"))
104 	return CPUNAME_CORTEXA9;
105       else
106 	return CPUNAME_ARMV7;
107     }
108     if (strstr(p, "0xc0f")) {
109       if(get_feature("neon"))
110 	return CPUNAME_CORTEXA15;
111       else
112 	return CPUNAME_ARMV7;
113     }
114   }
115 
116   p = (char *) NULL ;
117   infile = fopen("/proc/cpuinfo", "r");
118   if (infile == NULL) {
119     return CPUNAME_REFERENCE;
120   }
121 
122   while (fgets(buffer, sizeof(buffer), infile)) {
123     if ((!strncmp("model name", buffer, 10)) || (!strncmp("Processor", buffer, 9))) {
124       p = strchr(buffer, ':') + 2;
125       break;
126     }
127   }
128   fclose(infile);
129 
130   if(p != NULL) {
131     if (strstr(p, "ARMv7")) {
132       return CPUNAME_ARMV7;
133     }
134   }
135 
136   return CPUNAME_REFERENCE;
137 }
138 
main()139 int main()
140 {
141   int cpuname_id;
142 
143   cpuname_id=cpu_detect();
144   printf("%s\n", cpuname[cpuname_id]);
145   return 0;
146 }
147