Re: [PATCH v2 2/4] perf util: Add host_is_bigendian to util.h

From: Namhyung Kim
Date: Wed Nov 30 2022 - 13:53:09 EST


On Tue, Nov 29, 2022 at 10:30 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> Avoid libtraceevent dependency for tep_is_bigendian or trace-event.h
> dependency for bigendian. Add a new host_is_bigendian to util.h, using
> the compiler defined __BYTE_ORDER__ when available.
>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>

Acked-by: Namhyung Kim <namhyung@xxxxxxxxxx>

A nit below...

> ---
[SNIP]
> diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
> index 63cdab0e5314..87d418cb6792 100644
> --- a/tools/perf/util/util.h
> +++ b/tools/perf/util/util.h
> @@ -94,4 +94,23 @@ int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,
> 0; \
> })
>
> +static inline bool host_is_bigendian(void)
> +{
> +#ifdef __BYTE_ORDER__
> +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
> + return false;
> +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
> + return true;
> +#else
> +#error "Unrecognized __BYTE_ORDER__"
> +#endif
> +#else

It'd be nice if we could have a comment at least there's a nested
ifdef condition. Like below?

#else /* !__BYTE_ORDER__ */

Thanks,
Namhyung


> + unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
> + unsigned int *ptr;
> +
> + ptr = (unsigned int *)(void *)str;
> + return *ptr == 0x01020304;
> +#endif
> +}
> +
> #endif /* __PERF_UTIL_H */
> --
> 2.38.1.584.g0f3c55d4c2-goog
>