| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 1 | /* This file is distributed under the University of Illinois Open Source | 
 | 2 |  * License. See LICENSE.TXT for details. | 
 | 3 |  */ | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 4 |  | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 5 | /* uint64_t __fixunstfdi(long double x); */ | 
 | 6 | /* This file implements the PowerPC 128-bit double-double -> uint64_t conversion */ | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 7 |  | 
 | 8 | #include "DD.h" | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 9 |  | 
 | 10 | uint64_t __fixunstfdi(long double input) | 
 | 11 | { | 
 | 12 | 	const DD x = { .ld = input }; | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 13 | 	const doublebits hibits = { .d = x.s.hi }; | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 14 | 	 | 
 | 15 | 	const uint32_t highWordMinusOne = (uint32_t)(hibits.x >> 32) - UINT32_C(0x3ff00000); | 
 | 16 | 	 | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 17 | 	/* If (1.0 - tiny) <= input < 0x1.0p64: */ | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 18 | 	if (UINT32_C(0x04000000) > highWordMinusOne) | 
 | 19 | 	{ | 
 | 20 | 		const int unbiasedHeadExponent = highWordMinusOne >> 20; | 
 | 21 | 		 | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 22 | 		uint64_t result = hibits.x & UINT64_C(0x000fffffffffffff); /* mantissa(hi) */ | 
 | 23 | 		result |= UINT64_C(0x0010000000000000); /* matissa(hi) with implicit bit */ | 
 | 24 | 		result <<= 11; /* mantissa(hi) left aligned in the int64 field. */ | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 25 | 		 | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 26 | 		/* If the tail is non-zero, we need to patch in the tail bits. */ | 
 | 27 | 		if (0.0 != x.s.lo) | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 28 | 		{ | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 29 | 			const doublebits lobits = { .d = x.s.lo }; | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 30 | 			int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff); | 
 | 31 | 			tailMantissa |= INT64_C(0x0010000000000000); | 
 | 32 | 			 | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 33 | 			/* At this point we have the mantissa of |tail| */ | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 34 | 			 | 
 | 35 | 			const int64_t negationMask = ((int64_t)(lobits.x)) >> 63; | 
 | 36 | 			tailMantissa = (tailMantissa ^ negationMask) - negationMask; | 
 | 37 | 			 | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 38 | 			/* Now we have the mantissa of tail as a signed 2s-complement integer */ | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 39 | 			 | 
 | 40 | 			const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff; | 
 | 41 | 			 | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 42 | 			/* Shift the tail mantissa into the right position, accounting for the | 
 | 43 | 			 * bias of 11 that we shifted the head mantissa by. | 
 | 44 | 			 */ | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 45 | 			tailMantissa >>= (unbiasedHeadExponent - (biasedTailExponent - (1023 - 11))); | 
 | 46 | 			 | 
 | 47 | 			result += tailMantissa; | 
 | 48 | 		} | 
 | 49 | 		 | 
 | 50 | 		result >>= (63 - unbiasedHeadExponent); | 
 | 51 | 		return result; | 
 | 52 | 	} | 
 | 53 | 	 | 
| Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 54 | 	/* Edge cases are handled here, with saturation. */ | 
 | 55 | 	if (1.0 > x.s.hi) | 
| Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 56 | 		return UINT64_C(0); | 
 | 57 | 	else | 
 | 58 | 		return UINT64_MAX; | 
 | 59 | } |