this repo has no description
1
2/*
3 * llrint.c
4 *
5 * by Ian Ollmann
6 *
7 * Copyright (c) 2007, Apple Inc. All Rights Reserved.
8 *
9 * C99 implementation of llrint.
10 */
11
12#include <math.h>
13#include <stdint.h>
14#include <limits.h>
15
16#warning *** untested -- we don't have tests for this
17
18long long llrint( double x )
19{
20 union{ double d; uint64_t u;} u = {x};
21
22 uint64_t absx = u.u & 0x7fffffffffffffffULL;
23
24 // handle x is zero, large, or NaN simply convert to long long and return
25 if( absx >= 0x4330000000000000ULL )
26 {
27 long long result = (long long) x; //set invalid if necessary
28
29 //Deal with overflow cases
30 if( x < (double) LONG_LONG_MIN )
31 return LONG_LONG_MIN;
32
33 // Note: float representation of LONG_LONG_MAX likely inexact,
34 // which is why we do >= here
35 if( x >= -((double) LONG_LONG_MIN) )
36 return LONG_LONG_MAX;
37
38 return result;
39 }
40
41 // copysign( 0x1.0p52, x )
42 u.u = (u.u & 0x8000000000000000ULL) | 0x4330000000000000ULL;
43
44 //round according to current rounding mode
45 x += u.d;
46 x -= u.d;
47
48 return (long long) x;
49}