views:

318

answers:

4

One thing I lack understanding on is how can code compiled for an Intel CPU work on an AMD CPU.

My base understanding is that if you compile C code, for example, the compiler turns the source code into machine language which will have instructions for a particular processor. So, you need to compile with a compiler for whatever platform you want your application to use. Why don't you require a compiler for AMD versus Intel, and have to buy software for a particular processor?

I know that AMD processors and Intel processors share some compatibility in the older x86 instructions; what I'd like to know is how do they do that? I mean, it's not like AMD or Intel are calling each other up and telling each other their trade secrets, so how can they create instruction sets that are compatible?

Is all compatibility still based on the 386 instruction set, with a bunch of statements like "IF AMD CPU, do this ELSE IF INTEL do that"?

+17  A: 

AMD and Intel x86 and x86-64 CPUs are almost entirely compatible. They both implement all of x86 and x86-64. They each have their own extensions (like MMX and 3DNow), but compilers don't generally use them unless you tell them to. Nowadays, AMD and Intel both support almost all of each other's extensions. The only time you may have to worry about the difference is when you are doing really low-level kernel stuff.

By the way, the instruction set isn't really a "trade secret." The implementation is. Both companies produce documentation on the instruction set, so each can copy it. The reason for this is probably that if more companies produce compatible chips, more software will be written for this instruction set, so more people will buy these chips. x86/x86-64 is basically the standard architecture for desktops, guaranteeing that people will continue to buy Intel and AMD chips. Intel and AMD have very different implementations, they just execute the same code.

Zifre
So, along with bdonlan's answer.. instruction sets are publicly published by Intel or AMD, then the other creates a chip that supports the same (or some extension thereof) of that instruction set? The differences then lie within the specific hardware implementation of those instructions. Is that about right?
romandas
@romandas: Yes, your understanding is correct. The chip makers document the instruction set, allowing others to copy it.
Zifre
I suggest adding that information into your answer, or having bdonlan add yours to theirs, so I can accept the merged answer as definitive.
romandas
@romandas: I have edited my answer to include that information.
Zifre
Accepted; thanks!
romandas
The instruction set is publicly documented not only so that other companies can create a similar chip, but, more importantly, so that programmers can write code for that CPU
Nathan Fellman
+5  A: 

Actually they do have a cross-licensing agreement. Information and innovation flowed from Intel to AMD back in the 90's when AMD was a second source of 386 and 486 processors, and then from AMD to Intel when Intel adopted the x86-64 extensions.

florin
+6  A: 

Remember also that the instruction sets for these processors are publicly documented; typically one vendor will invent a new set of instructions, and a generation or two later the other vendor will implement compatible extensions. As such, rather than IF 386 .... you have something more along the lines of IF SSE4 IS SUPPORTED ..., without regard to the specific vendor.

bdonlan
Right, these are typically referred to as advanced CPUID queries.
John Gietzen
+1 for good supporting answer; I had Zifre add this information to their answer.
romandas
+1  A: 

Both vendors implement the same instruction set, so when your compiled code gets turned into machine instructions, they will work on both chips. However, there are some special instruction sets such as SSE that can be specific to one vendor or another. If you want to improve performance for an application, you can make runtime checks to use these features when available.

Tim Rupe