| 1 | // MMX Instruction Set |
| 2 | // Several suffixes are used to indicate what data size the instruction operates on: |
| 3 | // Byte (8 bits) |
| 4 | // Word (16 bits) |
| 5 | // Double word (32 bits) |
| 6 | // Quad word (64 bits) |
| 7 | // The signedness of the operation is also signified by the suffix: US for unsigned and S for signed. |
| 8 | // For example, PSUBUSB subtracts unsigned bytes, while PSUBSD subtracts signed double words. |
| 9 | // MMX defined over 40 new instructions, listed below. |
| 10 | // EMMS, MOVD, MOVQ, PACKSSDW, PACKSSWB, PACKUSWB, PADDB, PADDD, PADDSB, PADDSW, PADDUSB, PADDUSW, |
| 11 | // PADDW, PAND, PANDN, PCMPEQB, PCMPEQD, PCMPEQW, PCMPGTB, PCMPGTD, PCMPGTW, PMADDWD, PMULHW, PMULLW, |
| 12 | // POR, PSLLD, PSLLQ, PSLLW, PSRAD, PSRAW, PSRLD, PSRLQ, PSRLW, PSUBB, PSUBD, PSUBSB, PSUBSW, PSUBUSB, |
| 13 | // PSUBUSW, PSUBW, PUNPCKHBW, PUNPCKHDQ, PUNPCKHWD, PUNPCKLBW, PUNPCKLDQ, PUNPCKLWD, PXOR |
| 14 | |
| 15 | @[if amd64 && !tinyc && !msvc] |
| 16 | fn add_vectors_mmx(a &u8, b &u8, result &u8) { |
| 17 | unsafe { |
| 18 | asm volatile amd64 { |
| 19 | movq mm0, [a] // Load 8 bytes from a into MMX register mm0 |
| 20 | movq mm1, [b] // Load 8 bytes from b into MMX register mm1 |
| 21 | paddb mm0, mm1 // Add the two vectors using MMX instruction |
| 22 | movq [result], mm0 // Store the result back to memory |
| 23 | ; ; r (a) |
| 24 | r (b) |
| 25 | r (result) |
| 26 | ; mm0 |
| 27 | mm1 |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn main() { |
| 33 | a := [u8(1), 2, 3, 4, 5, 6, 7, 8] |
| 34 | b := [u8(8), 7, 6, 5, 4, 3, 2, 1] |
| 35 | result := []u8{len: 8} |
| 36 | add_vectors_mmx(&a[0], &b[0], &result[0]) |
| 37 | println(result) |
| 38 | assert result == [u8(9), 9, 9, 9, 9, 9, 9, 9] |
| 39 | } |
| 40 | |