Skip to content

Commit

Permalink
lib/bcd: optimize _bin2bcd() for improved performance
Browse files Browse the repository at this point in the history
The original _bin2bcd() function used / 10 and % 10 operations for
conversion.  Although GCC optimizes these operations and does not generate
division or modulus instructions, the new implementation reduces the
number of mov instructions in the generated code for both x86-64 and ARM
architectures.

This optimization calculates the tens digit using (val * 103) >> 10, which
is accurate for values of 'val' in the range [0, 178].  Given that the
valid input range is [0, 99], this method ensures correctness while
simplifying the generated code.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Kuan-Wei Chiu <[email protected]>
Cc: Ching-Chun (Jim) Huang <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
visitorckw authored and akpm00 committed Sep 2, 2024
1 parent ccbfd2d commit cbf164c
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/bcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ EXPORT_SYMBOL(_bcd2bin);

unsigned char _bin2bcd(unsigned val)
{
return ((val / 10) << 4) + val % 10;
const unsigned int t = (val * 103) >> 10;

return (t << 4) | (val - t * 10);
}
EXPORT_SYMBOL(_bin2bcd);

0 comments on commit cbf164c

Please sign in to comment.