How to print __u128_t in kernel?

Bjørn Mork bjorn at mork.no
Tue Sep 21 03:21:40 EDT 2021


Philipp Hortmann <philipp.g.hortmann at gmail.com> writes:

> I do like this example:
> https://lwn.net/Articles/745261/
>
> ...
> pr_err("%llx%llx\n", (u64) (val >> 64), (u64) val);
> pr_err("%llx%llx\n", (u64) (v >> 64), (u64) v);
> ...
>

This will produce the wrong output unless at least one of bit 60 - 63 is
set and have a confusing leading zero unless one of the upper 64 bits is
set.

Running this test:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
        __uint128_t foo;

        if (argc < 3)
                return -1;
        foo = (__uint128_t)strtoull(argv[1], NULL, 0) << 64 | strtoull(argv[2], NULL, 0);
        fprintf(stderr, "foo=0x%llx%llx\n", (__uint64_t) (foo >> 64), (__uint64_t) foo);
        fprintf(stderr, "foo=0x%016llx%016llx\n", (__uint64_t) (foo >> 64), (__uint64_t) foo);
        return 0;
}


Produces:

bjorn at miraculix:~$ /tmp/x 1 0
foo=0x10
foo=0x00000000000000010000000000000000
bjorn at miraculix:~$ /tmp/x 1 2
foo=0x12
foo=0x00000000000000010000000000000002
bjorn at miraculix:~$ /tmp/x 0 2
foo=0x02
foo=0x00000000000000000000000000000002



Bjørn



More information about the Kernelnewbies mailing list