2020-01-01 12:51:42 +01:00
|
|
|
// Copyright (C) 2019-2020 Free Software Foundation, Inc.
|
Define midpoint and lerp functions for C++20 (P0811R3)
The implementation of midpoint used for integral types is due to Howard
Hinnant and avoids a branch for int and larger types (but not for chars
and shorts).
The midpoint and lerp functions for floating point types come straight
from the P0811R3 proposal, with no attempt at optimization.
* include/c_compatibility/math.h [C++20] (lerp): Add using
declaration.
* include/c_global/cmath [C++20] (__cpp_lib_interpolate): Define.
(__lerp): Define function template to implement lerp.
(lerp(float, float, float), lerp(double, double, double))
(lerp(long double, long double, long double)): Define for C++20.
* include/std/numeric [C++20] (__cpp_lib_interpolate): Define.
(midpoint(T, T), midpoint(T*, T*)): Define.
* include/std::version [C++20] (__cpp_lib_interpolate): Define.
* testsuite/26_numerics/lerp.cc: New test.
* testsuite/26_numerics/midpoint/floating.cc: New test.
* testsuite/26_numerics/midpoint/integral.cc: New test.
* testsuite/26_numerics/midpoint/pointer.cc: New test.
From-SVN: r269398
2019-03-05 19:37:24 +01:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
// software; you can redistribute it and/or modify it under the
|
|
|
|
// terms of the GNU General Public License as published by the
|
|
|
|
// Free Software Foundation; either version 3, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this library; see the file COPYING3. If not see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// { dg-options "-std=gnu++2a" }
|
2019-05-24 17:39:35 +02:00
|
|
|
// { dg-do run { target c++2a } }
|
Define midpoint and lerp functions for C++20 (P0811R3)
The implementation of midpoint used for integral types is due to Howard
Hinnant and avoids a branch for int and larger types (but not for chars
and shorts).
The midpoint and lerp functions for floating point types come straight
from the P0811R3 proposal, with no attempt at optimization.
* include/c_compatibility/math.h [C++20] (lerp): Add using
declaration.
* include/c_global/cmath [C++20] (__cpp_lib_interpolate): Define.
(__lerp): Define function template to implement lerp.
(lerp(float, float, float), lerp(double, double, double))
(lerp(long double, long double, long double)): Define for C++20.
* include/std/numeric [C++20] (__cpp_lib_interpolate): Define.
(midpoint(T, T), midpoint(T*, T*)): Define.
* include/std::version [C++20] (__cpp_lib_interpolate): Define.
* testsuite/26_numerics/lerp.cc: New test.
* testsuite/26_numerics/midpoint/floating.cc: New test.
* testsuite/26_numerics/midpoint/integral.cc: New test.
* testsuite/26_numerics/midpoint/pointer.cc: New test.
From-SVN: r269398
2019-03-05 19:37:24 +01:00
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
#include <testsuite_hooks.h>
|
|
|
|
|
|
|
|
const int* p = nullptr;
|
|
|
|
static_assert(std::is_same_v<decltype(std::midpoint(p, p)), decltype(p)>);
|
|
|
|
// This is a GNU extension:
|
|
|
|
static_assert(noexcept(std::midpoint(p, p)));
|
|
|
|
|
|
|
|
struct test_type { };
|
|
|
|
template<typename T> decltype(std::midpoint((T*)0, (T*)0)) try_midpoint(int);
|
|
|
|
template<typename T> test_type try_midpoint(...);
|
|
|
|
template<typename T> constexpr bool no_midpoint()
|
|
|
|
{ return std::is_same_v<decltype(try_midpoint<T>()), test_type>; }
|
|
|
|
|
|
|
|
static_assert(no_midpoint<void>());
|
|
|
|
static_assert(no_midpoint<int()>());
|
|
|
|
static_assert(no_midpoint<int&>());
|
|
|
|
|
|
|
|
constexpr int ca[3] = {};
|
|
|
|
static_assert( std::midpoint(ca, ca+3) == ca+1 );
|
|
|
|
|
|
|
|
void
|
|
|
|
test01()
|
|
|
|
{
|
|
|
|
int a[4];
|
|
|
|
VERIFY( std::midpoint(a, a) == a );
|
|
|
|
VERIFY( std::midpoint(a, a+1) == a );
|
|
|
|
VERIFY( std::midpoint(a, a+2) == a+1 );
|
|
|
|
VERIFY( std::midpoint(a, a+3) == a+1 );
|
|
|
|
VERIFY( std::midpoint(a, a+4) == a+2 );
|
|
|
|
VERIFY( std::midpoint(a+1, a) == a+1 );
|
|
|
|
VERIFY( std::midpoint(a+2, a) == a+1 );
|
|
|
|
VERIFY( std::midpoint(a+3, a) == a+2 );
|
|
|
|
VERIFY( std::midpoint(a+4, a) == a+2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
test01();
|
|
|
|
}
|