GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libm/src/s_cosl.c Lines: 24 29 82.8 %
Date: 2017-11-13 Branches: 9 13 69.2 %

Line Branch Exec Source
1
/*	$OpenBSD: s_cosl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $	*/
2
/*-
3
 * Copyright (c) 2007 Steven G. Kargl
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice unmodified, this list of conditions, and the following
11
 *    disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
/*
29
 * Compute cos(x) for x where x is reduced to y = x - k * pi / 2.
30
 * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
31
 * an accuracy of <= 0.7412 ULP.
32
 */
33
34
#include <sys/types.h>
35
#include <machine/ieee.h>
36
#include <float.h>
37
#include <math.h>
38
39
#include "math_private.h"
40
41
#if LDBL_MANT_DIG == 64
42
#define	NX	3
43
#define	PREC	2
44
#elif LDBL_MANT_DIG == 113
45
#define	NX	5
46
#define	PREC	3
47
#else
48
#error "Unsupported long double format"
49
#endif
50
51
static const long double two24 = 1.67772160000000000000e+07L;
52
53
long double
54
cosl(long double x)
55
{
56
420
	union {
57
		long double e;
58
		struct ieee_ext bits;
59
	} z;
60
	int i, e0;
61
210
	double xd[NX], yd[PREC];
62
	long double hi, lo;
63
64
210
	z.e = x;
65
210
	z.bits.ext_sign = 0;
66
67
	/* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
68
210
	if (z.bits.ext_exp == 0)
69
14
		return (1.0);
70
71
	/* If x = NaN or Inf, then cos(x) = NaN. */
72
196
	if (z.bits.ext_exp == 32767)
73
		return ((x - x) / (x - x));
74
75
	/* Optimize the case where x is already within range. */
76
196
	if (z.e < M_PI_4)
77
56
		return (__kernel_cosl(z.e, 0));
78
79
	/* Split z.e into a 24-bit representation. */
80
140
	e0 = ilogbl(z.e) - 23;
81
140
	z.e = scalbnl(z.e, -e0);
82
1120
	for (i = 0; i < NX; i++) {
83
420
		xd[i] = (double)((int32_t)z.e);
84
420
		z.e = (z.e - xd[i]) * two24;
85
	}
86
87
	/* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
88
140
	e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
89
90
#if PREC == 2
91
140
	hi = (long double)yd[0] + yd[1];
92
140
	lo = yd[1] - (hi - yd[0]);
93
#else /* PREC == 3 */
94
	long double t;
95
	t = (long double)yd[2] + yd[1];
96
	hi = t + yd[0];
97
	lo = yd[0] - (hi - t);
98
#endif
99
100

140
	switch (e0 & 3) {
101
	case 0:
102
	    hi = __kernel_cosl(hi, lo);
103
	    break;
104
	case 1:
105
56
	    hi = - __kernel_sinl(hi, lo, 1);
106
56
	    break;
107
	case 2:
108
84
	    hi = - __kernel_cosl(hi, lo);
109
84
	    break;
110
	case 3:
111
	    hi = __kernel_sinl(hi, lo, 1);
112
	    break;
113
	}
114
115
140
	return (hi);
116
210
}
117
DEF_STD(cosl);