1*38fd1498Szrj// <numeric> -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj// Copyright (C) 2001-2018 Free Software Foundation, Inc. 4*38fd1498Szrj// 5*38fd1498Szrj// This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj// software; you can redistribute it and/or modify it under the 7*38fd1498Szrj// terms of the GNU General Public License as published by the 8*38fd1498Szrj// Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj// any later version. 10*38fd1498Szrj 11*38fd1498Szrj// This library is distributed in the hope that it will be useful, 12*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj// GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj// 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj// You should have received a copy of the GNU General Public License and 21*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj// <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj/* 26*38fd1498Szrj * 27*38fd1498Szrj * Copyright (c) 1994 28*38fd1498Szrj * Hewlett-Packard Company 29*38fd1498Szrj * 30*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 31*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 32*38fd1498Szrj * provided that the above copyright notice appear in all copies and 33*38fd1498Szrj * that both that copyright notice and this permission notice appear 34*38fd1498Szrj * in supporting documentation. Hewlett-Packard Company makes no 35*38fd1498Szrj * representations about the suitability of this software for any 36*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 37*38fd1498Szrj * 38*38fd1498Szrj * 39*38fd1498Szrj * Copyright (c) 1996,1997 40*38fd1498Szrj * Silicon Graphics Computer Systems, Inc. 41*38fd1498Szrj * 42*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 43*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 44*38fd1498Szrj * provided that the above copyright notice appear in all copies and 45*38fd1498Szrj * that both that copyright notice and this permission notice appear 46*38fd1498Szrj * in supporting documentation. Silicon Graphics makes no 47*38fd1498Szrj * representations about the suitability of this software for any 48*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 49*38fd1498Szrj */ 50*38fd1498Szrj 51*38fd1498Szrj/** @file include/numeric 52*38fd1498Szrj * This is a Standard C++ Library header. 53*38fd1498Szrj */ 54*38fd1498Szrj 55*38fd1498Szrj#ifndef _GLIBCXX_NUMERIC 56*38fd1498Szrj#define _GLIBCXX_NUMERIC 1 57*38fd1498Szrj 58*38fd1498Szrj#pragma GCC system_header 59*38fd1498Szrj 60*38fd1498Szrj#include <bits/c++config.h> 61*38fd1498Szrj#include <bits/stl_iterator_base_types.h> 62*38fd1498Szrj#include <bits/stl_numeric.h> 63*38fd1498Szrj 64*38fd1498Szrj#ifdef _GLIBCXX_PARALLEL 65*38fd1498Szrj# include <parallel/numeric> 66*38fd1498Szrj#endif 67*38fd1498Szrj 68*38fd1498Szrj/** 69*38fd1498Szrj * @defgroup numerics Numerics 70*38fd1498Szrj * 71*38fd1498Szrj * Components for performing numeric operations. Includes support for 72*38fd1498Szrj * for complex number types, random number generation, numeric 73*38fd1498Szrj * (n-at-a-time) arrays, generalized numeric algorithms, and special 74*38fd1498Szrj * math functions. 75*38fd1498Szrj */ 76*38fd1498Szrj 77*38fd1498Szrj#if __cplusplus >= 201402L 78*38fd1498Szrj#include <type_traits> 79*38fd1498Szrj 80*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default) 81*38fd1498Szrj{ 82*38fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_VERSION 83*38fd1498Szrj 84*38fd1498Szrjnamespace __detail 85*38fd1498Szrj{ 86*38fd1498Szrj // std::abs is not constexpr and doesn't support unsigned integers. 87*38fd1498Szrj template<typename _Tp> 88*38fd1498Szrj constexpr 89*38fd1498Szrj enable_if_t<__and_<is_integral<_Tp>, is_signed<_Tp>>::value, _Tp> 90*38fd1498Szrj __abs_integral(_Tp __val) 91*38fd1498Szrj { return __val < 0 ? -__val : __val; } 92*38fd1498Szrj 93*38fd1498Szrj template<typename _Tp> 94*38fd1498Szrj constexpr 95*38fd1498Szrj enable_if_t<__and_<is_integral<_Tp>, is_unsigned<_Tp>>::value, _Tp> 96*38fd1498Szrj __abs_integral(_Tp __val) 97*38fd1498Szrj { return __val; } 98*38fd1498Szrj 99*38fd1498Szrj void __abs_integral(bool) = delete; 100*38fd1498Szrj 101*38fd1498Szrj template<typename _Mn, typename _Nn> 102*38fd1498Szrj constexpr common_type_t<_Mn, _Nn> 103*38fd1498Szrj __gcd(_Mn __m, _Nn __n) 104*38fd1498Szrj { 105*38fd1498Szrj return __m == 0 ? __detail::__abs_integral(__n) 106*38fd1498Szrj : __n == 0 ? __detail::__abs_integral(__m) 107*38fd1498Szrj : __detail::__gcd(__n, __m % __n); 108*38fd1498Szrj } 109*38fd1498Szrj 110*38fd1498Szrj /// Least common multiple 111*38fd1498Szrj template<typename _Mn, typename _Nn> 112*38fd1498Szrj constexpr common_type_t<_Mn, _Nn> 113*38fd1498Szrj __lcm(_Mn __m, _Nn __n) 114*38fd1498Szrj { 115*38fd1498Szrj return (__m != 0 && __n != 0) 116*38fd1498Szrj ? (__detail::__abs_integral(__m) / __detail::__gcd(__m, __n)) 117*38fd1498Szrj * __detail::__abs_integral(__n) 118*38fd1498Szrj : 0; 119*38fd1498Szrj } 120*38fd1498Szrj} // namespace __detail 121*38fd1498Szrj 122*38fd1498Szrj#if __cplusplus > 201402L 123*38fd1498Szrj 124*38fd1498Szrj#define __cpp_lib_gcd_lcm 201606 125*38fd1498Szrj// These were used in drafts of SD-6: 126*38fd1498Szrj#define __cpp_lib_gcd 201606 127*38fd1498Szrj#define __cpp_lib_lcm 201606 128*38fd1498Szrj 129*38fd1498Szrj /// Greatest common divisor 130*38fd1498Szrj template<typename _Mn, typename _Nn> 131*38fd1498Szrj constexpr common_type_t<_Mn, _Nn> 132*38fd1498Szrj gcd(_Mn __m, _Nn __n) 133*38fd1498Szrj { 134*38fd1498Szrj static_assert(is_integral_v<_Mn>, "gcd arguments are integers"); 135*38fd1498Szrj static_assert(is_integral_v<_Nn>, "gcd arguments are integers"); 136*38fd1498Szrj static_assert(!is_same_v<remove_cv_t<_Mn>, bool>, 137*38fd1498Szrj "gcd arguments are not bools"); 138*38fd1498Szrj static_assert(!is_same_v<remove_cv_t<_Nn>, bool>, 139*38fd1498Szrj "gcd arguments are not bools"); 140*38fd1498Szrj return __detail::__gcd(__m, __n); 141*38fd1498Szrj } 142*38fd1498Szrj 143*38fd1498Szrj /// Least common multiple 144*38fd1498Szrj template<typename _Mn, typename _Nn> 145*38fd1498Szrj constexpr common_type_t<_Mn, _Nn> 146*38fd1498Szrj lcm(_Mn __m, _Nn __n) 147*38fd1498Szrj { 148*38fd1498Szrj static_assert(is_integral_v<_Mn>, "lcm arguments are integers"); 149*38fd1498Szrj static_assert(is_integral_v<_Nn>, "lcm arguments are integers"); 150*38fd1498Szrj static_assert(!is_same_v<remove_cv_t<_Mn>, bool>, 151*38fd1498Szrj "lcm arguments are not bools"); 152*38fd1498Szrj static_assert(!is_same_v<remove_cv_t<_Nn>, bool>, 153*38fd1498Szrj "lcm arguments are not bools"); 154*38fd1498Szrj return __detail::__lcm(__m, __n); 155*38fd1498Szrj } 156*38fd1498Szrj 157*38fd1498Szrj#endif // C++17 158*38fd1498Szrj 159*38fd1498Szrj_GLIBCXX_END_NAMESPACE_VERSION 160*38fd1498Szrj} // namespace std 161*38fd1498Szrj 162*38fd1498Szrj#endif // C++14 163*38fd1498Szrj 164*38fd1498Szrj 165*38fd1498Szrj#endif /* _GLIBCXX_NUMERIC */ 166