s_truncl.c (5014f8ded47a5e6d770ca588a2b929dff2617667) | s_truncl.c (2f2ee27de4367161d0f6e3913c5921f0d002fd98) |
---|---|
1/* 2 * ==================================================== 3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 4 * 5 * Developed at SunPro, a Sun Microsystems, Inc. business. 6 * Permission to use, copy, modify, and distribute this 7 * software is freely granted, provided that this notice 8 * is preserved. 9 * ==================================================== 10 * 11 * From: @(#)s_floor.c 5.1 93/09/24 12 */ 13 | 1/* 2 * ==================================================== 3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 4 * 5 * Developed at SunPro, a Sun Microsystems, Inc. business. 6 * Permission to use, copy, modify, and distribute this 7 * software is freely granted, provided that this notice 8 * is preserved. 9 * ==================================================== 10 * 11 * From: @(#)s_floor.c 5.1 93/09/24 12 */ 13 |
14#include <sys/cdefs.h> 15__FBSDID("$FreeBSD$"); | 14#ifndef lint 15static char rcsid[] = "$FreeBSD$"; 16#endif |
16 17/* 18 * truncl(x) 19 * Return x rounded toward 0 to integral value 20 * Method: 21 * Bit twiddling. 22 * Exception: 23 * Inexact flag raised if x not equal to truncl(x). --- 6 unchanged lines hidden (view full) --- 30#include "fpmath.h" 31 32#ifdef LDBL_IMPLICIT_NBIT 33#define MANH_SIZE (LDBL_MANH_SIZE + 1) 34#else 35#define MANH_SIZE LDBL_MANH_SIZE 36#endif 37 | 17 18/* 19 * truncl(x) 20 * Return x rounded toward 0 to integral value 21 * Method: 22 * Bit twiddling. 23 * Exception: 24 * Inexact flag raised if x not equal to truncl(x). --- 6 unchanged lines hidden (view full) --- 31#include "fpmath.h" 32 33#ifdef LDBL_IMPLICIT_NBIT 34#define MANH_SIZE (LDBL_MANH_SIZE + 1) 35#else 36#define MANH_SIZE LDBL_MANH_SIZE 37#endif 38 |
38static const long double huge = 1.0e300; 39static const float zero[] = { 0.0, -0.0 }; 40 | |
41long double 42truncl(long double x) 43{ 44 union IEEEl2bits u = { .e = x }; 45 int e = u.bits.exp - LDBL_MAX_EXP + 1; 46 47 if (e < MANH_SIZE - 1) { 48 if (e < 0) { /* raise inexact if x != 0 */ | 39long double 40truncl(long double x) 41{ 42 union IEEEl2bits u = { .e = x }; 43 int e = u.bits.exp - LDBL_MAX_EXP + 1; 44 45 if (e < MANH_SIZE - 1) { 46 if (e < 0) { /* raise inexact if x != 0 */ |
49 if (huge + x > 0.0) 50 u.e = zero[u.bits.sign]; | 47 u.e = 0.0; |
51 } else { 52 uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1); 53 if (((u.bits.manh & m) | u.bits.manl) == 0) 54 return (x); /* x is integral */ | 48 } else { 49 uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1); 50 if (((u.bits.manh & m) | u.bits.manl) == 0) 51 return (x); /* x is integral */ |
55 if (huge + x > 0.0) { /* raise inexact flag */ 56 u.bits.manh &= ~m; 57 u.bits.manl = 0; 58 } | 52 u.bits.manh &= ~m; 53 u.bits.manl = 0; |
59 } 60 } else if (e < LDBL_MANT_DIG - 1) { 61 uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1); 62 if ((u.bits.manl & m) == 0) 63 return (x); /* x is integral */ | 54 } 55 } else if (e < LDBL_MANT_DIG - 1) { 56 uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1); 57 if ((u.bits.manl & m) == 0) 58 return (x); /* x is integral */ |
64 if (huge + x > 0.0) /* raise inexact flag */ 65 u.bits.manl &= ~m; | 59 u.bits.manl &= ~m; |
66 } 67 return (u.e); 68} | 60 } 61 return (u.e); 62} |