[coreboot] libpayload: Convert HEX2BIN/BIN2HEX to functions, add abs() et al

Jordan Crouse jordan.crouse at amd.com
Tue Apr 8 04:43:17 CEST 2008


On 08/04/08 02:41 +0200, Uwe Hermann wrote:
> See patch.
> 
> 
> Uwe.
> -- 
> http://www.hermann-uwe.de  | http://www.holsham-traders.de
> http://www.crazy-hacks.org | http://www.unmaintained-free-software.org

> Convert BIN2HEX/HEX2BIN to functions and add the abs() family
> of functions while we're at it.
> 
> Signed-off-by: Uwe Hermann <uwe at hermann-uwe.de>
> 
> Index: include/libpayload.h
> ===================================================================
> --- include/libpayload.h	(Revision 3222)
> +++ include/libpayload.h	(Arbeitskopie)
> @@ -41,10 +41,6 @@
>  #define MAX(a,b) ((a) > (b) ? (a) : (b))
>  #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
>  
> -#define BIN2HEX(b) ("0123456789abcdef"[b & 15])
> -#define HEX2BIN(h) (('0' <= h && h <= '9') ? (h - '0') : \
> -                    ('a' <= h && h <= 'f') ? (h - 'a' + 10) : 0)
> -
>  #define LITTLE_ENDIAN	1234
>  #define BIG_ENDIAN	4321
>  #ifdef CONFIG_TARGET_I386
> @@ -116,6 +112,11 @@
>  /* libc/lib.c */
>  int bcd2dec(int b);
>  int dec2bcd(int d);
> +int abs(int j);
> +long int labs(long int j);
> +long long int llabs(long long int j);
> +u8 bin2hex(u8 b);
> +u8 hex2bin(u8 h);
>  
>  /* libc/memory.c */
>  void *memset(void *s, int c, size_t n);
> Index: libc/lib.c
> ===================================================================
> --- libc/lib.c	(Revision 3222)
> +++ libc/lib.c	(Arbeitskopie)
> @@ -27,6 +27,8 @@
>   * SUCH DAMAGE.
>   */
>  
> +#include <libpayload.h>
> +
>  /*
>   * Convert a number in BCD format to decimal.
>   *
> @@ -49,3 +51,34 @@
>  	return ((d / 10) << 4) | (d % 10);
>  }
>  
> +/**
> + * Return the absolute value of the specified integer.
> + *
> + * @param val The integer of which we want to know the absolute value.
> + * @return The absolute value of the specified integer.
> + */
> +int abs(int j)
> +{
> +	return (j >= 0 ? j : -j);
> +}
> +
> +long int labs(long int j)
> +{
> +	return (j >= 0 ? j : -j);
> +}
> +
> +long long int llabs(long long int j)
> +{
> +	return (j >= 0 ? j : -j);
> +}
> +

Do we really need all the types?  I think that we can get by
with just abs(), and probably even make it a #define which will
eliminate the need for type checking anyway.

#define abs(_a) ((_a) >= 0 ? (_a) : -(_a))

Jordan

-- 
Jordan Crouse
Systems Software Development Engineer 
Advanced Micro Devices, Inc.





More information about the coreboot mailing list