This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Return values smaller than âintâ


Hello,

Consider this program:

--8<---------------cut here---------------start------------->8---
#include <ffi.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

static uint16_t
foo (void)
{
  return 0x9876U;
}

int
main (int argc, char *argv[])
{
  ffi_cif cif;
  char rvalue[123];

  if (FFI_OK != ffi_prep_cif (&cif, FFI_DEFAULT_ABI, 0,
			      &ffi_type_uint16, NULL))
    abort ();

  ffi_call (&cif, (void *) &foo, rvalue, NULL);

  printf ("%02x %02x %02x %02x %02x %02x %02x %02x\n",
	  rvalue[0], rvalue[1], rvalue[2], rvalue[3],
	  rvalue[4], rvalue[5], rvalue[6], rvalue[7]);

  printf ("-> %04x vs %04x vs %04x\n",
	  * (uint16_t *) rvalue, * (int *) rvalue, foo ());

  return 0;
}
--8<---------------cut here---------------end--------------->8---

On x86_64, it prints:

--8<---------------cut here---------------start------------->8---
76 ffffff98 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
-> 9876 vs ffff9876 vs 9876
--8<---------------cut here---------------end--------------->8---

On PowerPC64:

--8<---------------cut here---------------start------------->8---
00 00 98 76 ff ff e3 40
-> 0000 vs 9876 vs 9876
--8<---------------cut here---------------end--------------->8---

On SPARC64:

--8<---------------cut here---------------start------------->8---
ffffffff ffffffff ffffff98 76 fffffff7 fffffffa ffffff90 00
-> ffff vs ffff9876 vs 9876
--8<---------------cut here---------------end--------------->8---

How can the result be retrieved in a portable way?

Thanks,
Ludo'.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]