pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: Use static inline functions for Float <-> Datum conversions
Date: Wed, 31 Aug 2016 09:44:14 -0400
Message-ID: <18068.1472651054@sss.pgh.pa.us> (raw)
In-Reply-To: <752262dc-0a51-22bd-9cfa-88532d2d8dc2@iki.fi>
References: <dbb82a4a-2c15-ba27-dd0a-009d2aa72b77@iki.fi>
	<6355.1472643514@sss.pgh.pa.us>
	<752262dc-0a51-22bd-9cfa-88532d2d8dc2@iki.fi>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

Heikki Linnakangas <hlinnaka@iki.fi> writes:
> On 08/31/2016 02:38 PM, Tom Lane wrote:
>> I wonder whether there is a compiler-dependent way of avoiding the union
>> trick ... or maybe gcc is already smart enough that it doesn't matter?

> It seems to compile into a single instruction, so it can't get any 
> better from a performance point of view.

Yeah, confirmed here.  On my not-real-new gcc (version 4.4.7, which
ships with RHEL6), these test functions:

Datum
compare_int8(PG_FUNCTION_ARGS)
{
	int64		x = PG_GETARG_INT64(0);
	int64		y = PG_GETARG_INT64(1);

	PG_RETURN_BOOL(x < y);
}

Datum
compare_float8(PG_FUNCTION_ARGS)
{
	double		x = PG_GETARG_FLOAT8(0);
	double		y = PG_GETARG_FLOAT8(1);

	PG_RETURN_BOOL(x < y);
}

compile into this (at -O2):

compare_int8:
	.cfi_startproc
	movq	40(%rdi), %rax
	cmpq	%rax, 32(%rdi)
	setl	%al
	movzbl	%al, %eax
	ret
	.cfi_endproc

compare_float8:
	.cfi_startproc
	movsd	40(%rdi), %xmm0
	xorl	%eax, %eax
	ucomisd	32(%rdi), %xmm0
	seta	%al
	ret
	.cfi_endproc

(Not sure why the compiler does the widening of the comparison result
differently, but it doesn't look like it matters.)  Before this patch,
that looked like:

compare_float8:
	.cfi_startproc
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	32(%rdi), %rdi
	call	DatumGetFloat8
	movq	40(%rbx), %rdi
	movsd	%xmm0, 8(%rsp)
	call	DatumGetFloat8
	xorl	%eax, %eax
	ucomisd	8(%rsp), %xmm0
	seta	%al
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc

Nice.

			regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers



view thread (4+ messages)

Message-ID: <18068.1472651054@sss.pgh.pa.us>
Permalink:  ../18068.1472651054@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/18068.1472651054@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, hlinnaka@iki.fi
  Subject: Re: Use static inline functions for Float <-> Datum conversions
  In-Reply-To: <18068.1472651054@sss.pgh.pa.us>
  References: <dbb82a4a-2c15-ba27-dd0a-009d2aa72b77@iki.fi> <6355.1472643514@sss.pgh.pa.us> <752262dc-0a51-22bd-9cfa-88532d2d8dc2@iki.fi> <18068.1472651054@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox