From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 96960 invoked by alias); 10 Oct 2016 07:17:16 -0000 Mailing-List: contact libffi-discuss-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libffi-discuss-owner@sourceware.org Received: (qmail 94749 invoked by uid 89); 10 Oct 2016 07:17:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.0 required=5.0 tests=BAYES_20,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=H*Ad:U*libffi-discuss, H*r:sk:libffi-, sk:libffi_, non-variadic X-HELO: mail-yw0-f174.google.com Received: from mail-yw0-f174.google.com (HELO mail-yw0-f174.google.com) (209.85.161.174) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 10 Oct 2016 07:17:05 +0000 Received: by mail-yw0-f174.google.com with SMTP id t193so65637687ywc.2 for ; Mon, 10 Oct 2016 00:17:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc:content-transfer-encoding; bh=0joY1QekqpGWtYBFAlzf47z+vRm2PaaxYn4LZrg2+wA=; b=ksX5Qt97YlgfCv/RGPFAi7iT9D3WuJIdmbZBQFNXbibVCWYrEJDIJafhYk0m73kkQ1 kJ5w6OfsjRk4lT5HozEObotkM+not3vpVaScAz5fbLBDz6lCbczb/dAJwRdy0bQnAc/i FfJhhOH5w64kNSAm66zWghpP7q+FuSp4SDMqCkhq2Aa6sKpxk1rj0C6Uyc3qbehvbZox ZRhiur1MWGUHQ6DxMJV61GCXJJxj9yK3LYA0C1TdDjyCczglVKw8XeKFJvY9NwUl+8ZP 5Bu41mq8SS5bdeSoetTi3L4LJapMrRQ3vlhfrHudMy23X7wDqrnfwdt2W1W4kUHq+Z+K Z6zg== X-Gm-Message-State: AA6/9RnBB7dG5ba67btVlcCASp0cQkziJP8/hPPn1qTFg6IJqskSsR6PbZcHmrmFK8JcwQ8YVj/Qz9INsdpNWw== X-Received: by 10.129.74.2 with SMTP id x2mr26257496ywa.59.1476083824182; Mon, 10 Oct 2016 00:17:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.129.76.78 with HTTP; Mon, 10 Oct 2016 00:17:03 -0700 (PDT) In-Reply-To: <20161010070352.GA11072@han> References: <20161010070352.GA11072@han> From: Andrew Pinski Date: Mon, 10 Oct 2016 07:17:00 -0000 Message-ID: Subject: Re: Floating-point variadic function call To: =?UTF-8?Q?Damien_Th=C3=A9bault?= Cc: "libffi-discuss@sourceware.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2016/txt/msg00043.txt.bz2 On Mon, Oct 10, 2016 at 12:03 AM, Damien Th=C3=A9bault wr= ote: > Hello, > > I am trying to use libffi to call various functions, and my first try > was with printf(). As this is a variadic function, I used > ffi_prep_cif_var(). Everything seems to be working, except with floats, > which are not working, and are even generating wrong memory accesses > (detected by valgrind). > > A similar function with ffi_prep_cif() and floats is working properly. > > Could type promotion from float to double be the issue here ? > Does libffi handle type promotion in variadic calls or is it the > caller's job ? > > I tested on a x86_64 computer both 64-bit and 32-bit versions (the > latter compiled with -m32) as well as ARM under qemu. > In all those cases, I didn't get the proper result with floats while > doubles are ok, as well as chars. That is because the variadic function ABI is different from the normal argument ABI on ARM hard-float EABI. Basically for variadic functions float are passed via the integer registers while for normal functions, they are passed via the vfp registers. Basically you need to use variadic function support in libff. Use ffi_prep_cif_var instead of ffi_prep_cif and then don't do: for(i=3D1 ; i<10 ; i++) { arg_types[i] =3D &ffi_type_float; } Thanks, Andrew Pinski > I've attached a sample source file that reproduces the problem and here > is the output of one test run: > > $ gcc $(pkg-config --cflags --libs libffi) -std=3Dc99 -pedantic -Wall -W= extra libffi_variadic_printf.c -o libffi_variadic_printf > $ ./libffi_variadic_printf > Non-variadic call: > [1.100000,2.200000,3.300000,4.400000,5.500000,6.600000,7.700000,8.800000,= 9.900000] > result is 83 > Variadic call: > [0.000000,0.000000,0.000000,0.000000,0.000000,91750.390826,0.000000,0.000= 000,0.000000] > result is 87 > > Thanks,