Date: Wed, 5 Jan 2000 09:23:57 +0100 From: Martin Cracauer <cracauer@cons.org> To: Markus Holmberg <saska@acc.umu.se> Cc: "Ronald F. Guilmette" <rfg@monkeys.com>, freebsd-hackers@FreeBSD.ORG Subject: Re: Should -mieee-fp equal fpsetmask(0) to avoid SIGFPE on FreeBSD? Message-ID: <20000105092356.A8100@cons.org> In-Reply-To: <20000105055138.A74746@fysgr387.sn.umu.se>; from saska@acc.umu.se on Wed, Jan 05, 2000 at 05:51:38AM %2B0100 References: <20000104121459.A8959@cons.org> <87632.947017515@monkeys.com> <20000105055138.A74746@fysgr387.sn.umu.se>
next in thread | previous in thread | raw e-mail | index | archive | help
--jRHKVT23PllUwdXP Content-Type: text/plain; charset=us-ascii Summary of following: The mozilla construction is a speed hack and it would usually work when exceptions are disabled. However, it is slower than the real thing would be and is hence useless and dangerous. In <20000105055138.A74746@fysgr387.sn.umu.se>, Markus Holmberg wrote: > To my understanding, there isn't a flaw in the Mozilla code. What is > happening is that a cast is made to test if a value inside a double > actually is just an int! If it wasn't, no harm is done and the double > will be continued to be treaten as a double. > > That's is how I've interpreted it. To see where the macro in question > (that generates the SIGFPE's) is defined and used, check this link: > > http://lxr.mozilla.org/seamonkey/ident?i=JSDOUBLE_IS_INT > > Here's the definition: > > #define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d) && \ > !JSDOUBLE_IS_NEGZERO(d) && ((d) == (i = (jsint)(d)))) Ah, OK, this macro is a conversion where the undefined result of the (int)double_bigger_max_int is (intentionally) used as a speed hack of a range check. The only problems is that it isn't faster than correct code. It is true that in practice it this macro will work as intended when exceptions are disabled, because: While the result of (int)double_bigger_max_int is undefined, you will usually get an i that is filled with any integer value. If d was > INT_MAX, the == with return nil for any value of i. Hence the thing works if the programmer remembers only to use i if the macro return true. Beside the fact that I don't like this construction style-wise, I have two problems with it: 1) In C, when an expression is undefined, *anything* may happen. You cannot depend on the fact that the code behaves as if i was filled with an integer at all. This is not ANSI C conformant. An conformant ANSI C compiler may make assumptions about this code that break it. 2) This speed hack is already weakend: If you look into the header file in Mozilla, you will find that the IS_FINITE and IS_NEGZERO was added to make MSVC++ happy, because it will run into exceptions otherwise. The alternative to this hack is a normal range check. This will work because behaviour is defined for comparisions on infinite and other exceptional values. You would get: - ANSI C conformance. - Make VC++ happy. - Make the code ready to leave those FPU exceptions enabled that indicate real and serious problems. - On my machine, it is actually faster than the current version. Here's one version, implemented as an inline function. A complete runnable example comparing the Mozilla construction (including timekeeping) to this one is appended. On my machines, the conforming version is 2-3 times faster than the Mozilla hack (and it doesn't trigger exceptions). static inline int cra_is_int(const double d, int *const i) { if (d <= (double)INT_MAX && d >= (double)INT_MIN) { *i = (int)d; return 1; } else return 0; } I can only urge the Mozilla team to use clean constructions and leave their hands off speed hacks that trigger undefined behaviour. The project needs reliability first, and then speed. In this case, you don't even get speed. Martin -- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Martin Cracauer <cracauer@cons.org> http://www.cons.org/cracauer/ Tel.: (private) +4940 5221829 Fax.: (private) +4940 5228536 --jRHKVT23PllUwdXP Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="cast.c" #include <stdio.h> #include <limits.h> #include <sys/time.h> #include <sys/types.h> #include <floatingpoint.h> #if defined(SysV) || defined(__svr4__) #include <limits.h> #include <time.h> #include <sys/times.h> #else #include <sys/resource.h> #endif typedef __uint32_t uint32; typedef int jsint; /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Mozilla Communicator client code, released * March 31, 1998. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the * provisions of the GPL are applicable instead of those above. * If you wish to allow use of your version of this file only * under the terms of the GPL and not to allow others to use your * version of this file under the NPL, indicate your decision by * deleting the provisions above and replace them with the notice * and other provisions required by the GPL. If you do not delete * the provisions above, a recipient may use your version of this * file under either the NPL or the GPL. */ /* * JS number (IEEE double) interface. * * JS numbers are optimistically stored in the top 31 bits of 32-bit integers, * but floating point literals, results that overflow 31 bits, and division and * modulus operands and results require a 64-bit IEEE double. These are GC'ed * and pointed to by 32-bit jsvals on the stack and in object properties. * * When a JS number is treated as an object (followed by . or []), the runtime * wraps it with a JSObject whose valueOf method returns the unwrapped number. */ #define IS_LITTLE_ENDIAN #ifdef IS_LITTLE_ENDIAN #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[1]) #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[0]) #else #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[0]) #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[1]) #endif #define JSDOUBLE_HI32_SIGNBIT 0x80000000 #define JSDOUBLE_HI32_EXPMASK 0x7ff00000 #define JSDOUBLE_HI32_MANTMASK 0x000fffff #define JSDOUBLE_IS_NaN(x) \ ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK && \ (JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK))) #define JSDOUBLE_IS_INFINITE(x) \ ((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK && \ !JSDOUBLE_LO32(x)) #define JSDOUBLE_IS_FINITE(x) \ ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK) #define JSDOUBLE_IS_NEGZERO(d) (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \ JSDOUBLE_LO32(d) == 0) /* * JSDOUBLE_IS_INT first checks that d is neither NaN nor infinite, to avoid * raising SIGFPE on platforms such as Alpha Linux, then (only if the cast is * safe) leaves i as (jsint)d. This also avoid anomalous NaN floating point * comparisons under MSVC. */ #define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d) && !JSDOUBLE_IS_NEGZERO(d) \ && ((d) == (i = (jsint)(d)))) static inline int cra_is_int(const double d, int *const i) { if (d <= (double)INT_MAX && d >= (double)INT_MIN) { *i = (int)d; return 1; } else return 0; } static double cpu_so_far(void) { #if defined(SysV) || defined(__svr4__) struct tms tms; if (times(&tms) == -1) io_error("times"); return ((double) tms.tms_utime) / ((double) CLK_TCK) + ((double) tms.tms_stime) / ((double) CLK_TCK); #else struct rusage rusage; getrusage(RUSAGE_SELF, &rusage); return ((double) rusage.ru_utime.tv_sec) + (((double) rusage.ru_utime.tv_usec) / 1000000.0) + ((double) rusage.ru_stime.tv_sec) + (((double) rusage.ru_stime.tv_usec) / 1000000.0); #endif } int main(void) { double time; int count; double d; int i; int res; const int n = 5000000; fpsetmask(0); time = cpu_so_far(); d = INT_MIN + n / 2; count = 0; for (i = 0; i < n; i++) { d--; if (JSDOUBLE_IS_INT(d, res)) count++; } printf("JS : %d values were valid, time: %.2f sec\n", count, cpu_so_far() - time); time = cpu_so_far(); d = INT_MAX - n / 2; count = 0; for (i = 0; i < n; i++) { d++; if (cra_is_int(d, &res)) count++; } printf("cra: %d values were valid, time: %.2f sec\n", count, cpu_so_far() - time); return 0; } --jRHKVT23PllUwdXP-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000105092356.A8100>