mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / extra / yassl / taocrypt / src / misc.cpp
blobb576d3d59d2f87bc1875952960ba1b90d4767c24
1 /*
2 Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to the
15 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16 MA 02110-1301 USA.
19 /* based on Wei Dai's misc.cpp from CryptoPP */
22 #include "runtime.hpp"
23 #include "misc.hpp"
26 #ifdef __GNUC__
27 #include <signal.h>
28 #include <setjmp.h>
29 #endif
31 #ifdef USE_SYS_STL
32 #include <algorithm>
33 #else
34 #include "algorithm.hpp"
35 #endif
37 namespace STL = STL_NAMESPACE;
40 #ifdef YASSL_PURE_C
42 void* operator new(size_t sz, TaoCrypt::new_t)
44 void* ptr = malloc(sz ? sz : 1);
45 if (!ptr) abort();
47 return ptr;
51 void operator delete(void* ptr, TaoCrypt::new_t)
53 if (ptr) free(ptr);
57 void* operator new[](size_t sz, TaoCrypt::new_t nt)
59 return ::operator new(sz, nt);
63 void operator delete[](void* ptr, TaoCrypt::new_t nt)
65 ::operator delete(ptr, nt);
69 /* uncomment to test
70 // make sure not using globals anywhere by forgetting to use overloaded
71 void* operator new(size_t sz);
73 void operator delete(void* ptr);
75 void* operator new[](size_t sz);
77 void operator delete[](void* ptr);
81 namespace TaoCrypt {
83 new_t tc; // for library new
87 #ifdef __sun
89 // Handler for pure virtual functions
90 namespace __Crun {
91 void pure_error() {
95 #endif
97 #if defined(__ICC) || defined(__INTEL_COMPILER) || (__GNUC__ > 2)
99 extern "C" {
101 int __cxa_pure_virtual() {
102 return 0;
105 } // extern "C"
107 #endif
109 #endif // YASSL_PURE_C
112 namespace TaoCrypt {
115 inline void XorWords(word* r, const word* a, unsigned int n)
117 for (unsigned int i=0; i<n; i++)
118 r[i] ^= a[i];
122 void xorbuf(byte* buf, const byte* mask, unsigned int count)
124 if (((size_t)buf | (size_t)mask | count) % WORD_SIZE == 0)
125 XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
126 else
128 for (unsigned int i=0; i<count; i++)
129 buf[i] ^= mask[i];
134 unsigned int BytePrecision(word value)
136 unsigned int i;
137 for (i=sizeof(value); i; --i)
138 if (value >> (i-1)*8)
139 break;
141 return i;
145 unsigned int BitPrecision(word value)
147 if (!value)
148 return 0;
150 unsigned int l = 0,
151 h = 8 * sizeof(value);
153 while (h-l > 1)
155 unsigned int t = (l+h)/2;
156 if (value >> t)
157 l = t;
158 else
159 h = t;
162 return h;
166 word Crop(word value, unsigned int size)
168 if (size < 8*sizeof(value))
169 return (value & ((1L << size) - 1));
170 else
171 return value;
176 #ifdef TAOCRYPT_X86ASM_AVAILABLE
179 bool HaveCpuId()
181 #ifdef _MSC_VER
182 __try
184 __asm
186 mov eax, 0
187 cpuid
190 __except (1)
192 return false;
194 return true;
195 #else
196 word32 eax, ebx;
197 __asm__ __volatile
199 /* Put EFLAGS in eax and ebx */
200 "pushf;"
201 "pushf;"
202 "pop %0;"
203 "movl %0,%1;"
205 /* Flip the cpuid bit and store back in EFLAGS */
206 "xorl $0x200000,%0;"
207 "push %0;"
208 "popf;"
210 /* Read EFLAGS again */
211 "pushf;"
212 "pop %0;"
213 "popf"
214 : "=r" (eax), "=r" (ebx)
216 : "cc"
219 if (eax == ebx)
220 return false;
221 return true;
222 #endif
226 void CpuId(word32 input, word32 *output)
228 #ifdef __GNUC__
229 __asm__
231 // save ebx in case -fPIC is being used
232 "push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx"
233 : "=a" (output[0]), "=D" (output[1]), "=c" (output[2]), "=d"(output[3])
234 : "a" (input)
236 #else
237 __asm
239 mov eax, input
240 cpuid
241 mov edi, output
242 mov [edi], eax
243 mov [edi+4], ebx
244 mov [edi+8], ecx
245 mov [edi+12], edx
247 #endif
251 bool IsPentium()
253 if (!HaveCpuId())
254 return false;
256 word32 cpuid[4];
258 CpuId(0, cpuid);
259 STL::swap(cpuid[2], cpuid[3]);
260 if (memcmp(cpuid+1, "GenuineIntel", 12) != 0)
261 return false;
263 CpuId(1, cpuid);
264 byte family = ((cpuid[0] >> 8) & 0xf);
265 if (family < 5)
266 return false;
268 return true;
273 static bool IsMmx()
275 if (!IsPentium())
276 return false;
278 word32 cpuid[4];
280 CpuId(1, cpuid);
281 if ((cpuid[3] & (1 << 23)) == 0)
282 return false;
284 return true;
288 bool isMMX = IsMmx();
291 #endif // TAOCRYPT_X86ASM_AVAILABLE
296 } // namespace