Skip to content

Commit

Permalink
Merge pull request #891 from kupsch/remove-muldivfuncs
Browse files Browse the repository at this point in the history
code cleanup integer funcs to * and / by constants
  • Loading branch information
kupsch committed Oct 19, 2020
2 parents 5d525e5 + 7dec653 commit 260318c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 97 deletions.
93 changes: 0 additions & 93 deletions common/src/linuxKludges.C
Original file line number Diff line number Diff line change
Expand Up @@ -106,99 +106,6 @@ int P_copy(const char *from, const char *to) {
}


unsigned long long PDYN_div1000(unsigned long long in) {
/* Divides by 1000 without an integer division instruction or library call, both of
* which are slow.
* We do only shifts, adds, and subtracts.
*
* We divide by 1000 in this way:
* multiply by 1/1000, or multiply by (1/1000)*2^30 and then right-shift by 30.
* So what is 1/1000 * 2^30?
* It is 1,073,742. (actually this is rounded)
* So we can multiply by 1,073,742 and then right-shift by 30 (neat, eh?)
*
* Now for multiplying by 1,073,742...
* 1,073,742 = (1,048,576 + 16384 + 8192 + 512 + 64 + 8 + 4 + 2)
* or, slightly optimized:
* = (1,048,576 + 16384 + 8192 + 512 + 64 + 16 - 2)
* for a total of 8 shifts and 6 add/subs, or 14 operations.
*
*/

unsigned long long temp = in << 20; // multiply by 1,048,576
// beware of overflow; left shift by 20 is quite a lot.
// If you know that the input fits in 32 bits (4 billion) then
// no problem. But if it's much bigger then start worrying...

temp += in << 14; // 16384
temp += in << 13; // 8192
temp += in << 9; // 512
temp += in << 6; // 64
temp += in << 4; // 16
temp -= in >> 2; // 2

return (temp >> 30); // divide by 2^30
}

unsigned long long PDYN_divMillion(unsigned long long in) {
/* Divides by 1,000,000 without an integer division instruction or library call,
* both of which are slow.
* We do only shifts, adds, and subtracts.
*
* We divide by 1,000,000 in this way:
* multiply by 1/1,000,000, or multiply by (1/1,000,000)*2^30 and then right-shift
* by 30. So what is 1/1,000,000 * 2^30?
* It is 1,074. (actually this is rounded)
* So we can multiply by 1,074 and then right-shift by 30 (neat, eh?)
*
* Now for multiplying by 1,074
* 1,074 = (1024 + 32 + 16 + 2)
* for a total of 4 shifts and 4 add/subs, or 8 operations.
*
* Note: compare with div1000 -- it's cheaper to divide by a million than
* by a thousand (!)
*
*/

unsigned long long temp = in << 10; // multiply by 1024
// beware of overflow...if the input arg uses more than 52 bits
// than start worrying about whether (in << 10) plus the smaller additions
// we're gonna do next will fit in 64...

temp += in << 5; // 32
temp += in << 4; // 16
temp += in << 1; // 2

return (temp >> 30); // divide by 2^30
}

unsigned long long PDYN_mulMillion(unsigned long long in) {
unsigned long long result = in;

/* multiply by 125 by multiplying by 128 and subtracting 3x */
result = (result << 7) - result - result - result;

/* multiply by 125 again, for a total of 15625x */
result = (result << 7) - result - result - result;

/* multiply by 64, for a total of 1,000,000x */
result <<= 6;

/* cost was: 3 shifts and 6 subtracts
* cost of calling mul1000(mul1000()) would be: 6 shifts and 4 subtracts
*
* Another algorithm is to multiply by 2^6 and then 5^6.
* The former is super-cheap (one shift); the latter is more expensive.
* 5^6 = 15625 = 16384 - 512 - 256 + 8 + 1
* so multiplying by 5^6 means 4 shift operations and 4 add/sub ops
* so multiplying by 1000000 means 5 shift operations and 4 add/sub ops.
* That may or may not be cheaper than what we're doing (3 shifts; 6 subtracts);
* I'm not sure. --ari
*/

return result;
}

#include <cxxabi.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/lock_guard.hpp>
Expand Down
4 changes: 0 additions & 4 deletions common/src/linuxKludges.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@

#include <vector>

unsigned long long PDYN_div1000(unsigned long long in);
unsigned long long PDYN_divMillion(unsigned long long in);
unsigned long long PDYN_mulMillion(unsigned long long in);

COMMON_EXPORT bool PtraceBulkRead(Dyninst::Address inTraced, unsigned size, void *inSelf, int pid);

COMMON_EXPORT bool PtraceBulkWrite(Dyninst::Address inTraced, unsigned size, const void *inSelf, int pid);
Expand Down

0 comments on commit 260318c

Please sign in to comment.