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,
19 /* based on Wei Dai's misc.cpp from CryptoPP */
22 #include "runtime.hpp"
34 #include "algorithm.hpp"
37 namespace STL
= STL_NAMESPACE
;
42 void* operator new(size_t sz
, TaoCrypt::new_t
)
44 void* ptr
= malloc(sz
? sz
: 1);
51 void operator delete(void* ptr
, TaoCrypt::new_t
)
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
);
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);
83 new_t tc
; // for library new
89 // Handler for pure virtual functions
97 #if defined(__ICC) || defined(__INTEL_COMPILER) || (__GNUC__ > 2)
101 int __cxa_pure_virtual() {
109 #endif // YASSL_PURE_C
115 inline void XorWords(word
* r
, const word
* a
, unsigned int n
)
117 for (unsigned int i
=0; i
<n
; 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
);
128 for (unsigned int i
=0; i
<count
; i
++)
134 unsigned int BytePrecision(word value
)
137 for (i
=sizeof(value
); i
; --i
)
138 if (value
>> (i
-1)*8)
145 unsigned int BitPrecision(word value
)
151 h
= 8 * sizeof(value
);
155 unsigned int t
= (l
+h
)/2;
166 word
Crop(word value
, unsigned int size
)
168 if (size
< 8*sizeof(value
))
169 return (value
& ((1L << size
) - 1));
176 #ifdef TAOCRYPT_X86ASM_AVAILABLE
199 /* Put EFLAGS in eax and ebx */
205 /* Flip the cpuid bit and store back in EFLAGS */
210 /* Read EFLAGS again */
214 : "=r" (eax
), "=r" (ebx
)
226 void CpuId(word32 input
, word32
*output
)
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])
259 STL::swap(cpuid
[2], cpuid
[3]);
260 if (memcmp(cpuid
+1, "GenuineIntel", 12) != 0)
264 byte family
= ((cpuid
[0] >> 8) & 0xf);
281 if ((cpuid
[3] & (1 << 23)) == 0)
288 bool isMMX
= IsMmx();
291 #endif // TAOCRYPT_X86ASM_AVAILABLE