mailing list of musl libc
 help / color / mirror / code / Atom feed
08abf326e866eb5ca79f3005b0cca24eaa54615d blob 1766 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
#include <limits.h>
#include <fenv.h>
#include <math.h>
#include "libm.h"

/*
If the result cannot be represented (overflow, nan), then
lrint raises the invalid exception.

Otherwise if the input was not an integer then the inexact
exception is raised.

C99 is a bit vague about whether inexact exception is
allowed to be raised when invalid is raised.
(F.9 explicitly allows spurious inexact exceptions, F.9.6.5
does not make it clear if that rule applies to lrint, but
IEEE 754r 7.8 seems to forbid spurious inexact exception in
the ineger conversion functions)

So we try to make sure that no spurious inexact exception is
raised in case of an overflow.

If the bit size of long > precision of double, then there
cannot be inexact rounding in case the result overflows,
otherwise LONG_MAX and LONG_MIN can be represented exactly
as a double.
*/

#if LONG_MAX < 1U<<53 && defined(FE_INEXACT)
#include <float.h>
#include <stdint.h>
#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1
#define EPS DBL_EPSILON
#elif FLT_EVAL_METHOD==2
#define EPS LDBL_EPSILON
#endif
#ifdef __GNUC__
/* avoid stack frame in lrint */
__attribute__((noinline))
#endif
static long lrint_slow(double x)
{
	#pragma STDC FENV_ACCESS ON
	int e;

	e = fetestexcept(FE_INEXACT);
	x = rint(x);
	if (!e && (x > LONG_MAX || x < LONG_MIN))
		feclearexcept(FE_INEXACT);
	/* conversion */
	return x;
}

long lrint(double x)
{
	union {double f; uint64_t i;} u = {x};
	uint32_t abstop = u.i>>32 & 0x7fffffff;

	if (abstop < 0x41dfffff) {
		/* |x| < 0x7ffffc00 */
		double_t y, t;
		union {double f; uint64_t i;} toint = {1/EPS};
		toint.i |= u.i & (1ULL << 63);
		t = toint.f;
		y = x + t - t;
		/* conversion */
		return y;
	}
	return lrint_slow(x);
}
#else
long lrint(double x)
{
	return rint(x);
}
#endif
debug log:

solving 08abf326 ...
found 08abf326 in https://inbox.vuxu.org/musl/20190921155234.GA22009@port70.net/
found bdca8b7c in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 bdca8b7cb82ec8b90ca9619e989dceb253b7ebc9	src/math/lrint.c

applying [1/1] https://inbox.vuxu.org/musl/20190921155234.GA22009@port70.net/
diff --git a/src/math/lrint.c b/src/math/lrint.c
index bdca8b7c..08abf326 100644

Checking patch src/math/lrint.c...
Applied patch src/math/lrint.c cleanly.

index at:
100644 08abf326e866eb5ca79f3005b0cca24eaa54615d	src/math/lrint.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).