Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
emulate_gcc_builtin.h
Go to the documentation of this file.
1#ifndef ROUTING_KIT_EMULATE_GCC_BUILTIN_H
2#define ROUTING_KIT_EMULATE_GCC_BUILTIN_H
3#ifdef ROUTING_KIT_NO_GCC_EXTENSIONS
4
5#include <mutex>
6#include <atomic>
7
8#define __builtin_expect(x, y) (x)
9
10namespace RoutingKit{namespace detail{
11 inline
12 unsigned emulated_builtin_popcountll(unsigned long long x){
13 int n=0;
14 for(unsigned long long i=1; i!=0; i<<=1)
15 if(x & i)
16 ++n;
17 return n;
18 }
19
20 inline
21 unsigned emulated_builtin_popcount(unsigned x){
22 return emulated_builtin_popcountll(x);
23 }
24
25 inline
26 unsigned emulated_builtin_ffsll(unsigned long long x){
27 int n=1;
28 for(unsigned long long i=1; i!=0; i<<=1, ++n)
29 if(x & i)
30 return n;
31 return 0;
32 }
33
34 template<class T>
35 bool emulated_sync_bool_compare_and_swap(T*var, T comp_value, T new_value){
36 static std::mutex m;
37 std::lock_guard<std::mutex> lock(m);
38
39 std::atomic_thread_fence(std::memory_order_seq_cst);
40
41 if(*var == comp_value){
42 *var = new_value;
43
44 std::atomic_thread_fence(std::memory_order_seq_cst);
45 return true;
46 }else{
47 return false;
48 }
49 }
50
51}}
52
53#define __builtin_popcount(x) ::RoutingKit::detail::emulated_builtin_popcount(x)
54
55#define __builtin_popcountll(x) ::RoutingKit::detail::emulated_builtin_popcountll(x)
56
57#define __builtin_ffsll(x) ::RoutingKit::detail::emulated_builtin_ffsll(x)
58
59#define __sync_bool_compare_and_swap(x, y, z) ::RoutingKit::detail::emulated_sync_bool_compare_and_swap(x, y, z)
60
61#endif
62#endif