bitwise 2 isas start
play

Bitwise 2 / ISAs (start) 1 Changelog Changes made in this version - PowerPoint PPT Presentation

Bitwise 2 / ISAs (start) 1 Changelog Changes made in this version not seen in fjrst lecture: 4 Feb 2019: exercise (near end): replace abcdef with uvwxyz in exercise + explanation to make it more obviously not hexadecimal 1 last time C nits:


  1. Bitwise 2 / ISAs (start) 1

  2. Changelog Changes made in this version not seen in fjrst lecture: 4 Feb 2019: exercise (near end): replace abcdef with uvwxyz in exercise + explanation to make it more obviously not hexadecimal 1

  3. last time C nits: structs, typedef, malloc/free, short-circuiting C traps: signed v unsigned; undefjned behavior bitshifting: exposing bit operations to software in hardware: one wire per bit — just ignore some wires? right shifts, arithmetic and logical left shifts (briefmy) 2

  4. lists from lists HW } node; x.ptr[0] x.ptr[1] x.ptr[2] 1 2 3 typedef struct node_t { short payload; struct node_t *next; ... short sentinel = -9999; x payload: 1 next: *x on stack or regs on heap ptr: len: 3 x ... short *x; x[3] = sentinel; ... x x[0] x[1] x[2] x[3] 3 typedef struct range_t { unsigned int length; short *ptr; } range; range x; 3 − 9999 1 2 x = malloc( sizeof ( short )*4); x.length = 3; x.ptr = malloc( sizeof ( short )*3); node *x; x = malloc( sizeof (node_t));

  5. lists from lists HW short payload; ptr: x.ptr[0] x.ptr[1] x.ptr[2] 1 2 3 typedef struct node_t { struct node_t *next; x } node; ... x payload: 1 next: *x or regs short sentinel = -9999; len: 3 ... x[0] x[1] x[2] short *x; x[3] = sentinel; ... x x[3] typedef struct range_t { unsigned int length; short *ptr; } range; range x; 3 ← on stack on heap → 3 − 9999 1 2 x = malloc( sizeof ( short )*4); x.length = 3; x.ptr = malloc( sizeof ( short )*3); node *x; x = malloc( sizeof (node_t));

  6. multiplying by 16 1 0 0 0 0 0 0 0 0 0xA 0 0 1 1 0 0 1 4 0xA × 16 = 0xA0

  7. shift left 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 instead: value << 4 value >> (-4) shr $-4, %reg 5 ✭✭✭✭✭✭✭✭✭✭✭✭ ❤❤❤❤❤❤❤❤❤❤❤❤ instead: shl $4, %reg (“ sh ift l eft”) ❤❤❤❤❤❤❤❤❤❤❤❤ ✭✭✭✭✭✭✭✭✭✭✭✭ 1 0 1 1 0

  8. shift left 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 instead: value << 4 value >> (-4) shr $-4, %reg 5 ✭✭✭✭✭✭✭✭✭✭✭✭ ❤❤❤❤❤❤❤❤❤❤❤❤ instead: shl $4, %reg (“ sh ift l eft”) ❤❤❤❤❤❤❤❤❤❤❤❤ ✭✭✭✭✭✭✭✭✭✭✭✭ 1 0 1 1 0

  9. shift left 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 x86 instruction: shl — shift left 0 … … … … 1 1 0 0 0 %reg (fjnal value) %reg (initial value) shl $ amount , %reg (or variable: shl %cl, %reg ) 6 1 0 1 1 0

  10. shift left 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 x86 instruction: shl — shift left 0 … … … … 1 1 0 0 0 %reg (fjnal value) %reg (initial value) shl $ amount , %reg (or variable: shl %cl, %reg ) 6 1 0 1 1 0

  11. left shift in math 1 << 0 == 1 << 1101 1000 1110 1100 1111 0110 0010 1000 10 << 2 == 40 0001 0100 10 << 1 == 20 0000 1010 10 << 0 == 10 0000 0100 1 << 2 == 4 0000 0010 1 << 1 == 2 0000 0001 7 − 10 << 0 == − 10 − 10 << 1 == − 20 − 10 << 2 == − 40

  12. left shift in math 10 << 1 == 20 1101 1000 1110 1100 1111 0110 0010 1000 10 << 2 == 40 1 << 0 == 1 0001 0100 0000 1010 10 << 0 == 10 0000 0100 1 << 2 == 4 0000 0010 1 << 1 == 2 0000 0001 7 − 10 << 0 == − 10 − 10 << 1 == − 20 − 10 << 2 == − 40 x << y = x × 2 y

  13. extracting nibble from more 0x } return (value % 256) / 16; unsigned extract_second_nibble( unsigned value) { } return (value / 16) % 16; unsigned extract_second_nibble( unsigned value) { // % -- remainder } return ???; extract_2nd( unsigned value) { unsigned 1 1 F 0 2 0 0 0 1 1 1 1 8 0 0 1 0 0 0 0 0

  14. extracting nibble from more 0x } return (value % 256) / 16; unsigned extract_second_nibble( unsigned value) { } return (value / 16) % 16; unsigned extract_second_nibble( unsigned value) { // % -- remainder } return ???; extract_2nd( unsigned value) { unsigned 1 1 F 0 2 0 0 0 1 1 1 1 8 0 0 1 0 0 0 0 0

  15. manipulating bits? easy to manipulate individual bits in HW how do we expose that to software? 9

  16. circuits: gates 0 0 0 1 1 0 1 1 0 1 1 0 10

  17. interlude: a truth table AND 0 1 0 0 0 1 0 1 AND with 1: keep a bit the same AND with 0: clear a bit method: construct “mask” of what to keep/remove 11

  18. interlude: a truth table AND 0 1 0 0 0 1 0 1 AND with 1: keep a bit the same AND with 0: clear a bit method: construct “mask” of what to keep/remove 11

  19. interlude: a truth table AND 0 1 0 0 0 1 0 1 AND with 1: keep a bit the same AND with 0: clear a bit method: construct “mask” of what to keep/remove 11

  20. interlude: a truth table AND 0 1 0 0 0 1 0 1 AND with 1: keep a bit the same AND with 0: clear a bit method: construct “mask” of what to keep/remove 11

  21. bitwise AND — & 0 … 1 0 1 0 & … 1 0 1 1 … 0 0 1 0 0 0 Treat value as array of bits 0 1 & 1 == 1 1 & 0 == 0 0 & 0 == 0 2 & 4 == 0 10 & 7 == 2 … 0 1 0 0 & … 0 1 0 0 … 12

  22. bitwise AND — & 0 … 1 0 1 0 & … 1 0 1 1 … 0 0 1 0 0 0 Treat value as array of bits 0 1 & 1 == 1 1 & 0 == 0 0 & 0 == 0 2 & 4 == 0 10 & 7 == 2 … 0 1 0 0 & … 0 1 0 0 … 12

  23. bitwise AND — & 0 … 1 0 1 0 & … 1 0 1 1 … 0 0 1 0 0 0 Treat value as array of bits 0 1 & 1 == 1 1 & 0 == 0 0 & 0 == 0 2 & 4 == 0 10 & 7 == 2 … 0 1 0 0 & … 0 1 0 0 … 12

  24. bitwise AND — C/assembly x86: and %reg, %reg C: foo & bar 13

  25. bitwise hardware ( 10 & 7 == 2 ) 1 0 0 1 0 0 1 1 1 10 0 1 0 . . . 7 14

  26. extract 0x3 from 0x1234 unsigned get_second_nibble1_bitwise( unsigned value) { return (value >> 4) & 0xF; // 0xF: 00001111 // like (value / 16) % 16 } unsigned get_second_nibble2_bitwise( unsigned value) { return (value & 0xF0) >> 4; // 0xF0: 11110000 // like (value % 256) / 16; } 15

  27. extract 0x3 from 0x1234 get_second_nibble1_bitwise: movl %edi, %eax shrl $4, %eax andl $0xF, %eax ret get_second_nibble2_bitwise: movl %edi, %eax andl $0xF0, %eax shrl $4, %eax ret 16

  28. and/or/xor 0 1 0 0 1 1 1 & XOR conditionally clear bit conditionally keep bit | conditionally set bit ^ conditionally fmip bit 0 1 AND 0 0 1 0 0 0 1 1 1 OR 0 1 0 0 1 1 17

  29. bitwise OR — | … 1 1 1 1 … 1 1 1 0 | 1 | 1 == 1 0 1 0 1 … 10 | 7 == 15 2 | 4 == 6 0 | 0 == 0 1 | 0 == 1 18

  30. bitwise xor — … 1 0 1 1 … 1 1 1 0 ^ ̂ 0 1 0 1 … 10 ^ 7 == 13 2 ^ 4 == 6 0 ^ 0 == 0 1 ^ 0 == 1 1 ^ 1 == 0 19

  31. negation / not — ~ 0 32 bits 1 1 1 1 … 1 1 0 0 0 ~ (‘complement’) is bitwise version of ! : … 0 0 ~ ~(( unsigned ) 2) == 0xFFFFFFFD 3 ) ~2 == ( int ) 0xFFFFFFFD (aka !notZero == 0 !0 == 1 20 ~0 == ( int ) 0xFFFFFFFF (aka − 1 )

  32. negation / not — ~ 0 32 bits 1 1 1 1 … 1 1 0 0 ~ (‘complement’) is bitwise version of ! : 0 … 0 0 ~ ~(( unsigned ) 2) == 0xFFFFFFFD !notZero == 0 !0 == 1 20 ~0 == ( int ) 0xFFFFFFFF (aka − 1 ) ~2 == ( int ) 0xFFFFFFFD (aka − 3 )

  33. negation / not — ~ 0 32 bits 1 1 1 1 … 1 1 0 0 ~ (‘complement’) is bitwise version of ! : 0 … 0 0 ~ ~(( unsigned ) 2) == 0xFFFFFFFD !notZero == 0 !0 == 1 20 ~0 == ( int ) 0xFFFFFFFF (aka − 1 ) ~2 == ( int ) 0xFFFFFFFD (aka − 3 )

  34. bit-puzzles future assignment bit manipulation puzzles solve some problem with bitwise ops maybe that you could do with normal arithmetic, comparisons, etc. why? good for thinking about HW design good for understanding bitwise ops unreasonably common interview question type 21

  35. note: ternary operator w = (x ? y : z) if (x) { w = y; } else { w = z; } 22

  36. one-bit ternary (x ? y : z) constraint: x, y, and z are 0 or 1 now: reimplement in C without if/else/ || /etc. (assembly: no jumps probably) divide-and-conquer: (x ? y : 0) (x ? 0 : z) 23

  37. one-bit ternary (x ? y : z) constraint: x, y, and z are 0 or 1 now: reimplement in C without if/else/ || /etc. (assembly: no jumps probably) divide-and-conquer: (x ? y : 0) (x ? 0 : z) 23

  38. one-bit ternary parts (1) constraint: x, y, and z are 0 or 1 (x ? y : 0) y=0 y=1 x=0 0 0 x=1 0 1 (x & y) 24

  39. one-bit ternary parts (1) constraint: x, y, and z are 0 or 1 (x ? y : 0) y=0 y=1 x=0 0 0 x=1 0 1 24 → (x & y)

  40. one-bit ternary parts (2) (x ? y : 0) = (x & y) (x ? 0 : z) opposite x : ~x ((~x) & z) 25

  41. one-bit ternary parts (2) (x ? y : 0) = (x & y) (x ? 0 : z) opposite x : ~x ((~x) & z) 25

  42. one-bit ternary constraint: x, y, and z are 0 or 1 (x ? y : z) (x ? y : 0) | (x ? 0 : z) (x & y) | ((~x) & z) 26

  43. multibit ternary constraint: x is 0 or 1 old solution ((x & y) | (~x) & z) only gets least sig. bit (x ? y : z) (x ? y : 0) | (x ? 0 : z) (( x) & y) | (( (x ^ 1)) & z) 27

  44. multibit ternary constraint: x is 0 or 1 old solution ((x & y) | (~x) & z) only gets least sig. bit (x ? y : z) (x ? y : 0) | (x ? 0 : z) (( x) & y) | (( (x ^ 1)) & z) 27

Recommend


More recommend