X
X
XCemaXX2018-10-19 13:31:24
Operating Systems
XCemaXX, 2018-10-19 13:31:24

How does a computer system choose which set of instructions to use to execute code?

Consider x86 architecture, Windows OS.
A program written and compiled once can run on processors with different instruction sets and on different versions of Windows.
But how is it determined with which set of instructions this program will be executed? Those. what execution units to use.
I understood from the article that the compiler adds conditions on how to execute the program, according to the presence of instruction sets. The executable program checks these conditions before each run and selects the best instructions from those available. But this example is for the OS itself. Does it work the same for programs?
Is it necessary to specify in the program code itself that it is necessary to perform work using certain instructions? Can, for example, for fast processing of an array of floating-point data, the compiler itself specify the use of vector processing?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
#
#, 2018-10-19
@XCemaXX

doesn't choose anyway. selects the compiler, according to the parameters of the target platform. After compilation, no selection occurs. It should be noted that platforms such as .Net, JVM, node.js and browsers allow you to write relatively portable programs (and even then with limitations), but the virtual machines / engines themselves are still implemented for their platform at the compilation stage, and may even have significant differences not only in compilation options, but also in the source code for different platforms
upd

A program written and compiled once can run on processors with different instruction sets and on different versions of Windows.
such a program does not use all the instructions of the processor, but only a guaranteed compatible set of instructions. in the case of games, or design software, the package may include dll sets (if we are talking about Windows, or other methods of splitting into modules), optimized for different sets of instructions at the compilation stage, including for using the GPU, to achieve maximum speed. in this case, which module can be used, again, not all
computing system
and the main program (the one on the universal instruction set). and all this does not happen automatically in any way, but is laid down at the design stage and implemented in the program code. although to determine the type of processor and instruction set, there have long been standardized APIs, usually at the OS level

V
vaut, 2018-10-19
@vaut

Is it necessary to specify in the program code itself that it is necessary to perform work using certain instructions?

No, it is not necessary to specify this in the code, the compiler performs optimizations itself.
This is how it all happens.
Next, a little about how our harsh world works.
1) During execution, the code does not change, which instructions the compiler has indicated to do and those are executed.
2) When compiling, compatibility with which architectures is required is indicated, based on this, a different set of instructions is used.
3) And as a result, for compatibility, binaries are distributed assembled with the target ancient architecture and new instructions are not used. Well, almost. Sometimes in places with a high requirement for speed, developers bother and make sure that the code for your architecture is executed with all the optimizations.

S
stul5tul, 2018-10-19
@stul5tul

How is the processor selected? The processor may be in one or another mode of operation. But this is only used at the OS level and is not what you meant.
What you meant is not the processor, but the program itself chooses. There, the capabilities of the processor are determined and one or another branch of the algorithm is selected - with instructions for one or another type of processor.
Not with different instruction sets. And with different sets of instructions. Core, base - the same instructions.
As a result, the program does not need to adapt, with the exception of program sections involved in fast calculations - it makes sense to do the algorithm for specific instructions.
And then there is .NET - the program is initially compiled into code that does not depend on any processor.
This is how it works primarily for application programs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question