Stories
Slash Boxes
Comments

SoylentNews is people

posted by martyb on Saturday October 31 2015, @03:33AM   Printer-friendly
from the A|Journey|Through|the|CPU|Pipeline dept.

It is good for programmers to understand what goes on inside a processor. The CPU is at the heart of our career.

What goes on inside the CPU? How long does it take for one instruction to run? What does it mean when a new CPU has a 12-stage pipeline, or 18-stage pipeline, or even a "deep" 31-stage pipeline?

Programs generally treat the CPU as a black box. Instructions go into the box in order, instructions come out of the box in order, and some processing magic happens inside.

As a programmer, it is useful to learn what happens inside the box. This is especially true if you will be working on tasks like program optimization. If you don't know what is going on inside the CPU, how can you optimize for it?

A primer for those with a less formal background.


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 0) by Anonymous Coward on Saturday October 31 2015, @07:38AM

    by Anonymous Coward on Saturday October 31 2015, @07:38AM (#256822)

    Is there any hardware offering a way to optimize if you have a prior idea of which branch of an if/then will be correct? For example, Metropolis Hastings algorithms often target ~25% accept vs 75% reject steps.

  • (Score: 2) by shortscreen on Saturday October 31 2015, @08:37AM

    by shortscreen (2252) on Saturday October 31 2015, @08:37AM (#256828) Journal

    On old CPUs (old as in '80s) branching usually took more cycles than not branching. So you might arrange your code so that conditional branches would usually fail. Of course, at the end of your loop there is always a branch back to the beginning of the loop that is likely to be taken. So later on, there were some CPUs with a simplistic branch prediction that assumed backwards-pointing branches would be taken. There were also CPUs that avoided a pipeline stall by continuing to execute instructions following a branch instruction regardless of whether it ended up being taken (delayed branch).

    Pentium 4s have a "hint" opcode. Newer CPUs just ignore it though because they have so many other features to mitigate stalls that it's not even a big deal anymore.

    • (Score: 0) by Anonymous Coward on Saturday October 31 2015, @09:49PM

      by Anonymous Coward on Saturday October 31 2015, @09:49PM (#257003)

      Thanks, I was wondering if it possible for me to do something like this:

      If (x < y, 0.25)
      {
      A=5
      }else{
      A=1
      }

      If I know a priori that x is less than y about 25% of the time, it seems this info should be able to translate into some kind of performance improvement.

      • (Score: 2) by maxwell demon on Monday November 02 2015, @12:24AM

        by maxwell demon (1608) on Monday November 02 2015, @12:24AM (#257319) Journal

        There's a __builtin_expect in gcc which however doesn't use a percentage.

        --
        The Tao of math: The numbers you can count are not the real numbers.