1// Copyright 2015 The Gemmlowp Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// internal/platform.h: a place to put platform specific code
16
17#ifndef GEMMLOWP_INTERNAL_PLATFORM_H_
18#define GEMMLOWP_INTERNAL_PLATFORM_H_
19
20#ifdef _WIN32
21#include <malloc.h>
22#include <windows.h>
23#else
24#include <stdlib.h>
25#include <time.h>
26#include <unistd.h>
27#endif
28
29#ifdef __APPLE__
30#include <sys/time.h>
31#endif
32
33#if defined ANDROID || defined __ANDROID__
34#include <malloc.h>
35#include <android/api-level.h>
36// The 18 here should be 16, but has to be 18 for now due
37// to a Google-internal issue.
38#if __ANDROID_API__ < 18
39#define GEMMLOWP_USE_MEMALIGN
40#endif
41// posix_memalign is missing on some 4.1 x86 devices
42#if __ANDROID_API__ == 18
43#ifdef GEMMLOWP_X86_32
44#define GEMMLOWP_USE_MEMALIGN
45#endif
46#endif
47#endif
48
49// Needed by chrome native builds
50#ifndef _SC_NPROCESSORS_CONF
51#define _SC_NPROCESSORS_CONF _SC_NPROCESSORS_ONLN
52#endif
53
54namespace gemmlowp {
55
56#ifdef _WIN32
57inline void *aligned_alloc(size_t alignment, size_t size) {
58 return _aligned_malloc(size, alignment);
59}
60
61inline void aligned_free(void *memptr) { _aligned_free(memptr); }
62
63inline int GetHardwareConcurrency(int max_threads) {
64 if (max_threads == 0) {
65 SYSTEM_INFO sysinfo;
66 GetSystemInfo(&sysinfo);
67 return sysinfo.dwNumberOfProcessors;
68 }
69 return max_threads;
70}
71
72inline double real_time_in_seconds() {
73 __int64 wintime;
74 GetSystemTimeAsFileTime((FILETIME *)&wintime);
75 wintime -= 116444736000000000LL; // 1jan1601 to 1jan1970
76 return wintime / 10000000LL + wintime % 10000000LL * 100 * 1e-9;
77}
78
79#else
80inline void *aligned_alloc(size_t alignment, size_t size) {
81#ifdef GEMMLOWP_USE_MEMALIGN
82 return memalign(alignment, size);
83#else
84 void *memptr;
85 if (posix_memalign(&memptr, alignment, size)) {
86 memptr = nullptr;
87 }
88 return memptr;
89#endif
90}
91
92inline int GetHardwareConcurrency(int max_threads) {
93 if (max_threads == 0) {
94 static const int hardware_threads_count =
95 static_cast<int>(sysconf(_SC_NPROCESSORS_CONF));
96 return hardware_threads_count;
97 }
98 return max_threads;
99}
100
101inline void aligned_free(void *memptr) { free(memptr); }
102
103inline double real_time_in_seconds() {
104#ifdef __APPLE__
105 timeval t;
106 gettimeofday(&t, nullptr);
107 return t.tv_sec + 1e-6 * t.tv_usec;
108#else
109 timespec t;
110 clock_gettime(CLOCK_REALTIME, &t);
111 return t.tv_sec + 1e-9 * t.tv_nsec;
112#endif
113}
114
115#endif
116} // namespace gemmlowp
117#endif // GEMMLOWP_INTERNAL_PLATFORM_H_
118