# CSCI 235 — Chapter 5

## What is hard?

Multiplication and division take a long time.

What about array index: `x[i]`? Usually, it does involve some sort of multiplication. What is the value of `x[i]` or `*&x[i]` or `&x+i` or `*(int *)(void *)(x+i*sizeof(*x))`?

### Intel x86_64

Assume register `%r10` contains address of x, register `%r11` contains i, and result should be stored in register `%r12`.

```     movl     (%r10,%r11,4), %r12
```

The Intel processer is a CISC, complex instruction set computer. However, just because it is one instruction, does not mean that it is fast!

### MIPS 32

Assume register `\$t5` contains address of x, register `%t6` contains i, and result should be stored in register `\$t7`.

```     sll       \$t7,\$t6,2
ld        \$t7,0(\$t7)
```

The MIPS processer is a RISC, reduced instruction set computer.

### IBM S/360

Assume register `R10` contains address of x, register `R11` contains i, and result should stored in register `R12`.

```     LR        R12,R11
LR        RR12,0(R11,R12)
```

The IBM S/360 processer is an ancient computer.

## Reduction of strength

How could a smart compiler reduce the cost of these instruction sequences?

```  int sum = 0 ;
for (int i=0; i<n; ++i) {
sum = sum + V[i] ;
}
// Use an integer pointer
```
```  for (int i=0; i<n; ++i) {
V[i] = 17*i ;
}
```
```  for (int i=0; i<n; ++i) {
V[i] = i*i ;
}
// Well, (n+1)^2 = n*2 + 2*n + 1 -- but it would require a programmer
```
```  for (int i=0; i<n; ++i) {
for (int j=0; j<n; ++j) {
if (i!=j) {
V[i][j] = 0 ;
} else {
V[i][j] = 1 ;
}
}
}
// Replace with two loops -- but this would probably require a programmer
```

## CMU 15-215 Video to view

This will not be easy because you haven’t seen Chapter 4. However, you will in Computer Systems II.