| 1 | #! /usr/bin/awk -f |
| 2 | # |
| 3 | # Generate test vectors for MPX testing |
| 4 | |
| 5 | # --- Generate an `l'-byte hex number --- |
| 6 | |
| 7 | function r(l, i, s, x) |
| 8 | { |
| 9 | if (!l) l = len; |
| 10 | s = ""; |
| 11 | for (i = 0; i < l; i++) { |
| 12 | x = int(rand() * 256); |
| 13 | s = s sprintf("%02X", x); |
| 14 | } |
| 15 | return (s); |
| 16 | } |
| 17 | |
| 18 | # --- Main code --- |
| 19 | |
| 20 | BEGIN { |
| 21 | |
| 22 | # --- Initialization --- |
| 23 | |
| 24 | i = 1; |
| 25 | if (i in ARGV) len = ARGV[i++]; else len = 32; |
| 26 | if (i in ARGV) op = ARGV[i++]; else op = "+"; |
| 27 | if (i in ARGV) rep = ARGV[i++]; else rep = 1; |
| 28 | |
| 29 | # --- Output filters --- |
| 30 | # |
| 31 | # This is complicated. `bc' emits numbers split over multiple lines with |
| 32 | # backslashes. It also doesn't pad to an even number of digits, which the |
| 33 | # test rig is expecting, or use lower-case, which looks nicer. |
| 34 | # |
| 35 | # The first bit matches a line ending with a backslash. If it finds one, |
| 36 | # it appends the next line, removes the backslash/newline pair, and loops |
| 37 | # around to the top. |
| 38 | # |
| 39 | # The next substitution translates the whole kaboodle into lower-case. |
| 40 | # |
| 41 | # The next one looks for an off number of hex digits and prepends a zero if |
| 42 | # it finds one. |
| 43 | # |
| 44 | # The one after that just indents by two spaces. The final one sticks a |
| 45 | # semicolon on the end. |
| 46 | |
| 47 | bc = "bc | sed '\ |
| 48 | :top\n\ |
| 49 | /\\\\$/ {\n\ |
| 50 | N\n\ |
| 51 | s/\\\\\\\n\ |
| 52 | //;\n\ |
| 53 | b top;\n\ |
| 54 | }\n\ |
| 55 | y/ABCDEF/abcdef/\n\ |
| 56 | s/^[0-9a-f]\\([0-9a-f][0-9a-f]\\)*$/0&/\n\ |
| 57 | s/^/ /\n\ |
| 58 | $ s/$/;/'"; |
| 59 | out = "sed 'y/ABCDEF/abcdef/; s/^/ /'" |
| 60 | |
| 61 | # --- Main code --- |
| 62 | |
| 63 | srand(); |
| 64 | |
| 65 | while (rep--) { |
| 66 | x = r(); |
| 67 | |
| 68 | print "obase = 16" | bc; |
| 69 | print "ibase = 16" | bc; |
| 70 | |
| 71 | # --- Shifting operations --- |
| 72 | |
| 73 | if (op == "<<" || op == ">>") { |
| 74 | y = int(rand() * len * 4) + int(rand() * len * 4); |
| 75 | rop = (op == "<<" ? "*" : "/"); |
| 76 | z = sprintf("%X", y); |
| 77 | print x, y | out; |
| 78 | print x, rop, "(2 ^ " z ")" | bc; |
| 79 | } |
| 80 | |
| 81 | # --- Division --- |
| 82 | |
| 83 | else if (op == "/") { |
| 84 | ylen = int(rand() * len) + 1; |
| 85 | y = r(ylen); |
| 86 | print x | out; |
| 87 | print y | out; |
| 88 | print x, "/", y | bc; |
| 89 | print x, "%", y | bc; |
| 90 | } |
| 91 | |
| 92 | # --- Squaring --- |
| 93 | |
| 94 | else if (op == "2") { |
| 95 | print x | out; |
| 96 | print x, "*", x | bc; |
| 97 | } |
| 98 | |
| 99 | # --- Other operations --- |
| 100 | |
| 101 | else { |
| 102 | y = r(); |
| 103 | if (op == "-" && x < y) { |
| 104 | t = x; x = y; y = t; |
| 105 | } |
| 106 | print x | out; |
| 107 | print y | out; |
| 108 | print x, op, y | bc; |
| 109 | } |
| 110 | |
| 111 | close(out); |
| 112 | close(bc); |
| 113 | if (rep) |
| 114 | print; |
| 115 | } |
| 116 | |
| 117 | exit 0; |
| 118 | } |