1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /* compile-time and runtime tests for whether to use MIPS-specific extensions */
6
7 #include "mips.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 enum{
14 MIPS_FLAG_LOONGSON3 = 1,
15 };
16
17 static unsigned
get_mips_cpu_flags(void)18 get_mips_cpu_flags(void)
19 {
20 unsigned flags = 0;
21 FILE *fin;
22
23 fin = fopen("/proc/cpuinfo","r");
24 if (fin != nullptr) {
25 char buf[1024];
26 memset(buf, 0, sizeof(buf));
27 fread(buf, sizeof(char), sizeof(buf) - 1, fin);
28 fclose(fin);
29 if (strstr(buf, "Loongson-3"))
30 flags |= MIPS_FLAG_LOONGSON3;
31 }
32 return flags;
33 }
34
35 static bool
check_loongson3(void)36 check_loongson3(void)
37 {
38 // Cache a local copy so we only have to read /proc/cpuinfo once.
39 static unsigned mips_cpu_flags = get_mips_cpu_flags();
40 return (mips_cpu_flags & MIPS_FLAG_LOONGSON3) != 0;
41 }
42
43 namespace mozilla {
44 namespace mips_private {
45 bool isLoongson3 = check_loongson3();
46 } // namespace mips_private
47 } // namespace mozilla
48