libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
blatant_old_msvc_compiler_hacks.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_BLATANT_OLD_MSVC_COMPILER_HACKS_HPP
2 #define LIBJMMCG_CORE_BLATANT_OLD_MSVC_COMPILER_HACKS_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "config.h"
23 
24 #include <new>
25 
26 /**
27  \mainpage
28  \section Overview
29  See <a href="http://libjmmcg.sf.net/">the web site</a> for an overview and previously-given presentations regarding the library.
30 */
31 
32 /** \file
33  This file contains some of the more blatant, nasty, evil hacks that need
34  to be done to work-around the venerable MSVC++ v6.0 sp5 compiler's "features".
35  Note that this compiler should really be referred to as "cl.exe" v12.00.8168.
36 */
37 
38 /// Work arounds for the older MSVC++ compilers being very broken regarding the use of various keywords.
39 #define JMMCG_MSVC_STDCALL_HACK __stdcall
40 #define JMMCG_MSVC_FASTCALL_HACK __fastcall
41 
42 #ifndef _CRT_SECURE_NO_WARNINGS
43 # define _CRT_SECURE_NO_WARNINGS
44 #endif
45 #ifndef _WIN32_WINNT
46 # define _WIN32_WINNT 0x0501
47 #endif
48 #ifndef WINVER
49 # define WINVER _WIN32_WINNT
50 #endif
51 
52 #if defined(_MSC_VER) && (_MSC_VER < 1300)
53 # define __FUNCTION__ _T("Message to M$: you should have defined __FUNCTION__!")
54 /// Hacks to get round MS macros in "windef.h" that conflict with the STL definitions of min() and max().
55 # undef min
56 # undef max
57 # ifndef JMMCG_MS_MINMAX_HACK
58 # define JMMCG_MS_MINMAX_HACK
59 # define NOMINMAX
60  namespace std {
61  template<class _Ty> inline const _Ty& __fastcall
62  max(const _Ty& _X, const _Ty& _Y) {
63  return _cpp_max(_X, _Y);
64  }
65  template<class _Ty, class _Pr> inline const _Ty& __fastcall
66  max(const _Ty& _X, const _Ty& _Y, _Pr _P) {
67  return _cpp_max(_X, _Y, _P);
68  }
69 
70  template<class _Ty> inline const _Ty& __fastcall
71  min(const _Ty& _X, const _Ty& _Y) {
72  return _cpp_min(_X, _Y);
73  }
74  template<class _Ty, class _Pr> inline const _Ty& __fastcall
75  min(const _Ty& _X, const _Ty& _Y, _Pr _P) {
76  return _cpp_min(_X, _Y, _P);
77  }
78  }
79 # endif
80 # if (_MSC_VER < 1300)
81 # undef JMMCG_MSVC_FASTCALL_HACK
82 # define JMMCG_MSVC_FASTCALL_HACK
83 # undef JMMCG_MSVC_STDCALL_HACK
84 # define JMMCG_MSVC_STDCALL_HACK
85 # endif
86 #endif
87 
88 #ifdef _MSC_VER
89 /// Get rid of stupid MSVC warnings.
90 # pragma warning(disable:4033) ///< Conversion from OMF to COFF warning.
91 # pragma warning(disable:4256) ///< constructor for class with virtual bases has '...'; calls may not be compatible with older versions of Visual C++
92 # pragma warning(disable:4290) ///< C++ Exception Specification ignored.
93 # pragma warning(disable:4503) ///< Decorated name length exceeded, name was truncated.
94 # pragma warning(disable:4710) ///< function '...' not inlined
95 # pragma warning(disable:4711) ///< function '...' selected for automatic inline expansion
96 # pragma warning(disable:4725) ///< Pentium fdiv warning.
97 # pragma warning(disable:4786) ///< "identifier was truncated to '255' characters in the debug information", (from MSVC++ v5.0 help).
98 # pragma warning(disable:4800) ///< forcing value to bool 'true' or 'false' (performance warning).
99 
100 # define _CRT_SECURE_NO_WARNINGS ///< This function or variable may be unsafe.
101 # define _SCL_SECURE_NO_WARNINGS ///< Function call with parameters that may be unsafe.
102 
103 # define __PRETTY_FUNCTION__ __FUNCSIG__
104 
105  typedef __int16 int16_t;
106 
107 # if defined(_MT) && !defined(_REENTRANT)
108 # define _REETRANT
109 # endif
110 
111 # define LIKELY(x) (x)
112 # define UNLIKELY(x) (x)
113 # define FORCE_INLINE
114 # define NEVER_INLINE
115 
116 # define POPCOUNTLL(x) __popcnt64(x)
117 
118  /**
119  Include "intrin.h" for this intrinsic. See <a href="https://msdn.microsoft.com/en-us/library/hh977023.aspx">MSDN Intrinsics</a> for more information.
120 
121  \param addr Prefetch a cache line from the address addr for reading.
122  \param locality Not used.
123  */
124 # define PREFETCH_READ(addr, locality) _m_prefetch(reinterpret_cast<void *>(addr))
125 
126  /**
127  Include "intrin.h" for this intrinsic. See <a href="https://msdn.microsoft.com/en-us/library/hh977023.aspx">MSDN Intrinsics</a> for more information.
128 
129  \param addr Prefetch a cache line from the address addr for writing.
130  \param locality Not used.
131  */
132 # define PREFETCH_WRITE(addr, locality) _m_prefetchw(reinterpret_cast<void *>(addr))
133 
134 #else
135 // Ensure that other 32-bit compilers can safely ignore unnecessary requirement in MSVC++-world for declaring functions with efficient calling conventions.
136 
137 # if defined(i386) || defined(__i386__) || defined(__x86_64__)
138 # include <x86intrin.h>
139 # endif
140 
141 # ifdef __GNU__
142 /// This is the general calling convention: use it anywhere.
143 # ifndef __fastcall
144 # define __fastcall __attribute__((regparm(3)))
145 # endif
146 
147 /// Calling convention necessary for constructors/destructors in MSVC.
148 # ifndef __stdcall
149 # define __stdcall __attribute__((regparm(3)))
150 # endif
151 
152 /// In MSVC++, the standard, slowest calling convention, used only for "main()" and other required places, e.g. var-arg functions.
153 # ifndef __cdecl
154 # define __cdecl __attribute__((cdecl))
155 # endif
156 
157 # define __CLR_OR_THIS_CALL __attribute__((regparm(3)))
158 # else
159 # ifndef __fastcall
160 # define __fastcall
161 # endif
162 
163 # ifndef __stdcall
164 # define __stdcall
165 # endif
166 
167 # ifndef __cdecl
168 # define __cdecl
169 # endif
170 
171 # define __CLR_OR_THIS_CALL
172 # endif
173 
174 # define __declspec(dllexport)
175 
176 /**
177  GCC has "quirks" in it's static branch-prediction: <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66573">Branch prediction in gcc</a>. Use these to ensure that if-statements are generated reliably.
178 */
179 # define LIKELY(x) __builtin_expect(!!(x), 1)
180 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
181 # define REALLY_FORCE_INLINE __attribute__((always_inline))
182 // Note that this can cause the compiler to take 100x longer...! And generate *HUGE* images. And create really slow code! BEWARE!
183 # define FORCE_INLINE
184 # define NEVER_INLINE __attribute__((cold, noinline))
185 
186 /// Yup: MSVC++ doesn't support "long long" properly.... Better use the MSVC++-special types.
187  typedef long long __int64;
188  typedef unsigned long long __uint64;
189 
190 # define POPCOUNTLL(x) __builtin_popcountll(x)
191 
192  /**
193  See <a href="https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Other-Builtins.html">GCC Intrinsics</a> and <a href="http://tomoyo.osdn.jp/cgi-bin/lxr/source/include/linux/prefetch.h">Prefetching strides</a> for more information.
194 
195  \param addr Prefetch a cache line from the address addr for reading. See the kernel marco L1_CACHE_BYTES for an approximation.
196  \param locality A compile-time integer constant in [1 .. 3], default 3. 1 means low locality, 3 highest.
197  */
198 # define PREFETCH_READ(addr, locality) __builtin_prefetch(reinterpret_cast<void const *>(addr), 0, (locality))
199 
200 # ifndef _MM_HINT_NTA
201  // May be on ARM...
202 # define _MM_HINT_NTA 0
203 # endif
204  /**
205  See <a href="https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Other-Builtins.html">GCC Intrinsics</a> and <a href="http://tomoyo.osdn.jp/cgi-bin/lxr/source/include/linux/prefetch.h">Prefetching strides</a> for more information.
206 
207  \param addr Prefetch a cache line from the address addr for writing. See the kernel marco L1_CACHE_BYTES for an approximation.
208  \param locality A compile-time integer constant in [1 .. 3], default 3. 1 means low locality, 3 highest.
209  */
210 # define PREFETCH_WRITE(addr, locality) __builtin_prefetch(reinterpret_cast<void const *>(addr), 1, (locality))
211 
212 #endif
213 
214 #define ALIGN_TO_L1_CACHE alignas(64 /* TODO std::hardware_constructive_interference_size */)
215 
216 #define LIBJMMCG_ENQUOTE_INTERNAL(x) #x
217 #define LIBJMMCG_ENQUOTE(x) LIBJMMCG_ENQUOTE_INTERNAL(x)
218 
219 #endif