1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ 2 #include <libpldm/bcd.h> 3 4 LIBPLDM_ABI_STABLE pldm_bcd_bcd2dec8(uint8_t bcd)5uint8_t pldm_bcd_bcd2dec8(uint8_t bcd) 6 { 7 return (bcd >> 4) * 10 + (bcd & 0x0f); 8 } 9 10 LIBPLDM_ABI_STABLE pldm_bcd_dec2bcd8(uint8_t dec)11uint8_t pldm_bcd_dec2bcd8(uint8_t dec) 12 { 13 return ((dec / 10) << 4) + (dec % 10); 14 } 15 16 LIBPLDM_ABI_STABLE pldm_bcd_bcd2dec16(uint16_t bcd)17uint16_t pldm_bcd_bcd2dec16(uint16_t bcd) 18 { 19 return pldm_bcd_bcd2dec8(bcd >> 8) * 100 + 20 pldm_bcd_bcd2dec8(bcd & 0xff); 21 } 22 23 LIBPLDM_ABI_STABLE pldm_bcd_dec2bcd16(uint16_t dec)24uint16_t pldm_bcd_dec2bcd16(uint16_t dec) 25 { 26 return pldm_bcd_dec2bcd8(dec % 100) | 27 ((uint16_t)(pldm_bcd_dec2bcd8(dec / 100)) << 8); 28 } 29 30 LIBPLDM_ABI_STABLE pldm_bcd_bcd2dec32(uint32_t bcd)31uint32_t pldm_bcd_bcd2dec32(uint32_t bcd) 32 { 33 return pldm_bcd_bcd2dec16(bcd >> 16) * 10000 + 34 pldm_bcd_bcd2dec16(bcd & 0xffff); 35 } 36 37 LIBPLDM_ABI_STABLE pldm_bcd_dec2bcd32(uint32_t dec)38uint32_t pldm_bcd_dec2bcd32(uint32_t dec) 39 { 40 return pldm_bcd_dec2bcd16(dec % 10000) | 41 ((uint32_t)(pldm_bcd_dec2bcd16(dec / 10000)) << 16); 42 } 43