Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hexdump: do a few calculations ahead

Instead of doing calculations in each case of different groupsize let's do
them beforehand. While there, change the switch to an if-else-if
construction.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andy Shevchenko and committed by
Linus Torvalds
5d909c8d 6f6f3fcb

+10 -24
+10 -24
lib/hexdump.c
··· 103 103 bool ascii) 104 104 { 105 105 const u8 *ptr = buf; 106 + int ngroups; 106 107 u8 ch; 107 108 int j, lx = 0; 108 109 int ascii_column; ··· 115 114 goto nil; 116 115 if (len > rowsize) /* limit to one line at a time */ 117 116 len = rowsize; 117 + if (!is_power_of_2(groupsize) || groupsize > 8) 118 + groupsize = 1; 118 119 if ((len % groupsize) != 0) /* no mixed size output */ 119 120 groupsize = 1; 120 121 121 - switch (groupsize) { 122 - case 8: { 122 + ngroups = len / groupsize; 123 + ascii_column = rowsize * 2 + rowsize / groupsize + 1; 124 + if (groupsize == 8) { 123 125 const u64 *ptr8 = buf; 124 - int ngroups = len / groupsize; 125 126 126 127 for (j = 0; j < ngroups; j++) 127 128 lx += scnprintf(linebuf + lx, linebuflen - lx, 128 129 "%s%16.16llx", j ? " " : "", 129 130 (unsigned long long)*(ptr8 + j)); 130 - ascii_column = rowsize * 2 + rowsize / 8 + 2; 131 - break; 132 - } 133 - 134 - case 4: { 131 + } else if (groupsize == 4) { 135 132 const u32 *ptr4 = buf; 136 - int ngroups = len / groupsize; 137 133 138 134 for (j = 0; j < ngroups; j++) 139 135 lx += scnprintf(linebuf + lx, linebuflen - lx, 140 136 "%s%8.8x", j ? " " : "", *(ptr4 + j)); 141 - ascii_column = rowsize * 2 + rowsize / 4 + 2; 142 - break; 143 - } 144 - 145 - case 2: { 137 + } else if (groupsize == 2) { 146 138 const u16 *ptr2 = buf; 147 - int ngroups = len / groupsize; 148 139 149 140 for (j = 0; j < ngroups; j++) 150 141 lx += scnprintf(linebuf + lx, linebuflen - lx, 151 142 "%s%4.4x", j ? " " : "", *(ptr2 + j)); 152 - ascii_column = rowsize * 2 + rowsize / 2 + 2; 153 - break; 154 - } 155 - 156 - default: 143 + } else { 157 144 for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) { 158 145 ch = ptr[j]; 159 146 linebuf[lx++] = hex_asc_hi(ch); ··· 150 161 } 151 162 if (j) 152 163 lx--; 153 - 154 - ascii_column = 3 * rowsize + 2; 155 - break; 156 164 } 157 165 if (!ascii) 158 166 goto nil; 159 167 160 - while (lx < (linebuflen - 1) && lx < (ascii_column - 1)) 168 + while (lx < (linebuflen - 1) && lx < ascii_column) 161 169 linebuf[lx++] = ' '; 162 170 for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) { 163 171 ch = ptr[j];