summaryrefslogtreecommitdiffstats
path: root/lib/bcd.c
diff options
context:
space:
mode:
authorKuan-Wei Chiu <visitorckw@gmail.com>2024-08-12 19:02:29 +0200
committerAndrew Morton <akpm@linux-foundation.org>2024-09-02 05:43:33 +0200
commitcbf164cd44e06c78938b4a4a4479d3541779c319 (patch)
tree92fbe8efae109927b1c172177447e9cb6b11ccc4 /lib/bcd.c
parentdrm/xe: clean up fault injection usage (diff)
downloadlinux-cbf164cd44e06c78938b4a4a4479d3541779c319.tar.xz
linux-cbf164cd44e06c78938b4a4a4479d3541779c319.zip
lib/bcd: optimize _bin2bcd() for improved performance
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/20240812170229.229380-1-visitorckw@gmail.com Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Cc: Ching-Chun (Jim) Huang <jserv@ccns.ncku.edu.tw> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'lib/bcd.c')
-rw-r--r--lib/bcd.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/lib/bcd.c b/lib/bcd.c
index 7e4750b6e801..c5e79ba9cd7b 100644
--- a/lib/bcd.c
+++ b/lib/bcd.c
@@ -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);