// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//==============================================================================
// NOTICE: This file has been modified by Green Hills Software, Inc.  Such
// modifications are subject to copyright by Green Hills Software, Inc.  See the
// "Green Hills Software Modifications" section in the LICENSE.txt included with
// this distribution for more information.
//==============================================================================

#ifndef _LIBCPP___BITS
#define _LIBCPP___BITS

#include <__config>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

#if defined(_LIBCPP_COMPILER_GHS)
#include "__support/ghs/support.h"
#endif

#if defined(__ghs__)
  #pragma ghs start_cxx_lib_header
  #pragma ghs startdata
  #if defined(__ghs_max_pack_value)
    #pragma pack (push, __ghs_max_pack_value)
  #endif
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

// ===------ GHS code ---
#ifndef __ghs__
#define _LIBCPP_CONSTEXPR_GHS _LIBCPP_CONSTEXPR
#else
#define _LIBCPP_CONSTEXPR_GHS _LIBCPP_CONSTEXPR_SINCE_CXX14
#endif
// ===------ end GHS code ---

// Note: the following code is modified, all _LIBCPP_CONSTEXPR_GHS was _LIBCPP_CONSTEXPR originally
// Note: The pragmas below suppress diagnostic for non-constexpr use of 
//       std::is_constant_evaluated(), which occurs when compiling with C++11

#pragma ghs nowarning 3684
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_ctz(unsigned __x)           _NOEXCEPT { return __builtin_ctz(__x); }
#pragma ghs endnowarning 3684

#pragma ghs nowarning 3684
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_ctz(unsigned long __x)      _NOEXCEPT { return __builtin_ctzl(__x); }
#pragma ghs endnowarning 3684

#pragma ghs nowarning 3684
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
#pragma ghs endnowarning 3684


#pragma ghs nowarning 3684
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_clz(unsigned __x)           _NOEXCEPT { return __builtin_clz(__x); }
#pragma ghs endnowarning 3684

#pragma ghs nowarning 3684
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_clz(unsigned long __x)      _NOEXCEPT { return __builtin_clzl(__x); }
#pragma ghs endnowarning 3684

#pragma ghs nowarning 3684
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
#pragma ghs endnowarning 3684

#  ifndef _LIBCPP_HAS_NO_INT128
#pragma ghs nowarning 3684
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_clz(__uint128_t __x) _NOEXCEPT {
  // The function is written in this form due to C++ constexpr limitations.
  // The algorithm:
  // - Test whether any bit in the high 64-bits is set
  // - No bits set:
  //   - The high 64-bits contain 64 leading zeros,
  //   - Add the result of the low 64-bits.
  // - Any bits set:
  //   - The number of leading zeros of the input is the number of leading
  //     zeros in the high 64-bits.
  return ((__x >> 64) == 0)
           ? (64 + __builtin_clzll(static_cast<unsigned long long>(__x)))
           : __builtin_clzll(static_cast<unsigned long long>(__x >> 64));
}
#pragma ghs endnowarning 3684
#  endif

inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_popcount(unsigned __x)           _NOEXCEPT { return __builtin_popcount(__x); }

inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_popcount(unsigned long __x)      _NOEXCEPT { return __builtin_popcountl(__x); }

inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_GHS
int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }

_LIBCPP_END_NAMESPACE_STD

#if defined(__ghs__)
  #if defined(__ghs_max_pack_value)
    #pragma pack(pop)
  #endif
  #pragma ghs enddata
  #pragma ghs end_cxx_lib_header
#endif

#endif // _LIBCPP___BITS
