a4424771ced2e46ffc3b983c7c026f32fcf90485
[novacoin.git] / src / util.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_UTIL_H
6 #define BITCOIN_UTIL_H
7
8 #include "uint256.h"
9
10 #ifndef WIN32
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <sys/resource.h>
14 #else
15 typedef int pid_t; /* define for windows compatiblity */
16 #endif
17 #include <map>
18 #include <vector>
19 #include <string>
20
21 #include <boost/thread.hpp>
22 #include <boost/filesystem.hpp>
23 #include <boost/filesystem/path.hpp>
24 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
25 #include <boost/interprocess/sync/scoped_lock.hpp>
26 #include <boost/interprocess/sync/interprocess_condition.hpp>
27 #include <boost/interprocess/sync/lock_options.hpp>
28 #include <boost/date_time/gregorian/gregorian_types.hpp>
29 #include <boost/date_time/posix_time/posix_time_types.hpp>
30
31 #include <openssl/sha.h>
32 #include <openssl/ripemd.h>
33
34 #include "netbase.h" // for AddTimeData
35 #include "ui_interface.h"
36
37 typedef long long  int64;
38 typedef unsigned long long  uint64;
39
40 static const int64 COIN = 100000000;
41 static const int64 CENT = 1000000;
42
43 #define loop                for (;;)
44 #define BEGIN(a)            ((char*)&(a))
45 #define END(a)              ((char*)&((&(a))[1]))
46 #define UBEGIN(a)           ((unsigned char*)&(a))
47 #define UEND(a)             ((unsigned char*)&((&(a))[1]))
48 #define ARRAYLEN(array)     (sizeof(array)/sizeof((array)[0]))
49 #define printf              OutputDebugStringF
50
51 #ifdef snprintf
52 #undef snprintf
53 #endif
54 #define snprintf my_snprintf
55
56 #ifndef PRI64d
57 #if defined(_MSC_VER) || defined(__MSVCRT__)
58 #define PRI64d  "I64d"
59 #define PRI64u  "I64u"
60 #define PRI64x  "I64x"
61 #else
62 #define PRI64d  "lld"
63 #define PRI64u  "llu"
64 #define PRI64x  "llx"
65 #endif
66 #endif
67
68 // This is needed because the foreach macro can't get over the comma in pair<t1, t2>
69 #define PAIRTYPE(t1, t2)    std::pair<t1, t2>
70
71 // Align by increasing pointer, must have extra space at end of buffer
72 template <size_t nBytes, typename T>
73 T* alignup(T* p)
74 {
75     union
76     {
77         T* ptr;
78         size_t n;
79     } u;
80     u.ptr = p;
81     u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
82     return u.ptr;
83 }
84
85 #ifdef WIN32
86 #define MSG_NOSIGNAL        0
87 #define MSG_DONTWAIT        0
88
89 #ifndef S_IRUSR
90 #define S_IRUSR             0400
91 #define S_IWUSR             0200
92 #endif
93 #define unlink              _unlink
94 #else
95 #define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d)
96 #define strlwr(psz)         to_lower(psz)
97 #define _strlwr(psz)        to_lower(psz)
98 #define MAX_PATH            1024
99 inline void Sleep(int64 n)
100 {
101     /*Boost has a year 2038 problem— if the request sleep time is past epoch+2^31 seconds the sleep returns instantly.
102       So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/
103     boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n));
104 }
105 #endif
106
107
108
109
110
111
112
113
114
115 extern std::map<std::string, std::string> mapArgs;
116 extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
117 extern bool fDebug;
118 extern bool fPrintToConsole;
119 extern bool fPrintToDebugger;
120 extern bool fRequestShutdown;
121 extern bool fShutdown;
122 extern bool fDaemon;
123 extern bool fServer;
124 extern bool fCommandLine;
125 extern std::string strMiscWarning;
126 extern bool fTestNet;
127 extern bool fNoListen;
128 extern bool fLogTimestamps;
129
130 void RandAddSeed();
131 void RandAddSeedPerfmon();
132 int OutputDebugStringF(const char* pszFormat, ...);
133 int my_snprintf(char* buffer, size_t limit, const char* format, ...);
134
135 /* It is not allowed to use va_start with a pass-by-reference argument.
136    (C++ standard, 18.7, paragraph 3). Use a dummy argument to work around this, and use a
137    macro to keep similar semantics.
138 */
139 std::string real_strprintf(const std::string &format, int dummy, ...);
140 #define strprintf(format, ...) real_strprintf(format, 0, __VA_ARGS__)
141
142 bool error(const char *format, ...);
143 void LogException(std::exception* pex, const char* pszThread);
144 void PrintException(std::exception* pex, const char* pszThread);
145 void PrintExceptionContinue(std::exception* pex, const char* pszThread);
146 void ParseString(const std::string& str, char c, std::vector<std::string>& v);
147 std::string FormatMoney(int64 n, bool fPlus=false);
148 bool ParseMoney(const std::string& str, int64& nRet);
149 bool ParseMoney(const char* pszIn, int64& nRet);
150 std::vector<unsigned char> ParseHex(const char* psz);
151 std::vector<unsigned char> ParseHex(const std::string& str);
152 bool IsHex(const std::string& str);
153 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
154 std::string DecodeBase64(const std::string& str);
155 std::string EncodeBase64(const unsigned char* pch, size_t len);
156 std::string EncodeBase64(const std::string& str);
157 void ParseParameters(int argc, const char*const argv[]);
158 bool WildcardMatch(const char* psz, const char* mask);
159 bool WildcardMatch(const std::string& str, const std::string& mask);
160 int GetFilesize(FILE* file);
161 boost::filesystem::path GetDefaultDataDir();
162 const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
163 boost::filesystem::path GetConfigFile();
164 boost::filesystem::path GetPidFile();
165 void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
166 bool ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
167 bool GetStartOnSystemStartup();
168 bool SetStartOnSystemStartup(bool fAutoStart);
169 void ShrinkDebugFile();
170 int GetRandInt(int nMax);
171 uint64 GetRand(uint64 nMax);
172 int64 GetTime();
173 void SetMockTime(int64 nMockTimeIn);
174 int64 GetAdjustedTime();
175 std::string FormatFullVersion();
176 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments);
177 void AddTimeData(const CNetAddr& ip, int64 nTime);
178
179
180
181
182
183
184
185
186
187
188
189 /** Wrapped boost mutex: supports recursive locking, but no waiting  */
190 typedef boost::interprocess::interprocess_recursive_mutex CCriticalSection;
191
192 /** Wrapped boost mutex: supports waiting but not recursive locking */
193 typedef boost::interprocess::interprocess_mutex CWaitableCriticalSection;
194
195 #ifdef DEBUG_LOCKORDER
196 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
197 void LeaveCritical();
198 #else
199 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
200 void static inline LeaveCritical() {}
201 #endif
202
203 /** Wrapper around boost::interprocess::scoped_lock */
204 template<typename Mutex>
205 class CMutexLock
206 {
207 private:
208     boost::interprocess::scoped_lock<Mutex> lock;
209 public:
210
211     void Enter(const char* pszName, const char* pszFile, int nLine)
212     {
213         if (!lock.owns())
214         {
215             EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
216 #ifdef DEBUG_LOCKCONTENTION
217             if (!lock.try_lock())
218             {
219                 printf("LOCKCONTENTION: %s\n", pszName);
220                 printf("Locker: %s:%d\n", pszFile, nLine);
221             }
222 #endif
223             lock.lock();
224         }
225     }
226
227     void Leave()
228     {
229         if (lock.owns())
230         {
231             lock.unlock();
232             LeaveCritical();
233         }
234     }
235
236     bool TryEnter(const char* pszName, const char* pszFile, int nLine)
237     {
238         if (!lock.owns())
239         {
240             EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
241             lock.try_lock();
242             if (!lock.owns())
243                 LeaveCritical();
244         }
245         return lock.owns();
246     }
247
248     CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::interprocess::defer_lock)
249     {
250         if (fTry)
251             TryEnter(pszName, pszFile, nLine);
252         else
253             Enter(pszName, pszFile, nLine);
254     }
255
256     ~CMutexLock()
257     {
258         if (lock.owns())
259             LeaveCritical();
260     }
261
262     operator bool()
263     {
264         return lock.owns();
265     }
266
267     boost::interprocess::scoped_lock<Mutex> &GetLock()
268     {
269         return lock;
270     }
271 };
272
273 typedef CMutexLock<CCriticalSection> CCriticalBlock;
274 typedef CMutexLock<CWaitableCriticalSection> CWaitableCriticalBlock;
275 typedef boost::interprocess::interprocess_condition CConditionVariable;
276
277 /** Wait for a given condition inside a WAITABLE_CRITICAL_BLOCK */
278 #define WAIT(name,condition) \
279    do { while(!(condition)) { (name).wait(waitablecriticalblock.GetLock()); } } while(0)
280
281 /** Notify waiting threads that a condition may hold now */
282 #define NOTIFY(name) \
283    do { (name).notify_one(); } while(0)
284
285 #define NOTIFY_ALL(name) \
286    do { (name).notify_all(); } while(0)
287
288 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
289 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
290 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
291 #define WAITABLE_LOCK(cs) CWaitableCriticalBlock waitablecriticalblock(cs, #cs, __FILE__, __LINE__)
292
293 #define ENTER_CRITICAL_SECTION(cs) \
294     { \
295         EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
296         (cs).lock(); \
297     }
298
299 #define LEAVE_CRITICAL_SECTION(cs) \
300     { \
301         (cs).unlock(); \
302         LeaveCritical(); \
303     }
304
305
306 // This is exactly like std::string, but with a custom allocator.
307 // (secure_allocator<> is defined in serialize.h)
308 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
309
310
311
312
313
314 inline std::string i64tostr(int64 n)
315 {
316     return strprintf("%"PRI64d, n);
317 }
318
319 inline std::string itostr(int n)
320 {
321     return strprintf("%d", n);
322 }
323
324 inline int64 atoi64(const char* psz)
325 {
326 #ifdef _MSC_VER
327     return _atoi64(psz);
328 #else
329     return strtoll(psz, NULL, 10);
330 #endif
331 }
332
333 inline int64 atoi64(const std::string& str)
334 {
335 #ifdef _MSC_VER
336     return _atoi64(str.c_str());
337 #else
338     return strtoll(str.c_str(), NULL, 10);
339 #endif
340 }
341
342 inline int atoi(const std::string& str)
343 {
344     return atoi(str.c_str());
345 }
346
347 inline int roundint(double d)
348 {
349     return (int)(d > 0 ? d + 0.5 : d - 0.5);
350 }
351
352 inline int64 roundint64(double d)
353 {
354     return (int64)(d > 0 ? d + 0.5 : d - 0.5);
355 }
356
357 inline int64 abs64(int64 n)
358 {
359     return (n >= 0 ? n : -n);
360 }
361
362 template<typename T>
363 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
364 {
365     if (itbegin == itend)
366         return "";
367     const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
368     const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
369     std::string str;
370     str.reserve((pend-pbegin) * (fSpaces ? 3 : 2));
371     for (const unsigned char* p = pbegin; p != pend; p++)
372         str += strprintf((fSpaces && p != pend-1 ? "%02x " : "%02x"), *p);
373     return str;
374 }
375
376 inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=false)
377 {
378     return HexStr(vch.begin(), vch.end(), fSpaces);
379 }
380
381 template<typename T>
382 void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true)
383 {
384     printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
385 }
386
387 inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
388 {
389     printf(pszFormat, HexStr(vch, fSpaces).c_str());
390 }
391
392 inline int64 GetPerformanceCounter()
393 {
394     int64 nCounter = 0;
395 #ifdef WIN32
396     QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
397 #else
398     timeval t;
399     gettimeofday(&t, NULL);
400     nCounter = t.tv_sec * 1000000 + t.tv_usec;
401 #endif
402     return nCounter;
403 }
404
405 inline int64 GetTimeMillis()
406 {
407     return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
408             boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
409 }
410
411 inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime)
412 {
413     time_t n = nTime;
414     struct tm* ptmTime = gmtime(&n);
415     char pszTime[200];
416     strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
417     return pszTime;
418 }
419
420 template<typename T>
421 void skipspaces(T& it)
422 {
423     while (isspace(*it))
424         ++it;
425 }
426
427 inline bool IsSwitchChar(char c)
428 {
429 #ifdef WIN32
430     return c == '-' || c == '/';
431 #else
432     return c == '-';
433 #endif
434 }
435
436 /**
437  * Return string argument or default value
438  *
439  * @param strArg Argument to get (e.g. "-foo")
440  * @param default (e.g. "1")
441  * @return command-line argument or default value
442  */
443 std::string GetArg(const std::string& strArg, const std::string& strDefault);
444
445 /**
446  * Return integer argument or default value
447  *
448  * @param strArg Argument to get (e.g. "-foo")
449  * @param default (e.g. 1)
450  * @return command-line argument (0 if invalid number) or default value
451  */
452 int64 GetArg(const std::string& strArg, int64 nDefault);
453
454 /**
455  * Return boolean argument or default value
456  *
457  * @param strArg Argument to get (e.g. "-foo")
458  * @param default (true or false)
459  * @return command-line argument or default value
460  */
461 bool GetBoolArg(const std::string& strArg, bool fDefault=false);
462
463 /**
464  * Set an argument if it doesn't already have a value
465  *
466  * @param strArg Argument to set (e.g. "-foo")
467  * @param strValue Value (e.g. "1")
468  * @return true if argument gets set, false if it already had a value
469  */
470 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
471
472 /**
473  * Set a boolean argument if it doesn't already have a value
474  *
475  * @param strArg Argument to set (e.g. "-foo")
476  * @param fValue Value (e.g. false)
477  * @return true if argument gets set, false if it already had a value
478  */
479 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
480
481
482
483
484
485
486
487
488
489 // Randomize the stack to help protect against buffer overrun exploits
490 #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn)     \
491     {                                           \
492         static char nLoops;                     \
493         if (nLoops <= 0)                        \
494             nLoops = GetRand(20) + 1;           \
495         if (nLoops-- > 1)                       \
496         {                                       \
497             ThreadFn;                           \
498             return;                             \
499         }                                       \
500     }
501
502
503 template<typename T1>
504 inline uint256 Hash(const T1 pbegin, const T1 pend)
505 {
506     static unsigned char pblank[1];
507     uint256 hash1;
508     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
509     uint256 hash2;
510     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
511     return hash2;
512 }
513
514 template<typename T1, typename T2>
515 inline uint256 Hash(const T1 p1begin, const T1 p1end,
516                     const T2 p2begin, const T2 p2end)
517 {
518     static unsigned char pblank[1];
519     uint256 hash1;
520     SHA256_CTX ctx;
521     SHA256_Init(&ctx);
522     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
523     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
524     SHA256_Final((unsigned char*)&hash1, &ctx);
525     uint256 hash2;
526     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
527     return hash2;
528 }
529
530 template<typename T1, typename T2, typename T3>
531 inline uint256 Hash(const T1 p1begin, const T1 p1end,
532                     const T2 p2begin, const T2 p2end,
533                     const T3 p3begin, const T3 p3end)
534 {
535     static unsigned char pblank[1];
536     uint256 hash1;
537     SHA256_CTX ctx;
538     SHA256_Init(&ctx);
539     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
540     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
541     SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
542     SHA256_Final((unsigned char*)&hash1, &ctx);
543     uint256 hash2;
544     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
545     return hash2;
546 }
547
548 template<typename T>
549 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
550 {
551     // Most of the time is spent allocating and deallocating CDataStream's
552     // buffer.  If this ever needs to be optimized further, make a CStaticStream
553     // class with its buffer on the stack.
554     CDataStream ss(nType, nVersion);
555     ss.reserve(10000);
556     ss << obj;
557     return Hash(ss.begin(), ss.end());
558 }
559
560 inline uint160 Hash160(const std::vector<unsigned char>& vch)
561 {
562     uint256 hash1;
563     SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
564     uint160 hash2;
565     RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
566     return hash2;
567 }
568
569
570 /** Median filter over a stream of values. 
571  * Returns the median of the last N numbers
572  */
573 template <typename T> class CMedianFilter
574 {
575 private:
576     std::vector<T> vValues;
577     std::vector<T> vSorted;
578     int nSize;
579 public:
580     CMedianFilter(int size, T initial_value):
581         nSize(size)
582     {
583         vValues.reserve(size);
584         vValues.push_back(initial_value);
585         vSorted = vValues;
586     }
587     
588     void input(T value)
589     {
590         if(vValues.size() == nSize)
591         {
592             vValues.erase(vValues.begin());
593         }
594         vValues.push_back(value);
595
596         vSorted.resize(vValues.size());
597         std::copy(vValues.begin(), vValues.end(), vSorted.begin());
598         std::sort(vSorted.begin(), vSorted.end());
599     }
600
601     T median() const
602     {
603         int size = vSorted.size();
604         assert(size>0);
605         if(size & 1) // Odd number of elements
606         {
607             return vSorted[size/2];
608         }
609         else // Even number of elements
610         {
611             return (vSorted[size/2-1] + vSorted[size/2]) / 2;
612         }
613     }
614
615     int size() const
616     {
617         return vValues.size();
618     }
619
620     std::vector<T> sorted () const
621     {
622         return vSorted;
623     }
624 };
625
626
627
628
629
630
631
632
633
634
635 // Note: It turns out we might have been able to use boost::thread
636 // by using TerminateThread(boost::thread.native_handle(), 0);
637 #ifdef WIN32
638 typedef HANDLE pthread_t;
639
640 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
641 {
642     DWORD nUnused = 0;
643     HANDLE hthread =
644         CreateThread(
645             NULL,                        // default security
646             0,                           // inherit stack size from parent
647             (LPTHREAD_START_ROUTINE)pfn, // function pointer
648             parg,                        // argument
649             0,                           // creation option, start immediately
650             &nUnused);                   // thread identifier
651     if (hthread == NULL)
652     {
653         printf("Error: CreateThread() returned %d\n", GetLastError());
654         return (pthread_t)0;
655     }
656     if (!fWantHandle)
657     {
658         CloseHandle(hthread);
659         return (pthread_t)-1;
660     }
661     return hthread;
662 }
663
664 inline void SetThreadPriority(int nPriority)
665 {
666     SetThreadPriority(GetCurrentThread(), nPriority);
667 }
668 #else
669 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
670 {
671     pthread_t hthread = 0;
672     int ret = pthread_create(&hthread, NULL, (void*(*)(void*))pfn, parg);
673     if (ret != 0)
674     {
675         printf("Error: pthread_create() returned %d\n", ret);
676         return (pthread_t)0;
677     }
678     if (!fWantHandle)
679     {
680         pthread_detach(hthread);
681         return (pthread_t)-1;
682     }
683     return hthread;
684 }
685
686 #define THREAD_PRIORITY_LOWEST          PRIO_MAX
687 #define THREAD_PRIORITY_BELOW_NORMAL    2
688 #define THREAD_PRIORITY_NORMAL          0
689 #define THREAD_PRIORITY_ABOVE_NORMAL    0
690
691 inline void SetThreadPriority(int nPriority)
692 {
693     // It's unclear if it's even possible to change thread priorities on Linux,
694     // but we really and truly need it for the generation threads.
695 #ifdef PRIO_THREAD
696     setpriority(PRIO_THREAD, 0, nPriority);
697 #else
698     setpriority(PRIO_PROCESS, 0, nPriority);
699 #endif
700 }
701
702 inline void ExitThread(size_t nExitCode)
703 {
704     pthread_exit((void*)nExitCode);
705 }
706 #endif
707
708
709
710
711
712 inline uint32_t ByteReverse(uint32_t value)
713 {
714     value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
715     return (value<<16) | (value>>16);
716 }
717
718 #endif
719