MCPcopy Create free account
hub / github.com/Tripwire/tripwire-open-source / ShortDivide

Method ShortDivide

src/cryptlib/integer.cpp:1943–1980  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1941}
1942
1943word Integer::ShortDivide(Integer &quotient, const Integer &dividend, word divisor)
1944{
1945#ifdef THROW_EXCEPTIONS
1946 if (!divisor)
1947 throw Integer::DivideByZero();
1948#endif
1949
1950 assert(divisor);
1951
1952 if ((divisor & (divisor-1)) == 0) // divisor is a power of 2
1953 {
1954 quotient = dividend >> (BitPrecision(divisor)-1);
1955 return dividend.reg[(unsigned int)0] & (divisor-1);
1956 }
1957
1958 unsigned int i = dividend.WordCount();
1959 quotient.reg.CleanNew(RoundupSize(i));
1960 word remainder = 0;
1961 while (i--)
1962 {
1963 quotient.reg[i] = word(MAKE_DWORD(dividend.reg[i], remainder) / divisor);
1964 remainder = word(MAKE_DWORD(dividend.reg[i], remainder) % divisor);
1965 }
1966
1967 if (dividend.NotNegative())
1968 quotient.sign = POSITIVE;
1969 else
1970 {
1971 quotient.sign = NEGATIVE;
1972 if (remainder)
1973 {
1974 --quotient;
1975 remainder = divisor - remainder;
1976 }
1977 }
1978
1979 return remainder;
1980}
1981
1982Integer operator/(const Integer &a, word b)
1983{

Callers

nothing calls this directly

Calls 6

DivideByZeroClass · 0.85
BitPrecisionFunction · 0.85
RoundupSizeFunction · 0.85
WordCountMethod · 0.80
CleanNewMethod · 0.80
NotNegativeMethod · 0.80

Tested by

no test coverage detected