2

I am accessing memory on a FPGA from a HPS running Linux and I stumbled upon a problem.

    {
        long long address_debug = *(shared_memory + i);
        printf("index: %i - value: %16x \n", i, address_debug);
    }

returns the values that I expect in a hexadecimal format, whereas


    for (i = 0; i < 700; i++)
    {
        long long address_debug = *(shared_memory + i);

            printf("index: %i - value: %lld \n", i, address_debug);
    }

returns values that are shifted 32 bit to the left. I get correct results with:

printf("index: %i - value: %lld \n", i, address_debug>>31);

or

printf("index: %i - value: %llu \n", i, address_debug>>31);

I am confused, as the variable itself has the same value, what am I missing?

  • 7
    `"%x"` is for `int`, you want `"%llx"`. – Some programmer dude Jun 07 '19 at 07:59
  • 1
    Most compilers nowadays have the ability to diagnose problems involving arguments that don't match the format string in a `printf`. Be sure to enable those warnings, and pay attention to them. – user3386109 Jun 07 '19 at 08:07
  • `%llx` will show you the true value of `address_debug.` What you're seeing with `%x` won't be correct. It should be trivial for you to check this (print it one bit at a time if you really feel the need). – Tom Karzes Jun 07 '19 at 08:07
  • The first fragment has undefined behaviour. It can do absolutely anything, including the thing that you expect. The second fragment is correct. If it does not do what you expect, then your expectations are not grounded in reality and you.probably want to adjust them. *What* value you expect to be printed and *why*? – n. m. could be an AI Jun 07 '19 at 08:17
  • What is `shared_memory` and how was it filled? What you get could be a hint that something weird happens there... – Serge Ballesta Jun 07 '19 at 08:23
  • Thank you for all your answers! The problem I am facing has to do with the width of the bus I am using: https://www.intel.com/content/www/us/en/programmable/documentation/doq1481305867183.html#sxr1481303255441 I should use the H2F Bridge instead of the Lightweight Bus. Sometimes it helps to rtfm ;) – user3637713 Jun 07 '19 at 09:22

1 Answers1

3

When you use "%16x", printf handles the given value as unsigned int.

Please, tell to printf that value is long long by using: "%16llx"

From man page of printf:

ll

(ell-ell). A following integer conversion corresponds to a long long int or unsigned long long int argument, or a following n conversion corresponds to a pointer to a long long int argument.

SKi
  • 8,007
  • 2
  • 26
  • 57