integer-division

How can I use bit shifting to replace integer division?

不羁的心 提交于 2019-11-28 08:24:42
I understand how to do it for powers of 2 so that's not my question. For example, if I want to find 5% of a number using a bit shift instead of an integer divide, how would i calculate that? So instead of (x * 20 / 19), I could do (x * 100 >> 11). Now this isn't right but it's close and I arrived at it using trial and error. How would I determine the most possible precise shift to use? High Performance Mark Best approach is to let the compiler do it for you. You simply write a/b in your language of choice, and the compiler generates the bit twiddling. EDIT (I hope you don't mind, i'm adding

The integer division algorithm of Intel's x86 processors

▼魔方 西西 提交于 2019-11-28 07:35:56
问题 Which integer division algorithm does Intel implement in their x86 processors? 回答1: Intel has a paper, Improvements in the Intel® Core™2 Processor Family Architecture and Microarchitecture, in which they discuss a number of different division algorithms. The first paragraph: The new Radix-16 floating-point divider with variable latency Radix-16 integer divide capability replaces the Merom Radix-4 floating point divide and Radix-2 square root and integer divide hardware. The preceding

C integer division and floor

≯℡__Kan透↙ 提交于 2019-11-28 07:01:11
In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes? Pete Becker a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int . floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part, and returns the result as a double. ouah floor returns a double while a / b where both a and b

How to compute 2⁶⁴/n in C?

时光毁灭记忆、已成空白 提交于 2019-11-28 03:21:09
问题 How to compute the integer division, 2 64 /n? Assuming: unsigned long is 64-bit We use a 64-bit CPU 1 < n < 2 64 If we do 18446744073709551616ul / n , we get warning: integer constant is too large for its type at compile time. This is because we cannot express 2 64 in a 64-bit CPU. Another way is the following: #define IS_POWER_OF_TWO(x) ((x & (x - 1)) == 0) unsigned long q = 18446744073709551615ul / n; if (IS_POWER_OF_TWO(n)) return q + 1; else return q; Is there any faster (CPU cycle) or

Why does this division result in zero?

为君一笑 提交于 2019-11-28 02:15:12
I was writing this code in C when I encountered the following problem. #include <stdio.h> int main() { int i=2; int j=3; int k,l; float a,b; k=i/j*j; l=j/i*i; a=i/j*j; b=j/i*i; printf("%d %d %f %f\n",k,l,a,b); return 0; } Can anyone tell me why the code is returning zero for the first and third variables ( k and a )? What I think you are experiencing is integer arithmetic . You correctly suppose l and b to be 2, but incorrectly assume that k and a will be 3 because it's the same operation. But it's not, it's integer arithmetic (rather than floating-point arithmetic). So when you do i / j

Unsigned 128-bit division on 64-bit machine

扶醉桌前 提交于 2019-11-27 22:34:37
I have a 128-bit number stored as 2 64-bit numbers ("Hi" and "Lo"). I need only to divide it by a 32-bit number. How could I do it, using the native 64-bit operations from CPU? (Please, note that I DO NOT need an arbitrary precision library. Just need to know how to make this simple division using native operations. Thank you). If you are storing the value (128-bits) using the largest possible native representation your architecture can handle (64-bits) you will have problems handling the intermediate results of the division (as you already found :) ). But you always can use a SMALLER

How does one do integer (signed or unsigned) division on ARM?

北慕城南 提交于 2019-11-27 22:05:24
I'm working on Cortex-A8 and Cortex-A9 in particular. I know that some architectures don't come with integer division, but what is the best way to do it other than convert to float, divide, convert to integer? Or is that indeed the best solution? Cheers! = ) The compiler normally includes a divide in its library, gcclib for example I have extracted them from gcc and use them directly: https://github.com/dwelch67/stm32vld/ then stm32f4d/adventure/gcclib going to float and back is probably not the best solution. you can try it and see how fast it is...This is a multiply but could as easily make

Integer division & modulo operation with negative operands in Python

泄露秘密 提交于 2019-11-27 15:40:50
Questions arise when I type in these expressions to Python 3.3.0 -10 // 3 # -4 -10 % 3 # 2 10 // -3 # -4 10 % -3 # -2 -10 // -3 # 3 It appears as though it takes the approximate floating point (-3.33)? and rounds down either way in integer division but in the modulo operation it does something totally different. It seems like it returns the remainder +/-1 and only switches the sign depending on where the negative operand is. I am utterly confused, even after looking over other answers on this site! I hope someone can clearly explain this too me! The book says hint: recall this magic formula a

Efficiently implementing floored / euclidean integer division

最后都变了- 提交于 2019-11-27 14:34:05
问题 Floored division is when the result is always floored down (towards −∞), not towards 0: Is it possible to efficiently implement floored or euclidean integer division in C/C++? (the obvious solution is to check the dividend's sign) 回答1: I'm revisiting this question five years later, as this is relevant for me too. I did some performance measurements on two pure-C versions and two inline-assembly versions for x86-64, and the results may be interesting. The tested variants of floored division

C: converting Farenheit to Celsius

不问归期 提交于 2019-11-27 09:53:23
int main (void) { int fahrenheit; // fahrenheit stands for fahrenheit double c; // c stands for celsius printf("Enter your fahrenheit, we'll covnvert it into celsius! "); scanf("%f", &fahrenheit); c = 5/9 * (fahrenheit - 32); printf("Here is your %f in celsius!.\n"); return (0); } I've followed the code through break points and when it takes in my input the calculations are off, but the formula is correct. Some sort of logic error I can't put my finger on. Please help! The scanf call uses the wrong format string. You are reading an int so you need it to be: scanf("%d", &fahrenheit); The