Why did x86 designers (or other CPU architectures as well) decide not to include it? It is a logic gate that can be used to build other logic gates, thus it is fast as a single instruction. Rather than chaining not and and instructions (both are created from nand), why no nand instruction?.
|
|
|||
|
http://www.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.alangref/idalangref_nand_nd_instrs.htm : POWER has NAND. But generally modern CPUs are built to match automated code generation by compilers, and bitwise NAND is very rarely called for. Bitwise AND and OR get used more often for manipulating bitfields in data structures. In fact, SSE has AND-NOT but not NAND. Every instruction has a cost in the decode logic and consumes an opcode that could be used for something else. Especially in variable-length encodings like x86, you can run out of short opcodes and have to use longer ones, which potentially slows down all code. |
|||||||||||||||||||||
|
|
The cost of such an ALU functions is 1) the logic that performs the function itself 2) the selector that selects this function result instead of the others out of all ALU functions 3) the cost of having this option in the instruction set (and not having some other usefull function) I agree with you that the 1) cost is very small. The 2) and 3) cost however is almost independent of the function. I think in this case the 3) cost (the bits occupied in the instruction) were the reason not to have this specific instruction. Bits in an instruction are a very scarce resource for a CPU/architecture designer. |
||||
|
|
|
Turn it around - first see why Nand was popular in hardware logic design - it has several useful properties there. Then ask whether those properties still apply in a CPU instruction... TL/DR - they don't, so there's no downside to using And, Or or Not instead. The biggest advantage to hardwired Nand logic was speed, gained by reducing the number of logic levels (transistor stages) between a circuit's inputs and outputs. In a CPU, the clock speed is determined by the speed of much more complex operations like addition, so speeding up an AND operation won't enable you to increase clock rate. And the number of times you need to combine other instructions is vanishingly small - enough so that Nand really doesn't earn its space in the instrucnion set. |
|||
|
|
|
First off don't confuse bitwise and logical operations. Bitwise operations are usually used to set/clear/toggle/check bits in bitfields. None of these operations require nand ("and not", also known as "bit clear" is more useful). Logical operations in most modern programming languages are short-circuit. So usually a branch-based approach to implementing them is needed. Even when the compiler could prove that short-circuit vs complete evaluation makes no difference to program behaviour the operands for the logical operations are usually not in a conviniant form to implement the expression using the bitwise asm operations. |
|||
|
|
|
I'd like to agree with Brian here, and Wouter and pjc50. I'd also like to add that on general-purpose, especially CISC, processors, instructions don't all have the same throughputs – a complicated operation might simply take more cycles that an easy one. Consider X86: Input code:
Command to produce assembly:
Output Assembly (shortened):
As you can see, for the sub-64-sized data types, things are simply all handled as longs (hence the andl and notl), since that's the "native" bitwidth of my compiler, as it seems. The fact that there's For 64 bits, it's the same – just with "quad" (hence, trailing It seems that for 128 bit operands and larger, Intel didn't care to implement a "not" operation; instead, the compiler produces an all- In short: By implementing a complicated operation with multiple elementary instructions, you don't necessarily slow down operation – there's simply no advantage to having one instruction that does the job of multiple instructions if it isn't faster. |
||||
|
|
!(a & b)can be translated into a single instruction instead of 2. So, why not? It's just a few nand gates. – Amumu 23 hours agoBICinstruction, which isa & ~b. Arm Thumb-2 has theORNinstruction which is~(a | b). ARM is pretty modern. Encoding an instruction in the CPU instruction set has its costs. So only the most "useful" ones are making their way into ISA. – Eugene Sh. 22 hours ago~(((a << 1) | (b >> 1)) | 0x55555555)instruction too. The purpose would be so that~(((a << 1) | (b >> 1)) | 0x55555555)can be translated into a single instruction instead of 6. So, why not? – immibis 18 hours ago