Content deleted Content added
→See also: add 2-column put more general bitmanip page in place of x86 specific one Tags: Mobile edit Mobile web edit Advanced mobile edit |
→Example of bit manipulation: add cross-ref examples in SWAR page Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
(5 intermediate revisions by the same user not shown) | |||
Line 9:
Integer arithmetic operators can also effect bit-operations in conjunction with the other operators.
Bit manipulation, in some cases, can obviate or reduce the need to loop over a data structure and can give manyfold speed-ups, as bit manipulations are processed in parallel. However if the operation is unusual or costly to [[Instruction_set_architecture#Instruction_set_implementation|implement]] its inclusion cannot be justified, inspiring innovative research.<ref>https://www.researchgate.net/publication/4217315_Performance_and_energy_benefits_of_instruction_set_extensions_in_an_FPGA_soft_core</ref>
== Terminology ==
Line 24:
== Example of bit manipulation ==
{{See also|SWAR#Examples}}
To determine if a number is a power of two, conceptually we may repeatedly do integer divide by two until the number won't divide by 2 evenly; if the only factor left is 1, the original number was a power of 2. Using bit and logical operators, there is a simple expression which will return true (1) or false (0):
Line 45 ⟶ 47:
{{see also|Bit Manipulation Instruction Sets}}
Processors typically provide only a subset of the useful bit operators. Programming languages don't directly support most bit operations, so idioms must be used to code them. The 'C' programming language, for example provides only bit-wise AND(&), OR(|), XOR(^) and NOT(~). Fortran provides AND(.and.), OR (.or.), XOR (.neqv.) and EQV(.eqv.).
Algol provides syntactic bitfield extract and insert. When languages provide bit operations that don't directly map to hardware instructions, compilers must synthesize the operation from available operators.
An especially useful bit operation is ''count leading zeros'' used to find the high set bit of a machine word, though it may have different names on various architectures.<ref>On most Intel chips, it's BSR (bitscan reverse), though newer SoCs also have LZCNT (count leading zeros)</ref> There's no simple programming language idiom, so it must be provided by a compiler intrinsic or system library routine. Without that operator, it is ''very'' expensive (see [[Find first set#CLZ]]) to do any operations with regard to the high bit of a word, due to the asymmetric carry-propagate of arithmetic operations. Fortunately, most cpu architectures have provided that since the middle 1980s. An accompanying operation ''count ones'', also called POPCOUNT, which counts the number of set bits in a machine word, is also usually provided as a hardware operator. Simpler bit operations like bit set, reset, test and toggle are often provided as hardware operators, but are easily simulated if they aren't - for example (SET R0, 1; LSHFT R0, i; OR x, R0) sets bit i in operand x.▼
▲An especially useful bit operation is ''count leading zeros'' used to find the high set bit of a machine word, though it may have different names on various architectures.<ref>On most Intel chips, it's BSR (bitscan reverse), though newer SoCs also have LZCNT (count leading zeros)</ref> There's no simple programming language idiom, so it must be provided by a compiler intrinsic or system library routine. Without that operator, it is ''very'' expensive (see [[Find first set#CLZ]]) to do any operations with regard to the high bit of a word, due to the asymmetric carry-propagate of arithmetic operations. Fortunately, most cpu architectures have provided that since the middle 1980s. An accompanying operation ''count ones'', also called
Simpler bit operations like bit set, reset, test and toggle are often provided as hardware operators, but are easily simulated if they aren't - for example (SET R0, 1; LSHFT R0, i; OR x, R0) sets bit i in operand x.
Some of the more useful and complex bit operations that must be coded as idioms in the programming language and synthesized by compilers include:
Line 59 ⟶ 65:
*bitfield scatter/gather operations which distribute contiguous portions of a bitfield over a machine word, or gather disparate bitfields in the word into a contiguous portion of a bitfield (see recent Intel PEXT/PDEP operators). Used by cryptography and video encoding.
*matrix inversion
Some arithmetic operations can be reduced to simpler operations and bit operations:
* reduce multiply by constant to sequence of shift-add
Multiply by 9 for example, is copy operand, shift up by 3 (multiply by 8), and add to original operand.
* reduce division by constant to sequence of shift-subtract
Bit-reversing, byte-reversing, and the various forms of rotation, can all be costly in terms of the number of instructions needed. Shifting has variants including Arithmetic shift, Logical shift and Rotate.
Unusual instructions include [[Binary-coded decimal#Packed BCD|Packed BCD]] which are very common in the financial, commercial and industrial industries.
=== Masking ===
|