Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/utility.h

    rf9feab8 r90152a4  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Thr Aug 17 11:38:00 2017
    13 // Update Count     : 34
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Sun May  6 22:24:16 2018
     13// Update Count     : 40
    1414//
    1515
     
    3131#include "Common/Indenter.h"
    3232
     33class Expression;
     34
    3335template< typename T >
    3436static inline T * maybeClone( const T *orig ) {
     
    98100}
    99101
     102template< typename SrcContainer, typename DestContainer, typename Predicate >
     103void cloneAll_if( const SrcContainer &src, DestContainer &dest, Predicate pred ) {
     104        std::back_insert_iterator< DestContainer > out( dest );
     105        for ( auto x : src ) {
     106                if ( pred(x) ) {
     107                        *out++ = x->clone();
     108                }
     109        } // while
     110}
     111
    100112template< typename Container >
    101113void assertAll( const Container &container ) {
     
    151163        return os.str();
    152164}
     165
     166#define toCString( ... ) toString( __VA_ARGS__ ).c_str()
    153167
    154168// replace element of list with all elements of another list
     
    424438}
    425439
    426 
     440// -----------------------------------------------------------------------------
     441// O(1) polymorphic integer ilog2, using clz, which returns the number of leading 0-bits, starting at the most
     442// significant bit (single instruction on x86)
     443
     444template<typename T>
     445inline
     446#if defined(__GNUC__) && __GNUC__ > 4
     447constexpr
     448#endif
     449T ilog2(const T & t) {
     450        if(std::is_integral<T>::value) {
     451                const constexpr int r = sizeof(t) * __CHAR_BIT__ - 1;
     452                if( sizeof(T) == sizeof(unsigned       int) ) return r - __builtin_clz  ( t );
     453                if( sizeof(T) == sizeof(unsigned      long) ) return r - __builtin_clzl ( t );
     454                if( sizeof(T) == sizeof(unsigned long long) ) return r - __builtin_clzll( t );
     455        }
     456        assert(false);
     457        return -1;
     458} // ilog2
     459
     460// -----------------------------------------------------------------------------
     461/// evaluates expr as a long long int. If second is false, expr could not be evaluated
     462std::pair<long long int, bool> eval(Expression * expr);
    427463
    428464// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.