irixKludges.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 #include "common/h/headers.h"
00032 #include "common/h/irixKludges.h"
00033 
00034 // copied from solarisKludges.C
00035 unsigned long long PDYN_div1000(unsigned long long in) {
00036    /* Divides by 1000 without an integer division instruction or library call, both of
00037     * which are slow.
00038     * We do only shifts, adds, and subtracts.
00039     *
00040     * We divide by 1000 in this way:
00041     * multiply by 1/1000, or multiply by (1/1000)*2^30 and then right-shift by 30.
00042     * So what is 1/1000 * 2^30?
00043     * It is 1,073,742.   (actually this is rounded)
00044     * So we can multiply by 1,073,742 and then right-shift by 30 (neat, eh?)
00045     *
00046     * Now for multiplying by 1,073,742...
00047     * 1,073,742 = (1,048,576 + 16384 + 8192 + 512 + 64 + 8 + 4 + 2)
00048     * or, slightly optimized:
00049     * = (1,048,576 + 16384 + 8192 + 512 + 64 + 16 - 2)
00050     * for a total of 8 shifts and 6 add/subs, or 14 operations.
00051     *
00052     */
00053 
00054    unsigned long long temp = in << 20; // multiply by 1,048,576
00055       // beware of overflow; left shift by 20 is quite a lot.
00056       // If you know that the input fits in 32 bits (4 billion) then
00057       // no problem.  But if it's much bigger then start worrying...
00058 
00059    temp += in << 14; // 16384
00060    temp += in << 13; // 8192
00061    temp += in << 9;  // 512
00062    temp += in << 6;  // 64
00063    temp += in << 4;  // 16
00064    temp -= in >> 2;  // 2
00065 
00066    return (temp >> 30); // divide by 2^30
00067 }
00068 
00069 // copied from solarisKludges.C
00070 unsigned long long PDYN_divMillion(unsigned long long in) {
00071    /* Divides by 1,000,000 without an integer division instruction or library call,
00072     * both of which are slow.
00073     * We do only shifts, adds, and subtracts.
00074     *
00075     * We divide by 1,000,000 in this way:
00076     * multiply by 1/1,000,000, or multiply by (1/1,000,000)*2^30 and then right-shift
00077     * by 30.  So what is 1/1,000,000 * 2^30?
00078     * It is 1,074.   (actually this is rounded)
00079     * So we can multiply by 1,074 and then right-shift by 30 (neat, eh?)
00080     *
00081     * Now for multiplying by 1,074
00082     * 1,074 = (1024 + 32 + 16 + 2)
00083     * for a total of 4 shifts and 4 add/subs, or 8 operations.
00084     *
00085     * Note: compare with div1000 -- it's cheaper to divide by a million than
00086     *       by a thousand (!)
00087     *
00088     */
00089 
00090    unsigned long long temp = in << 10; // multiply by 1024
00091       // beware of overflow...if the input arg uses more than 52 bits
00092       // than start worrying about whether (in << 10) plus the smaller additions
00093       // we're gonna do next will fit in 64...
00094 
00095    temp += in << 5; // 32
00096    temp += in << 4; // 16
00097    temp += in << 1; // 2
00098 
00099    return (temp >> 30); // divide by 2^30
00100 }
00101 
00102 // copied from solarisKludges.C
00103 unsigned long long PDYN_mulMillion(unsigned long long in) {
00104    unsigned long long result = in;
00105 
00106    /* multiply by 125 by multiplying by 128 and subtracting 3x */
00107    result = (result << 7) - result - result - result;
00108 
00109    /* multiply by 125 again, for a total of 15625x */
00110    result = (result << 7) - result - result - result;
00111 
00112    /* multiply by 64, for a total of 1,000,000x */
00113    result <<= 6;
00114 
00115    /* cost was: 3 shifts and 6 subtracts
00116     * cost of calling mul1000(mul1000()) would be: 6 shifts and 4 subtracts
00117     *
00118     * Another algorithm is to multiply by 2^6 and then 5^6.
00119     * The former is super-cheap (one shift); the latter is more expensive.
00120     * 5^6 = 15625 = 16384 - 512 - 256 + 8 + 1
00121     * so multiplying by 5^6 means 4 shift operations and 4 add/sub ops
00122     * so multiplying by 1000000 means 5 shift operations and 4 add/sub ops.
00123     * That may or may not be cheaper than what we're doing (3 shifts; 6 subtracts);
00124     * I'm not sure.  --ari
00125     */
00126 
00127    return result;
00128 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1