Skip to content

Commit ac77d03

Browse files
committed
[libc++][math] Adding [[nodicard]]
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.htm - https://wg21.link/c.math
1 parent 4125e73 commit ac77d03

19 files changed

+757
-340
lines changed

libcxx/include/__math/error_functions.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,37 @@ namespace __math {
2323

2424
// erf
2525

26-
inline _LIBCPP_HIDE_FROM_ABI float erf(float __x) _NOEXCEPT { return __builtin_erff(__x); }
26+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float erf(float __x) _NOEXCEPT { return __builtin_erff(__x); }
2727

2828
template <class = int>
29-
_LIBCPP_HIDE_FROM_ABI double erf(double __x) _NOEXCEPT {
29+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double erf(double __x) _NOEXCEPT {
3030
return __builtin_erf(__x);
3131
}
3232

33-
inline _LIBCPP_HIDE_FROM_ABI long double erf(long double __x) _NOEXCEPT { return __builtin_erfl(__x); }
33+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double erf(long double __x) _NOEXCEPT {
34+
return __builtin_erfl(__x);
35+
}
3436

3537
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
36-
inline _LIBCPP_HIDE_FROM_ABI double erf(_A1 __x) _NOEXCEPT {
38+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double erf(_A1 __x) _NOEXCEPT {
3739
return __builtin_erf((double)__x);
3840
}
3941

4042
// erfc
4143

42-
inline _LIBCPP_HIDE_FROM_ABI float erfc(float __x) _NOEXCEPT { return __builtin_erfcf(__x); }
44+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float erfc(float __x) _NOEXCEPT { return __builtin_erfcf(__x); }
4345

4446
template <class = int>
45-
_LIBCPP_HIDE_FROM_ABI double erfc(double __x) _NOEXCEPT {
47+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double erfc(double __x) _NOEXCEPT {
4648
return __builtin_erfc(__x);
4749
}
4850

49-
inline _LIBCPP_HIDE_FROM_ABI long double erfc(long double __x) _NOEXCEPT { return __builtin_erfcl(__x); }
51+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double erfc(long double __x) _NOEXCEPT {
52+
return __builtin_erfcl(__x);
53+
}
5054

5155
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
52-
inline _LIBCPP_HIDE_FROM_ABI double erfc(_A1 __x) _NOEXCEPT {
56+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double erfc(_A1 __x) _NOEXCEPT {
5357
return __builtin_erfc((double)__x);
5458
}
5559

libcxx/include/__math/exponential_functions.h

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,139 +26,155 @@ namespace __math {
2626

2727
// exp
2828

29-
inline _LIBCPP_HIDE_FROM_ABI float exp(float __x) _NOEXCEPT { return __builtin_expf(__x); }
29+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float exp(float __x) _NOEXCEPT { return __builtin_expf(__x); }
3030

3131
template <class = int>
32-
_LIBCPP_HIDE_FROM_ABI double exp(double __x) _NOEXCEPT {
32+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double exp(double __x) _NOEXCEPT {
3333
return __builtin_exp(__x);
3434
}
3535

36-
inline _LIBCPP_HIDE_FROM_ABI long double exp(long double __x) _NOEXCEPT { return __builtin_expl(__x); }
36+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double exp(long double __x) _NOEXCEPT {
37+
return __builtin_expl(__x);
38+
}
3739

3840
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
39-
inline _LIBCPP_HIDE_FROM_ABI double exp(_A1 __x) _NOEXCEPT {
41+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double exp(_A1 __x) _NOEXCEPT {
4042
return __builtin_exp((double)__x);
4143
}
4244

4345
// frexp
4446

45-
inline _LIBCPP_HIDE_FROM_ABI float frexp(float __x, int* __e) _NOEXCEPT { return __builtin_frexpf(__x, __e); }
47+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float frexp(float __x, int* __e) _NOEXCEPT {
48+
return __builtin_frexpf(__x, __e);
49+
}
4650

4751
template <class = int>
48-
_LIBCPP_HIDE_FROM_ABI double frexp(double __x, int* __e) _NOEXCEPT {
52+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double frexp(double __x, int* __e) _NOEXCEPT {
4953
return __builtin_frexp(__x, __e);
5054
}
5155

52-
inline _LIBCPP_HIDE_FROM_ABI long double frexp(long double __x, int* __e) _NOEXCEPT {
56+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double frexp(long double __x, int* __e) _NOEXCEPT {
5357
return __builtin_frexpl(__x, __e);
5458
}
5559

5660
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
57-
inline _LIBCPP_HIDE_FROM_ABI double frexp(_A1 __x, int* __e) _NOEXCEPT {
61+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double frexp(_A1 __x, int* __e) _NOEXCEPT {
5862
return __builtin_frexp((double)__x, __e);
5963
}
6064

6165
// ldexp
6266

63-
inline _LIBCPP_HIDE_FROM_ABI float ldexp(float __x, int __e) _NOEXCEPT { return __builtin_ldexpf(__x, __e); }
67+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float ldexp(float __x, int __e) _NOEXCEPT {
68+
return __builtin_ldexpf(__x, __e);
69+
}
6470

6571
template <class = int>
66-
_LIBCPP_HIDE_FROM_ABI double ldexp(double __x, int __e) _NOEXCEPT {
72+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double ldexp(double __x, int __e) _NOEXCEPT {
6773
return __builtin_ldexp(__x, __e);
6874
}
6975

70-
inline _LIBCPP_HIDE_FROM_ABI long double ldexp(long double __x, int __e) _NOEXCEPT {
76+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double ldexp(long double __x, int __e) _NOEXCEPT {
7177
return __builtin_ldexpl(__x, __e);
7278
}
7379

7480
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
75-
inline _LIBCPP_HIDE_FROM_ABI double ldexp(_A1 __x, int __e) _NOEXCEPT {
81+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double ldexp(_A1 __x, int __e) _NOEXCEPT {
7682
return __builtin_ldexp((double)__x, __e);
7783
}
7884

7985
// exp2
8086

81-
inline _LIBCPP_HIDE_FROM_ABI float exp2(float __x) _NOEXCEPT { return __builtin_exp2f(__x); }
87+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float exp2(float __x) _NOEXCEPT { return __builtin_exp2f(__x); }
8288

8389
template <class = int>
84-
_LIBCPP_HIDE_FROM_ABI double exp2(double __x) _NOEXCEPT {
90+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double exp2(double __x) _NOEXCEPT {
8591
return __builtin_exp2(__x);
8692
}
8793

88-
inline _LIBCPP_HIDE_FROM_ABI long double exp2(long double __x) _NOEXCEPT { return __builtin_exp2l(__x); }
94+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double exp2(long double __x) _NOEXCEPT {
95+
return __builtin_exp2l(__x);
96+
}
8997

9098
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
91-
inline _LIBCPP_HIDE_FROM_ABI double exp2(_A1 __x) _NOEXCEPT {
99+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double exp2(_A1 __x) _NOEXCEPT {
92100
return __builtin_exp2((double)__x);
93101
}
94102

95103
// expm1
96104

97-
inline _LIBCPP_HIDE_FROM_ABI float expm1(float __x) _NOEXCEPT { return __builtin_expm1f(__x); }
105+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float expm1(float __x) _NOEXCEPT { return __builtin_expm1f(__x); }
98106

99107
template <class = int>
100-
_LIBCPP_HIDE_FROM_ABI double expm1(double __x) _NOEXCEPT {
108+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double expm1(double __x) _NOEXCEPT {
101109
return __builtin_expm1(__x);
102110
}
103111

104-
inline _LIBCPP_HIDE_FROM_ABI long double expm1(long double __x) _NOEXCEPT { return __builtin_expm1l(__x); }
112+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double expm1(long double __x) _NOEXCEPT {
113+
return __builtin_expm1l(__x);
114+
}
105115

106116
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
107-
inline _LIBCPP_HIDE_FROM_ABI double expm1(_A1 __x) _NOEXCEPT {
117+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double expm1(_A1 __x) _NOEXCEPT {
108118
return __builtin_expm1((double)__x);
109119
}
110120

111121
// scalbln
112122

113-
inline _LIBCPP_HIDE_FROM_ABI float scalbln(float __x, long __y) _NOEXCEPT { return __builtin_scalblnf(__x, __y); }
123+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float scalbln(float __x, long __y) _NOEXCEPT {
124+
return __builtin_scalblnf(__x, __y);
125+
}
114126

115127
template <class = int>
116-
_LIBCPP_HIDE_FROM_ABI double scalbln(double __x, long __y) _NOEXCEPT {
128+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double scalbln(double __x, long __y) _NOEXCEPT {
117129
return __builtin_scalbln(__x, __y);
118130
}
119131

120-
inline _LIBCPP_HIDE_FROM_ABI long double scalbln(long double __x, long __y) _NOEXCEPT {
132+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double scalbln(long double __x, long __y) _NOEXCEPT {
121133
return __builtin_scalblnl(__x, __y);
122134
}
123135

124136
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
125-
inline _LIBCPP_HIDE_FROM_ABI double scalbln(_A1 __x, long __y) _NOEXCEPT {
137+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double scalbln(_A1 __x, long __y) _NOEXCEPT {
126138
return __builtin_scalbln((double)__x, __y);
127139
}
128140

129141
// scalbn
130142

131-
inline _LIBCPP_HIDE_FROM_ABI float scalbn(float __x, int __y) _NOEXCEPT { return __builtin_scalbnf(__x, __y); }
143+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float scalbn(float __x, int __y) _NOEXCEPT {
144+
return __builtin_scalbnf(__x, __y);
145+
}
132146

133147
template <class = int>
134-
_LIBCPP_HIDE_FROM_ABI double scalbn(double __x, int __y) _NOEXCEPT {
148+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double scalbn(double __x, int __y) _NOEXCEPT {
135149
return __builtin_scalbn(__x, __y);
136150
}
137151

138-
inline _LIBCPP_HIDE_FROM_ABI long double scalbn(long double __x, int __y) _NOEXCEPT {
152+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double scalbn(long double __x, int __y) _NOEXCEPT {
139153
return __builtin_scalbnl(__x, __y);
140154
}
141155

142156
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
143-
inline _LIBCPP_HIDE_FROM_ABI double scalbn(_A1 __x, int __y) _NOEXCEPT {
157+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double scalbn(_A1 __x, int __y) _NOEXCEPT {
144158
return __builtin_scalbn((double)__x, __y);
145159
}
146160

147161
// pow
148162

149-
inline _LIBCPP_HIDE_FROM_ABI float pow(float __x, float __y) _NOEXCEPT { return __builtin_powf(__x, __y); }
163+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float pow(float __x, float __y) _NOEXCEPT {
164+
return __builtin_powf(__x, __y);
165+
}
150166

151167
template <class = int>
152-
_LIBCPP_HIDE_FROM_ABI double pow(double __x, double __y) _NOEXCEPT {
168+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double pow(double __x, double __y) _NOEXCEPT {
153169
return __builtin_pow(__x, __y);
154170
}
155171

156-
inline _LIBCPP_HIDE_FROM_ABI long double pow(long double __x, long double __y) _NOEXCEPT {
172+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double pow(long double __x, long double __y) _NOEXCEPT {
157173
return __builtin_powl(__x, __y);
158174
}
159175

160176
template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
161-
inline _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2> pow(_A1 __x, _A2 __y) _NOEXCEPT {
177+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2> pow(_A1 __x, _A2 __y) _NOEXCEPT {
162178
using __result_type = __promote_t<_A1, _A2>;
163179
static_assert(!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value), "");
164180
return __math::pow((__result_type)__x, (__result_type)__y);

libcxx/include/__math/fdim.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2323

2424
namespace __math {
2525

26-
inline _LIBCPP_HIDE_FROM_ABI float fdim(float __x, float __y) _NOEXCEPT { return __builtin_fdimf(__x, __y); }
26+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float fdim(float __x, float __y) _NOEXCEPT {
27+
return __builtin_fdimf(__x, __y);
28+
}
2729

2830
template <class = int>
29-
_LIBCPP_HIDE_FROM_ABI double fdim(double __x, double __y) _NOEXCEPT {
31+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double fdim(double __x, double __y) _NOEXCEPT {
3032
return __builtin_fdim(__x, __y);
3133
}
3234

33-
inline _LIBCPP_HIDE_FROM_ABI long double fdim(long double __x, long double __y) _NOEXCEPT {
35+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double fdim(long double __x, long double __y) _NOEXCEPT {
3436
return __builtin_fdiml(__x, __y);
3537
}
3638

3739
template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
38-
inline _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2> fdim(_A1 __x, _A2 __y) _NOEXCEPT {
40+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2> fdim(_A1 __x, _A2 __y) _NOEXCEPT {
3941
using __result_type = __promote_t<_A1, _A2>;
4042
static_assert(!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value), "");
4143
return __math::fdim((__result_type)__x, (__result_type)__y);

libcxx/include/__math/fma.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,25 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2323

2424
namespace __math {
2525

26-
inline _LIBCPP_HIDE_FROM_ABI float fma(float __x, float __y, float __z) _NOEXCEPT {
26+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float fma(float __x, float __y, float __z) _NOEXCEPT {
2727
return __builtin_fmaf(__x, __y, __z);
2828
}
2929

3030
template <class = int>
31-
_LIBCPP_HIDE_FROM_ABI double fma(double __x, double __y, double __z) _NOEXCEPT {
31+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double fma(double __x, double __y, double __z) _NOEXCEPT {
3232
return __builtin_fma(__x, __y, __z);
3333
}
3434

35-
inline _LIBCPP_HIDE_FROM_ABI long double fma(long double __x, long double __y, long double __z) _NOEXCEPT {
35+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double
36+
fma(long double __x, long double __y, long double __z) _NOEXCEPT {
3637
return __builtin_fmal(__x, __y, __z);
3738
}
3839

3940
template <class _A1,
4041
class _A2,
4142
class _A3,
4243
__enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value && is_arithmetic<_A3>::value, int> = 0>
43-
inline _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2, _A3> fma(_A1 __x, _A2 __y, _A3 __z) _NOEXCEPT {
44+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __promote_t<_A1, _A2, _A3> fma(_A1 __x, _A2 __y, _A3 __z) _NOEXCEPT {
4445
using __result_type = __promote_t<_A1, _A2, _A3>;
4546
static_assert(
4647
!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value && _IsSame<_A3, __result_type>::value),

libcxx/include/__math/gamma.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,39 @@ namespace __math {
2323

2424
// lgamma
2525

26-
inline _LIBCPP_HIDE_FROM_ABI float lgamma(float __x) _NOEXCEPT { return __builtin_lgammaf(__x); }
26+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float lgamma(float __x) _NOEXCEPT { return __builtin_lgammaf(__x); }
2727

2828
template <class = int>
29-
_LIBCPP_HIDE_FROM_ABI double lgamma(double __x) _NOEXCEPT {
29+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double lgamma(double __x) _NOEXCEPT {
3030
return __builtin_lgamma(__x);
3131
}
3232

33-
inline _LIBCPP_HIDE_FROM_ABI long double lgamma(long double __x) _NOEXCEPT { return __builtin_lgammal(__x); }
33+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double lgamma(long double __x) _NOEXCEPT {
34+
return __builtin_lgammal(__x);
35+
}
3436

3537
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
36-
inline _LIBCPP_HIDE_FROM_ABI double lgamma(_A1 __x) _NOEXCEPT {
38+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double lgamma(_A1 __x) _NOEXCEPT {
3739
return __builtin_lgamma((double)__x);
3840
}
3941

4042
// nan
4143

4244
// tgamma
4345

44-
inline _LIBCPP_HIDE_FROM_ABI float tgamma(float __x) _NOEXCEPT { return __builtin_tgammaf(__x); }
46+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float tgamma(float __x) _NOEXCEPT { return __builtin_tgammaf(__x); }
4547

4648
template <class = int>
47-
_LIBCPP_HIDE_FROM_ABI double tgamma(double __x) _NOEXCEPT {
49+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double tgamma(double __x) _NOEXCEPT {
4850
return __builtin_tgamma(__x);
4951
}
5052

51-
inline _LIBCPP_HIDE_FROM_ABI long double tgamma(long double __x) _NOEXCEPT { return __builtin_tgammal(__x); }
53+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double tgamma(long double __x) _NOEXCEPT {
54+
return __builtin_tgammal(__x);
55+
}
5256

5357
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
54-
inline _LIBCPP_HIDE_FROM_ABI double tgamma(_A1 __x) _NOEXCEPT {
58+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double tgamma(_A1 __x) _NOEXCEPT {
5559
return __builtin_tgamma((double)__x);
5660
}
5761

0 commit comments

Comments
 (0)