From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 8E1BE7EEBF for ; Thu, 23 Jul 2015 11:58:41 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of misterherr@freenet.de) identity=pra; client-ip=195.4.92.92; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="misterherr@freenet.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of misterherr@freenet.de) identity=mailfrom; client-ip=195.4.92.92; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="misterherr@freenet.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mout2.freenet.de) identity=helo; client-ip=195.4.92.92; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="postmaster@mout2.freenet.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0DFAACCurBVnFxcBMNbgmmFDL1Ogl4CgUxMAQEBAQEBEgEBAQEBBg0JCSEuhCQBAQQjFUARCxgCAgUWCwICCQMCAQIBRRMIAQGIFAEWAwG1IJAYCYEzAYRIAQoBAQEegSKKKoRTOheCUoFDBZRgAYdXhiaEHYJrA4xrg2GEJIM4AQEB X-IPAS-Result: A0DFAACCurBVnFxcBMNbgmmFDL1Ogl4CgUxMAQEBAQEBEgEBAQEBBg0JCSEuhCQBAQQjFUARCxgCAgUWCwICCQMCAQIBRRMIAQGIFAEWAwG1IJAYCYEzAYRIAQoBAQEegSKKKoRTOheCUoFDBZRgAYdXhiaEHYJrA4xrg2GEJIM4AQEB X-IronPort-AV: E=Sophos;i="5.15,529,1432591200"; d="scan'208";a="140965381" Received: from mout2.freenet.de ([195.4.92.92]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 23 Jul 2015 11:58:40 +0200 Received: from [195.4.92.142] (helo=mjail2.freenet.de) by mout2.freenet.de with esmtpa (ID misterherr@freenet.de) (port 25) (Exim 4.85 #1) id 1ZIDGy-0004gT-7J for caml-list@inria.fr; Thu, 23 Jul 2015 11:58:40 +0200 Received: from localhost ([::1]:45955 helo=mjail2.freenet.de) by mjail2.freenet.de with esmtpa (ID misterherr@freenet.de) (Exim 4.85 #1) id 1ZIDGy-0000JN-43 for caml-list@inria.fr; Thu, 23 Jul 2015 11:58:40 +0200 Received: from mx11.freenet.de ([195.4.92.21]:55281) by mjail2.freenet.de with esmtpa (ID misterherr@freenet.de) (Exim 4.85 #1) id 1ZIDD5-0000wb-Ii for caml-list@inria.fr; Thu, 23 Jul 2015 11:54:39 +0200 Received: from cl-252.ham-02.de.sixxs.net ([2001:6f8:1c00:fb::2]:59807) by mx11.freenet.de with esmtpsa (ID misterherr@freenet.de) (TLSv1.2:DHE-RSA-AES128-SHA:128) (port 465) (Exim 4.85 #1) id 1ZIDD5-0005rM-BK for caml-list@inria.fr; Thu, 23 Jul 2015 11:54:39 +0200 To: caml-list@inria.fr References: <20150723083517.GA2029@pl-59055.rocqadm.inria.fr> <55B0AD74.2030302@inria.fr> From: "Mr. Herr" Message-ID: <55B0B9DE.2070609@freenet.de> Date: Thu, 23 Jul 2015 11:54:38 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: <55B0AD74.2030302@inria.fr> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Originated-At: 2001:6f8:1c00:fb::2!59807 Subject: Re: [Caml-list] Comparing floats On 23.07.2015 11:01, Xavier Leroy wrote: > On 23/07/2015 10:35, S=C3=A9bastien Hinderer wrote: >> What's the most efficient way to compare floats, please? >> Is it the polymorphic compare function, or is there a more specialized >> version of it? > You'll get good performance by type-specializing Pervasives.compare: > > let compare_float (x: float) (y: float) =3D compare x y > > If you're absolutely sure your floats are not NaN, you can shave a few > CPU cycles: > > let compare_float (x: float) (y: float) =3D > if x < y then -1 else if x > y then 1 else 0 > The assembler code says compare_float is directly compiled to a function th= at=20 compares the 2 values in xmm0 and xmm1 registers, while Pervasives.compare is a library function = written in=20 C doing the same thing. The assembler code looks very very good. But I doubt that you could measure a difference. The type system will always yield the float_compare function, doesn't it? So far my quickcheck... --- Assembler code of your suggested function: camlTest_comparefl3__compare_float_1008: .cfi_startproc .L102: movsd (%rbx), %xmm0 movsd (%rax), %xmm1 comisd %xmm1, %xmm0 jbe .L101 movq $-1, %rax ret .align 4 .L101: comisd %xmm0, %xmm1 jbe .L100 movq $3, %rax ret .align 4 .L100: movq $1, %rax ret .cfi_endproc --- C code of library function: CAMLprim value caml_float_compare(value vf, value vg) { double f =3D Double_val(vf); double g =3D Double_val(vg); if (f =3D=3D g) return Val_int(0); if (f < g) return Val_int(-1); if (f > g) return Val_int(1); /* One or both of f and g is NaN. Order according to the convention NaN =3D NaN and NaN < x for all other floats x. */ if (f =3D=3D f) return Val_int(1); /* f is not NaN, g is NaN */ if (g =3D=3D g) return Val_int(-1); /* g is not NaN, f is NaN */ return Val_int(0); /* both f and g are NaN */ }