xref: /minix3/lib/libc/gdtoa/ulp.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /* $NetBSD: ulp.c,v 1.3 2011/03/20 23:15:35 christos Exp $ */
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /****************************************************************
42fe8fb19SBen Gras 
52fe8fb19SBen Gras The author of this software is David M. Gay.
62fe8fb19SBen Gras 
72fe8fb19SBen Gras Copyright (C) 1998, 1999 by Lucent Technologies
82fe8fb19SBen Gras All Rights Reserved
92fe8fb19SBen Gras 
102fe8fb19SBen Gras Permission to use, copy, modify, and distribute this software and
112fe8fb19SBen Gras its documentation for any purpose and without fee is hereby
122fe8fb19SBen Gras granted, provided that the above copyright notice appear in all
132fe8fb19SBen Gras copies and that both that the copyright notice and this
142fe8fb19SBen Gras permission notice and warranty disclaimer appear in supporting
152fe8fb19SBen Gras documentation, and that the name of Lucent or any of its entities
162fe8fb19SBen Gras not be used in advertising or publicity pertaining to
172fe8fb19SBen Gras distribution of the software without specific, written prior
182fe8fb19SBen Gras permission.
192fe8fb19SBen Gras 
202fe8fb19SBen Gras LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
212fe8fb19SBen Gras INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
222fe8fb19SBen Gras IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
232fe8fb19SBen Gras SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
242fe8fb19SBen Gras WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
252fe8fb19SBen Gras IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
262fe8fb19SBen Gras ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
272fe8fb19SBen Gras THIS SOFTWARE.
282fe8fb19SBen Gras 
292fe8fb19SBen Gras ****************************************************************/
302fe8fb19SBen Gras 
312fe8fb19SBen Gras /* Please send bug reports to David M. Gay (dmg at acm dot org,
322fe8fb19SBen Gras  * with " at " changed at "@" and " dot " changed to ".").	*/
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include "gdtoaimp.h"
352fe8fb19SBen Gras 
362fe8fb19SBen Gras  double
ulp(x)372fe8fb19SBen Gras ulp
382fe8fb19SBen Gras #ifdef KR_headers
39*f14fb602SLionel Sambuc 	(x) U *x;
402fe8fb19SBen Gras #else
41*f14fb602SLionel Sambuc 	(U *x)
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras {
442fe8fb19SBen Gras 	Long L;
45*f14fb602SLionel Sambuc 	U a;
462fe8fb19SBen Gras 
472fe8fb19SBen Gras 	L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
482fe8fb19SBen Gras #ifndef Sudden_Underflow
492fe8fb19SBen Gras 	if (L > 0) {
502fe8fb19SBen Gras #endif
512fe8fb19SBen Gras #ifdef IBM
522fe8fb19SBen Gras 		L |= Exp_msk1 >> 4;
532fe8fb19SBen Gras #endif
54*f14fb602SLionel Sambuc 		word0(&a) = L;
55*f14fb602SLionel Sambuc 		word1(&a) = 0;
562fe8fb19SBen Gras #ifndef Sudden_Underflow
572fe8fb19SBen Gras 		}
582fe8fb19SBen Gras 	else {
592fe8fb19SBen Gras 		L = (unsigned int)-L >> Exp_shift;
602fe8fb19SBen Gras 		if (L < Exp_shift) {
61*f14fb602SLionel Sambuc 			word0(&a) = 0x80000 >> L;
62*f14fb602SLionel Sambuc 			word1(&a) = 0;
632fe8fb19SBen Gras 			}
642fe8fb19SBen Gras 		else {
65*f14fb602SLionel Sambuc 			word0(&a) = 0;
662fe8fb19SBen Gras 			L -= Exp_shift;
67*f14fb602SLionel Sambuc 			word1(&a) = L >= 31 ? 1 : 1 << (31 - L);
682fe8fb19SBen Gras 			}
692fe8fb19SBen Gras 		}
702fe8fb19SBen Gras #endif
71*f14fb602SLionel Sambuc 	return dval(&a);
722fe8fb19SBen Gras 	}
73