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