lib/bcd: optimize _bin2bcd() for improved performance
PATCH 1/1 lib/bcd: optimize _bin2bcd() for improved performance
[ Upstream commit cbf164cd44e06c78938b4a4a4479d3541779c319 ]
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.
--- include/bcd.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
include/bcd.h +3 -1
@@ -17,7 +17,9 @@ static inline unsigned int bcd2bin(unsigned int val)static inline unsigned int bin2bcd (unsigned int val) { -return (((val /10 ) <<4 ) | (val %10 )); +const unsigned int t = (val *103 ) >>10 ; + +return (t <<4 ) | (val - t *10 ); }#endif /* _BCD_H */
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Quoted 9 lines · Kuan-Wei Chiu
[ Upstream commit
cbf164cd44e06c78938b4a4a4479d3541779c319]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.[...]
Applied to u-boot/next, thanks!
[1/1] lib/bcd: optimize _bin2bcd() for improved performance
commit: 9ac621e671858bf0b80dd26b883f3781cc5acb34
Signature · 1 line
Tom