-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmembench.h
138 lines (119 loc) · 3.31 KB
/
membench.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* string operation benchmarks
*
* Copyright (C) 2025 William Horvath
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#pragma once
#include <stdlib.h>
#ifdef _WIN32
# define stdlib "ucrtbase.dll"
# define stdlib_fb "msvcrt.dll"
# ifdef __i386__
# define memlib "./libmembase32-windows-msvc.dll"
# else
# define memlib "./libmembase64-windows-msvc.dll"
# endif
#elif defined(MUSL)
# define stdlib "libc.so"
# define stdlib_fb stdlib
# ifdef __i386__
# define memlib "./libmembase32-linux-musl.so"
# else
# define memlib "./libmembase64-linux-musl.so"
# endif
#else
# define stdlib "libc.so.6"
# define stdlib_fb stdlib
# ifdef __i386__
# define memlib "./libmembase32-linux-gnu.so"
# else
# define memlib "./libmembase64-linux-gnu.so"
# endif
#endif
#ifdef _WIN32
#include <windows.h>
#include <malloc.h>
#include <stdint.h>
#define RTLD_NOW 0
typedef HMODULE dl_handle;
#define dlopen(path, mode) LoadLibraryA(path)
#define dlclose(handle) FreeLibrary(handle)
#define dlerror() "Windows error"
static inline void *dlsym(dl_handle handle, const char *symbol)
{
FARPROC proc = GetProcAddress(handle, symbol);
union
{
FARPROC proc;
void *ptr;
} cast = {proc};
return cast.ptr;
}
static inline void *__aligned_alloc(size_t alignment, size_t size)
{
return _aligned_malloc(size, alignment);
}
static inline void __aligned_free(void *ptr)
{
_aligned_free(ptr);
}
#else
#include <dlfcn.h>
#include <time.h>
typedef void *dl_handle;
static inline void *__aligned_alloc(size_t alignment, size_t size)
{
return aligned_alloc(alignment, size);
}
static inline void __aligned_free(void *ptr)
{
free(ptr);
}
#endif
struct timespec_portable
{
int64_t tv_sec;
int64_t tv_nsec;
};
static inline void get_monotonic_time(struct timespec_portable *ts)
{
__asm__ __volatile__ ( "mfence" : : : "memory" );
#ifdef _WIN32
static LARGE_INTEGER freq;
static int init = 0;
LARGE_INTEGER count;
if (!init)
{
QueryPerformanceFrequency(&freq);
init = 1;
}
QueryPerformanceCounter(&count);
ts->tv_sec = count.QuadPart / freq.QuadPart;
ts->tv_nsec = ((count.QuadPart % freq.QuadPart) * 1000000000) / freq.QuadPart;
#else
struct timespec native_ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &native_ts);
ts->tv_sec = native_ts.tv_sec;
ts->tv_nsec = native_ts.tv_nsec;
#endif
__asm__ __volatile__ ( "mfence" : : : "memory" );
}
static inline double timespec_to_seconds(const struct timespec_portable *start,
const struct timespec_portable *end)
{
return (end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec) / 1e9;
}