How to do 64-bit by 32-bit division? [duplicate] - arm

This question already has an answer here:
64bit/32bit division faster algorithm for ARM / NEON?
(1 answer)
Closed 5 years ago.
I use IAR with STM32 and I need 64-bit arithmetic. How to implement 32-bit array for 64-bit arithmetic?
For example I have a 64 bit value 0x3B5456DF32 stored in a 32-bit array
A[0]= 0x3B
A[1] = 0x5456DF32
I have to divide it by B = 0x3216523

There are so many questions like this before. Please do a search and look for the most appropriate answer for you before asking
64 bit by 32 bit division
Signed 64 by 32 integer division
64 bit division
64/32-bit division on a processor with 32/16-bit division
64bit/32bit division faster algorithm for ARM / NEON?
How does one do integer (signed or unsigned) division on ARM?

Related

What happens when I apply the unary "-" operator to an unsigned integer? [duplicate]

This question already has answers here:
Assigning negative numbers to an unsigned int?
(14 answers)
Closed 6 years ago.
This should be a pretty simple question but I can't seem to find the answer in my textbook and can't find the right keywords to find it online.
What does it mean when you have a negative sign in front of an unsigned int?
Specifically, if x is an unsigned int equal to 1, what is the bit value of -x?
Per the C standard, arithmetic for unsigned integers is performed modulo 2bit width. So, for a 32-bit integer, the negation will be taken mod 232 = 4294967296.
For a 32-bit number, then, the value you'll get if you negate a number n is going to be 0-n = 4294967296-n. In your specific case, assuming unsigned int is 32 bits wide, you'd get 4294967296-1 = 4294967295 = 0xffffffff (the number with all bits set).
The relevant text in the C standard is in §6.2.5/9:
a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type
It will overflow in the negative direction, i.e. if your int is 16 bits x will be 65535. The bit value will be 1111111111111111 (16 ones)
If int is 32 bits, x will be 4294967295
when you apply the "-", the Two's complement of the integer is stored in variable. see here for details

Computable Size of Integer [duplicate]

This question already has answers here:
What is “two's complement”?
(24 answers)
Closed 7 years ago.
An int32 is represented in computer memory with a size of 4 bytes (32 bits).
So, 32 bits have 1 sign bit and 31 data bits. But if 1st bit starts at 2^0, then the 31st bit should have 2^30, and the last bit is of course the sign bit.
How is it then that integer extends from -2^31 to (2^31)-1?
So, 32 bits have 1 sign bit and 31 data bits.
No. Most platforms use two's complement to represent integers.
This avoids double zero (+- 0) and instead extends the range of negative numbers by 1. The main advantage is in arithmetic: Many operations, like addition and subtraction can simply ignore the sign.
An int32 has exactly 32 bits, and can hold 2^32 different values.
In unsigned form, these will be 0 -> (2^32)-1.
In signed form, these will be -2^31 -> (2^31)-1. Notice that:
(0 - (-2^31)) + ((2^31)-1 - 0) =
2^31 + 2^31 - 1 =
2*2^31 - 1 =
(2^32) - 1
Exactly the same range.

Number of bits in an integer [duplicate]

This question already has answers here:
Is int in C Always 32-bit?
(8 answers)
Is the size of C "int" 2 bytes or 4 bytes?
(13 answers)
Closed 9 years ago.
The number of bits in an integer in C is compiler and machine dependent. What is meant by this? Does the number of bits in an int vary with different C compilers and different processor architecture? Can you illustrate what it means?
This wikipedia article gives a good overview: http://en.wikipedia.org/wiki/Word_(data_type)
Types such as integers are represented in hardware. Hardware changes, and so do the size of certain types. The more bits in a type, the larger the number (for integers) or more precision you can store (for floating-point types).
There are some types which specifically specify the number of bits, such as int16.
It means exactly what it says and what you said in your own words.
For example, on some compilers and with some platforms, an int is 32 bits, on other compilers and platforms an int is 64 bits.
I remember long ago when I was programming on the Commodore Amiga, there were two different C compilers available from two different manufacturers. On one compiler, an int was 16 bits, on the other compiler an int was 32 bits.
You can use sizeof to determine how many bytes an int is on your compiler.

What C standard says about bitwise XOR of two signed integers? [duplicate]

This question already has answers here:
Are the results of bitwise operations on signed integers defined?
(4 answers)
Closed 9 years ago.
Which ANSI C standard says something about bit wise XOR of two signed integers? I tried to read the standard, but it is vast.
XOR of two signed integers is valid as per C standard? What will happen to the sign bit of the result?
Bitwise operations operate on bits, that is, on the actual 1's and 0's of the data. Therefore, sign is irrelevant; the result of the operation is what you'd get from writing out the series of 1's and 0's that represents each integer and then XORing each corresponding pair of bits. (Even type information is irrelevant, as long as the operands are integers of some sort, as opposed to doubles, pointers, etc.)

why X86 provides pair of division and multiply instructions?

I noticed that, unsigned int and int shared the same instruction for addition and subtract. But provides idivl / imull for integer division and mutiply, divl / mull for unsigned int . May I know the underlying reason for this ?
The results are different when you multiply or divide, depending on whether your arguments are signed or unsigned.
It's really the magic of two's complement that allows us to use the same operation for signed and unsigned addition and subtraction. This is not true in other representations -- ones' complement and sign-magnitude both use a different addition and subtraction algorithm than unsigned arithmetic does.
For example, with 32-bit words, -1 is represented by 0xffffffff. Squaring this, you get different results for signed and unsigned versions:
Signed: -1 * -1 = 1 = 0x00000000 00000001
Unsigned: 0xffffffff * 0xffffffff = 0xfffffffe 00000001
Note that the low word of the result is the same. On processors that don't give you the high bits, there is only one multiplication instruction necessary. On PPC, there are three multiplication instructions — one for the low bits, and two for the high bits depending on whether the operands are signed or unsigned.
Most microprocessors implement multiplication and division with shift-and-add algorithm (or a similar algorithm. This of course requires that the sign of the operands be handled separately.
While implementing multiplication and divisions with add-an-substract would have allowed to not worry about sign and hence allowed to handle signed vs. unsigned integer values interchangeably, it is much less efficient algorithm and that's likely why it wasn't used.
I just read that some modern CPUs use alternatively the Booth encoding method, but that algorithm also implies asserting the sign of the values.
In x86 sign store in high bit of word (if will talk about integer and unsigned integer)
ADD and SUB command use one algorithm for signed and unsigned in - it get correct result in both.
For MULL and DIV this is not worked. And you should "tell" to CPU what int you want "use" signed or unsigned.
For unsigned use MULL and DIV. It just operate words - it is fast.
For signed use MULL and IDIV. It get word to absolute (positive) value, store sign for result and then make operation. This is slower than MULL and DIV.

Resources