All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_atomic.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #if defined(_MSC_VER)
21 #else
23 #endif
24 
25 /******************************************************************************
26  * LOAD
27  *****************************************************************************/
28 
29 // double as_load_double(const double* target)
30 #define as_load_double(_target) ({ \
31  uint64_t v = as_load_uint64((const uint64_t*)_target); \
32  *(double*)&v; \
33  })
34 
35 // float as_load_float(const float* target)
36 #define as_load_float(_target) ({ \
37  uint32_t v = as_load_uint32((const uint32_t*)_target); \
38  *(float*)&v; \
39  })
40 
41 // Note - this assumes bool is implemented as a single byte.
42 // bool as_load_bool(const bool* target)
43 #define as_load_bool(_target) ({ \
44  (bool)as_load_uint8((const uint8_t*)_target); \
45  })
46 
47 // Note - this assumes bool is implemented as a single byte.
48 // bool as_load_bool_acq(const bool* target)
49 #define as_load_bool_acq(_target) ({ \
50  (bool)as_load_uint8_acq((const uint8_t*)_target); \
51  })
52 
53 /******************************************************************************
54  * STORE
55  *****************************************************************************/
56 
57 // void as_store_double(double* target, double value)
58 #define as_store_double(_target, _value) ({ \
59  double v = _value; \
60  as_store_uint64((uint64_t*)_target, *(uint64_t*)&v); \
61  })
62 
63 // void as_store_float(float* target, float value)
64 #define as_store_float(_target, _value) ({ \
65  float v = _value; \
66  as_store_uint32((uint32_t*)_target, *(uint32_t*)&v); \
67  })
68 
69 // Note - this assumes bool is implemented as a single byte.
70 // void as_store_bool(bool* target, bool value)
71 #define as_store_bool(_target, _value) ({ \
72  as_store_uint8((uint8_t*)_target, (uint8_t)_value); \
73  })
74 
75 // Note - this assumes bool is implemented as a single byte.
76 // void as_store_bool_rls(bool* target, bool value)
77 #define as_store_bool_rls(_target, _value) ({ \
78  as_store_uint8_rls((uint8_t*)_target, (uint8_t)_value); \
79  })
80 
81 /******************************************************************************
82  * FETCH AND SWAP
83  *****************************************************************************/
84 
85 // Note - this assumes pointers are 8 bytes.
86 // void* as_fas_ptr(void** target, void* value)
87 #define as_fas_ptr(_target, _value) ({ \
88  (void*)as_fas_uint64((uint64_t*)_target, (uint64_t)_value); \
89  })
90 
91 // double as_fas_double(double* target, double value)
92 #define as_fas_double(_target, _value) ({ \
93  double nv = _value; \
94  uint64_t ov = as_fas_uint64((uint64_t*)_target, *(uint64_t*)&nv); \
95  *(double*)&ov; \
96  })
97 
98 // float as_fas_float(float* target, float value)
99 #define as_fas_float(_target, _value) ({ \
100  float nv = _value; \
101  uint32_t ov = as_fas_uint32((uint32_t*)_target, *(uint32_t*)&nv); \
102  *(float*)&ov; \
103  })
104 
105 /******************************************************************************
106  * COMPARE AND SWAP
107  *****************************************************************************/
108 
109 // Note - this assumes pointers are 8 bytes.
110 // bool as_cas_ptr(void** target, void* old_value, void* new_value)
111 #define as_cas_ptr(_target, _old_value, _new_value) ({ \
112  as_cas_uint64((uint64_t*)_target, (uint64_t)_old_value, \
113  (uint64_t)_new_value); \
114  })
115 
116 // bool as_cas_double(double* target, double old_value, double new_value)
117 #define as_cas_double(_target, _old_value, _new_value) ({ \
118  double ov = _old_value; \
119  double nv = _new_value; \
120  as_cas_uint64((uint64_t*)_target, *(uint64_t*)&ov, *(uint64_t*)&nv); \
121  })
122 
123 // bool as_cas_float(float* target, float old_value, float new_value)
124 #define as_cas_float(_target, _old_value, _new_value) ({ \
125  float ov = _old_value; \
126  float nv = _new_value; \
127  as_cas_uint32((uint32_t*)_target, *(uint32_t*)&ov, *(uint32_t*)&nv); \
128  })