mirror of
https://source.denx.de/u-boot/u-boot.git
synced 2026-01-15 21:51:57 +01:00
[ 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. Link: https://lkml.kernel.org/r/20240812170229.229380-1-visitorckw@gmail.com Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Cc: Ching-Chun Huang (Jim) <jserv@ccns.ncku.edu.tw> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> [visitorckw@gmail.com: Adapt to bin2bcd() in include/bcd.h] Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
26 lines
585 B
C
26 lines
585 B
C
/* Permission is hereby granted to copy, modify and redistribute this code
|
|
* in terms of the GNU Library General Public License, Version 2 or later,
|
|
* at your option.
|
|
*/
|
|
|
|
/* inline functions to translate to/from binary and binary-coded decimal
|
|
* (frequently found in RTC chips).
|
|
*/
|
|
|
|
#ifndef _BCD_H
|
|
#define _BCD_H
|
|
|
|
static inline unsigned int bcd2bin(unsigned int val)
|
|
{
|
|
return ((val) & 0x0f) + ((val & 0xff) >> 4) * 10;
|
|
}
|
|
|
|
static inline unsigned int bin2bcd(unsigned int val)
|
|
{
|
|
const unsigned int t = (val * 103) >> 10;
|
|
|
|
return (t << 4) | (val - t * 10);
|
|
}
|
|
|
|
#endif /* _BCD_H */
|