/* RealImplSupport.c Copyright Ó 1988, 1990 by Xerox Corporation. All rights reserved. Michael Plass, March 2, 1988 3:39:45 pm PST Bryan Lyles October 18, 1988 3:00:40 pm PDT Russ Atkinson, December 20, 1990 1:46:43 pm PST */ /* must be compiled with -fsingle */ /* RealImplSupport.c provides the hooks into c floating-point support that are required by Basics.mesa and RealImpl.mesa */ #include typedef int REAL32; typedef unsigned word, *ptr; REAL32 XR_REAL32_from_double(); double XR_double_from_REAL32(); REAL32 XR_REAL32_from_double(d) double d; { float f = d; return (*((REAL32*) &f)); } double XR_double_from_REAL32(r) REAL32 r; { return (*((float*) &r)); } extern int XR_REAL32_Fix(r) REAL32 r; { int i; i = (int) (*(float *)(&r)); return(i); } extern int XR_REAL32_Round(r) REAL32 r; { /* this is a relatively expensive, but correct, round to nearest */ word w = r; word absw = (w << 1) >> 1; /* quick and cheap hack for absolute value */ float f = (*(float *)(&absw)); float h = 0.5; int rnd = f+h; /* round towards nearest (roughly) */ float rf = rnd; float df = rf - f; if (df == h) if ((rnd & 1) == 1) rnd = rnd - 1; if (w != absw) rnd = -rnd; return (rnd); } extern int XR_REAL32_Ceiling(r) REAL32 r; { int temp; float foo = (*(float *) (&r)); temp = (int) foo; if (r <= 0) return (temp); if (temp == foo) return (temp); return (temp+1); } extern int XR_REAL32_Floor(r) REAL32 r; { int temp; float foo = (*(float *) (&r)); temp = (int) foo; if (r >= 0) return (temp); if (temp == foo) return (temp); return (temp - 1); } extern int XR_RealCompare(x, y) REAL32 x, y; { /* ret: 0 --> x < y, 1 --> x = y, 2 --> x > y */ int temp; float xf = (*(float *) (&x)); float yf = (*(float *) (&y)); if (xf < yf) return (0); if (xf > yf) return (2); return (1); }