duke@435: /* stefank@2314: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "libadt/port.hpp" stefank@2314: duke@435: // Code for portable compiling duke@435: duke@435: #ifdef __GNUC__ duke@435: #pragma implementation duke@435: #endif duke@435: duke@435: // %%%%% includes not needed with AVM framework - Ungar duke@435: // #include "port.hpp" duke@435: duke@435: // This is only used if turboc is used and it causes problems with duke@435: // gcc. duke@435: #ifdef __TURBOC__ duke@435: #include duke@435: #endif duke@435: duke@435: #include duke@435: duke@435: //------------------------------gcd-------------------------------------------- duke@435: // Greatest common divisor duke@435: uint32 gcd( register uint32 x, register uint32 y ) duke@435: { duke@435: register uint32 tmp; duke@435: while( x ) { // While not zero duke@435: tmp = x; // Hold onto smaller x value duke@435: x = y % x; // Compute modulus; since y>=x, 0 <= mod < x duke@435: y = tmp; // y = old x duke@435: } duke@435: return y; duke@435: } duke@435: duke@435: //----------------------------------------------------------------------------- duke@435: // Find first 1, or return 32 if empty duke@435: int ff1( uint32 mask ) duke@435: { duke@435: unsigned i, n = 0; duke@435: duke@435: for( i=1, n=0; i; i<<=1, n++) duke@435: if( mask&i ) return n; duke@435: return 32; duke@435: } duke@435: duke@435: //----------------------------------------------------------------------------- duke@435: // Find highest 1, or return 32 if empty duke@435: int fh1( uint32 mask ) duke@435: { duke@435: unsigned i, n = 0; duke@435: duke@435: for( i=((uint32)1<<31), n=31; i; i>>=1, n--) duke@435: if( mask&i ) return n; duke@435: return 32; duke@435: } duke@435: duke@435: //------------------------------rotate32--------------------------------------- duke@435: // Rotate 32bits. Postive rotates left (bits move toward high-order bit), duke@435: // negative rotates right. duke@435: uint32 rotate32( register uint32 x, register int32 cnt ) duke@435: { duke@435: if( cnt >= 0 ) { // Positive rotates left duke@435: cnt &= 31; // Mask off extra shift bits duke@435: } else { // Negative rotates right duke@435: cnt = (-cnt)&31; // Flip sign; mask extra shift bits duke@435: cnt = 32-cnt; // Rotate right by big left rotation duke@435: } duke@435: return (x << cnt) | (x >> (32-cnt)); duke@435: } duke@435: duke@435: /* Disabled - we have another log2 in the system. duke@435: This function doesn't work if used as substitute duke@435: for the existing log2. Keep around until we have duke@435: verified all uses of log2 do the correct thing! duke@435: //------------------------------log2------------------------------------------- duke@435: // Log base 2. Might also be called 'count leading zeros'. Log2(x) returns duke@435: // an l such that (1L<= 0 ) // While high bit is clear duke@435: sx <<= 1, l--; // Shift bits left, count down log2 duke@435: return l; duke@435: } duke@435: */ duke@435: duke@435: //------------------------------print------------------------------------------ duke@435: // Print a pointer without modifying the contents duke@435: #ifdef __TURBOC__ duke@435: ostream &ostream::operator << (const void *ptr) duke@435: { duke@435: return (*this) << "0x" << hex << (uint)ptr << dec; duke@435: } duke@435: #else duke@435: /*ostream &operator << (ostream &os, const void *ptr) duke@435: { duke@435: return os << "0x" << hex << (uint)ptr << dec; duke@435: }*/ duke@435: #endif