aboutsummaryrefslogtreecommitdiff
path: root/internal/platform.h
blob: 49e41a9acfe8af5562c1f17f4c93d10bbaf5739d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2015 The Gemmlowp Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// internal/platform.h: a place to put platform specific code

#ifndef GEMMLOWP_INTERNAL_PLATFORM_H_
#define GEMMLOWP_INTERNAL_PLATFORM_H_


#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#endif
#include <malloc.h>

#if defined ANDROID || defined __ANDROID__
#include <android/api-level.h>
// The 18 here should be 16, but has to be 18 for now due
// to a Google-internal issue.
#if __ANDROID_API__ < 18
#define GEMMLOWP_USE_MEMALIGN
#endif
// posix_memalign is missing on some 4.1 x86 devices
#if __ANDROID_API__ == 18
#ifdef GEMMLOWP_X86_32
#define GEMMLOWP_USE_MEMALIGN
#endif
#endif
#endif


namespace gemmlowp {

#ifdef _WIN32
inline void *aligned_alloc(size_t alignment, size_t size) {
  return _aligned_malloc(size, alignment);
}

inline void aligned_free(void *memptr) {
  _aligned_free(memptr);
}

inline int GetHardwareConcurrency(int max_threads) {
  if (max_threads == 0) {
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    return sysinfo.dwNumberOfProcessors;
  }
  return max_threads;
}

inline double real_time_in_seconds() {
  __int64 wintime; GetSystemTimeAsFileTime((FILETIME*)&wintime);
  wintime -= 116444736000000000i64;	            //1jan1601 to 1jan1970
  return wintime / 10000000i64 + wintime % 10000000i64 * 100 * 1e-9;
}

#else
inline void *aligned_alloc(size_t alignment, size_t size) {
#ifdef GEMMLOWP_USE_MEMALIGN
  return memalign(alignment, size);
#else
  void *memptr;
  if (posix_memalign(&memptr, alignment, size)) {
    memptr = nullptr;
  }
  return memptr;
#endif
}

inline int GetHardwareConcurrency(int max_threads) {
  if (max_threads == 0) {
    static const int hardware_threads_count =
        static_cast<int>(sysconf(_SC_NPROCESSORS_CONF));
    return hardware_threads_count;
  }
  return max_threads;
}

inline void aligned_free(void *memptr) {
  free(memptr);
}

inline double real_time_in_seconds() {
#ifdef __APPLE__
  timeval t;
  gettimeofday(&t, nullptr);
  return t.tv_sec + 1e-6 * t.tv_usec;
#else
  timespec t;
  clock_gettime(CLOCK_REALTIME, &t);
  return t.tv_sec + 1e-9 * t.tv_nsec;
#endif
}

#endif
} // namespace gemmlowp
#endif  // GEMMLOWP_INTERNAL_PLATFORM_H_