xref: /freebsd/contrib/gdtoa/ulp.c (revision 50dad48bb740a8e56d185d9e8c165e0758f46e25)
1cc36ccd1SDavid Schultz /****************************************************************
2cc36ccd1SDavid Schultz 
3cc36ccd1SDavid Schultz The author of this software is David M. Gay.
4cc36ccd1SDavid Schultz 
5cc36ccd1SDavid Schultz Copyright (C) 1998, 1999 by Lucent Technologies
6cc36ccd1SDavid Schultz All Rights Reserved
7cc36ccd1SDavid Schultz 
8cc36ccd1SDavid Schultz Permission to use, copy, modify, and distribute this software and
9cc36ccd1SDavid Schultz its documentation for any purpose and without fee is hereby
10cc36ccd1SDavid Schultz granted, provided that the above copyright notice appear in all
11cc36ccd1SDavid Schultz copies and that both that the copyright notice and this
12cc36ccd1SDavid Schultz permission notice and warranty disclaimer appear in supporting
13cc36ccd1SDavid Schultz documentation, and that the name of Lucent or any of its entities
14cc36ccd1SDavid Schultz not be used in advertising or publicity pertaining to
15cc36ccd1SDavid Schultz distribution of the software without specific, written prior
16cc36ccd1SDavid Schultz permission.
17cc36ccd1SDavid Schultz 
18cc36ccd1SDavid Schultz LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19cc36ccd1SDavid Schultz INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20cc36ccd1SDavid Schultz IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21cc36ccd1SDavid Schultz SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22cc36ccd1SDavid Schultz WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23cc36ccd1SDavid Schultz IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24cc36ccd1SDavid Schultz ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25cc36ccd1SDavid Schultz THIS SOFTWARE.
26cc36ccd1SDavid Schultz 
27cc36ccd1SDavid Schultz ****************************************************************/
28cc36ccd1SDavid Schultz 
29c88250a5SDavid Schultz /* Please send bug reports to David M. Gay (dmg at acm dot org,
30c88250a5SDavid Schultz  * with " at " changed at "@" and " dot " changed to ".").	*/
31cc36ccd1SDavid Schultz 
32cc36ccd1SDavid Schultz #include "gdtoaimp.h"
33cc36ccd1SDavid Schultz 
34cc36ccd1SDavid Schultz  double
ulp(x)35cc36ccd1SDavid Schultz ulp
36cc36ccd1SDavid Schultz #ifdef KR_headers
37*50dad48bSDavid Schultz 	(x) U *x;
38cc36ccd1SDavid Schultz #else
39*50dad48bSDavid Schultz 	(U *x)
40cc36ccd1SDavid Schultz #endif
41cc36ccd1SDavid Schultz {
42cc36ccd1SDavid Schultz 	Long L;
43*50dad48bSDavid Schultz 	U a;
44cc36ccd1SDavid Schultz 
45cc36ccd1SDavid Schultz 	L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
46cc36ccd1SDavid Schultz #ifndef Sudden_Underflow
47cc36ccd1SDavid Schultz 	if (L > 0) {
48cc36ccd1SDavid Schultz #endif
49cc36ccd1SDavid Schultz #ifdef IBM
50cc36ccd1SDavid Schultz 		L |= Exp_msk1 >> 4;
51cc36ccd1SDavid Schultz #endif
52*50dad48bSDavid Schultz 		word0(&a) = L;
53*50dad48bSDavid Schultz 		word1(&a) = 0;
54cc36ccd1SDavid Schultz #ifndef Sudden_Underflow
55cc36ccd1SDavid Schultz 		}
56cc36ccd1SDavid Schultz 	else {
57cc36ccd1SDavid Schultz 		L = -L >> Exp_shift;
58cc36ccd1SDavid Schultz 		if (L < Exp_shift) {
59*50dad48bSDavid Schultz 			word0(&a) = 0x80000 >> L;
60*50dad48bSDavid Schultz 			word1(&a) = 0;
61cc36ccd1SDavid Schultz 			}
62cc36ccd1SDavid Schultz 		else {
63*50dad48bSDavid Schultz 			word0(&a) = 0;
64cc36ccd1SDavid Schultz 			L -= Exp_shift;
65*50dad48bSDavid Schultz 			word1(&a) = L >= 31 ? 1 : 1 << (31 - L);
66cc36ccd1SDavid Schultz 			}
67cc36ccd1SDavid Schultz 		}
68cc36ccd1SDavid Schultz #endif
69*50dad48bSDavid Schultz 	return dval(&a);
70cc36ccd1SDavid Schultz 	}
71