Math.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <cmath>
  18. namespace folly {
  19. #ifndef __ANDROID__
  20. /**
  21. * Most platforms hopefully provide std::nextafter.
  22. */
  23. /* using override */ using std::nextafter;
  24. #else // !__ANDROID__
  25. /**
  26. * On Android, std::nextafter isn't implemented. However, the C functions and
  27. * compiler builtins are still provided. Using the GCC builtin is actually
  28. * slightly faster, as they're constexpr and the use cases within folly are in
  29. * constexpr context.
  30. */
  31. #if defined(__GNUC__) && !defined(__clang__)
  32. constexpr float nextafter(float x, float y) {
  33. return __builtin_nextafterf(x, y);
  34. }
  35. constexpr double nextafter(double x, double y) {
  36. return __builtin_nextafter(x, y);
  37. }
  38. constexpr long double nextafter(long double x, long double y) {
  39. return __builtin_nextafterl(x, y);
  40. }
  41. #else // __GNUC__
  42. inline float nextafter(float x, float y) {
  43. return ::nextafterf(x, y);
  44. }
  45. inline double nextafter(double x, double y) {
  46. return ::nextafter(x, y);
  47. }
  48. inline long double nextafter(long double x, long double y) {
  49. return ::nextafterl(x, y);
  50. }
  51. #endif // __GNUC__
  52. #endif // __ANDROID__
  53. } // namespace folly