I like something like this for clarity.  But I don't see any reason
for these function-like macros instead of the more general definition
of an SI prefix constant multiple.  A multiple works with numeric
literals and variables alike.  Something like:

#define GiB ((size_t)1 << 30)
my_foo = 15 * GiB;

(There's nothing byte-specific about SI prefixes, but "Gi" alone is a
worse name.  Arguably, size_t is wrong for quantities of bytes on
32-bit platforms with 64-bit off_t.)

The compiler will still reduce constant expressions.  Perhaps even
better, conversion away is straightforward units math, and the
compiler can still do the clever thing with right shifts:

my_gb = howmany(my_foo, GiB); //or
my_gb = my_foo / GiB;

Unfortunately, I expect a lot of code to already have defines or
variables with conflicting names, so I'm not sure adding these names
to primary headers is viable as-is.

Best,
Conrad

On Fri, Apr 6, 2018 at 4:20 AM, Roger Pau Monné <roy...@freebsd.org> wrote:
> Author: royger
> Date: Fri Apr  6 11:20:06 2018
> New Revision: 332092
> URL: https://svnweb.freebsd.org/changeset/base/332092
>
> Log:
>   remove GiB/MiB macros from param.h
>
>   And instead define them in the files where they are used.
>
>   Requested by: bde
>
> Modified:
>   head/sys/amd64/amd64/mp_machdep.c
>   head/sys/sys/param.h
>   head/sys/x86/x86/mp_x86.c
>
> Modified: head/sys/amd64/amd64/mp_machdep.c
> ==============================================================================
> --- head/sys/amd64/amd64/mp_machdep.c   Fri Apr  6 09:25:08 2018        
> (r332091)
> +++ head/sys/amd64/amd64/mp_machdep.c   Fri Apr  6 11:20:06 2018        
> (r332092)
> @@ -83,6 +83,8 @@ __FBSDID("$FreeBSD$");
>  #define BIOS_RESET             (0x0f)
>  #define BIOS_WARM              (0x0a)
>
> +#define GiB(v)                 (v ## ULL << 30)
> +
>  extern struct pcpu __pcpu[];
>
>  /* Temporary variables for init_secondary()  */
>
> Modified: head/sys/sys/param.h
> ==============================================================================
> --- head/sys/sys/param.h        Fri Apr  6 09:25:08 2018        (r332091)
> +++ head/sys/sys/param.h        Fri Apr  6 11:20:06 2018        (r332092)
> @@ -362,8 +362,4 @@ __END_DECLS
>   */
>  #define __PAST_END(array, offset) (((__typeof__(*(array)) *)(array))[offset])
>
> -/* Unit conversion macros. */
> -#define GiB(v) (v ## ULL << 30)
> -#define MiB(v) (v ## ULL << 20)
> -
>  #endif /* _SYS_PARAM_H_ */
>
> Modified: head/sys/x86/x86/mp_x86.c
> ==============================================================================
> --- head/sys/x86/x86/mp_x86.c   Fri Apr  6 09:25:08 2018        (r332091)
> +++ head/sys/x86/x86/mp_x86.c   Fri Apr  6 11:20:06 2018        (r332092)
> @@ -160,6 +160,8 @@ struct cache_info {
>
>  unsigned int boot_address;
>
> +#define MiB(v) (v ## ULL << 20)
> +
>  void
>  mem_range_AP_init(void)
>  {
>
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to