userlibandfileserver/fileserver/inc/filesystem_utils.inl
changeset 10 36bfc973b146
equal deleted inserted replaced
9:96e5fb8b040d 10:36bfc973b146
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // 
       
    15 
       
    16 /**
       
    17     @file
       
    18     @internalTechnology
       
    19 */
       
    20 
       
    21 #if !defined(__FILESYSTEM_UTILS_INL__)
       
    22 #define __FILESYSTEM_UTILS_INL__
       
    23 
       
    24 
       
    25 //-----------------------------------------------------------------------------
       
    26 /** @return 2^aVal */
       
    27 inline TUint32 Pow2(TUint32 aVal)
       
    28     {
       
    29     ASSERT(aVal<32);
       
    30     return 1 << aVal;
       
    31     }
       
    32 
       
    33 //-----------------------------------------------------------------------------
       
    34 /** @return TUint32 value equals 2^aVal */
       
    35 inline TUint32 Pow2_32(TUint32 aVal)
       
    36     {
       
    37     ASSERT(aVal < 32);
       
    38     return 1 << aVal;
       
    39     }
       
    40 
       
    41 //-----------------------------------------------------------------------------
       
    42 /** @return TUint64 value equals 2^aVal */
       
    43 inline TUint64 Pow2_64(TUint32 aVal)
       
    44     {
       
    45     ASSERT(aVal < 64);
       
    46     return ((TUint64)1) << aVal;
       
    47     }
       
    48 
       
    49 //-----------------------------------------------------------------------------
       
    50 /**
       
    51     Indicates if a number passed in is a power of two
       
    52     @return ETrue if aVal is a power of 2 
       
    53 */
       
    54 inline TBool IsPowerOf2(TUint32 aVal)
       
    55     {
       
    56     if (aVal==0)
       
    57         return EFalse;
       
    58 
       
    59     return !(aVal & (aVal-1));
       
    60     }
       
    61 
       
    62 //-----------------------------------------------------------------------------
       
    63 /**
       
    64     Indicates if a number passed in is a power of two
       
    65     @return ETrue if aVal is a power of 2 
       
    66 */
       
    67 inline TBool IsPowerOf2_64(TUint64 aVal)
       
    68     {
       
    69     if (aVal==0)
       
    70         return EFalse;
       
    71 
       
    72     return !(aVal & (aVal-1));
       
    73 
       
    74     }
       
    75 
       
    76 //-----------------------------------------------------------------------------
       
    77 
       
    78 /**
       
    79     rounds down the given value to 2^aGranularityLog2
       
    80     @param  aVal                input value to round down
       
    81     @param  aGranularityLog2    Log2(granularity)
       
    82     @return rounded - down value
       
    83 */
       
    84 inline TUint32 RoundDown(TUint32 aVal, TUint32 aGranularityLog2)
       
    85 {
       
    86     ASSERT(aGranularityLog2 < 32);
       
    87     return (aVal >> aGranularityLog2) << aGranularityLog2;
       
    88 }
       
    89 
       
    90 //-----------------------------------------------------------------------------
       
    91 /**  
       
    92     Rounds up aVal to the 2^aGranularityLog2 
       
    93     For example: RoundUp(0x08, 2) == 0x08; RoundUp(0x08, 3) == 0x08; RoundUp(0x08, 4) == 0x10; RoundUp(0x19, 4) == 0x20
       
    94     
       
    95     @return rounded-up value
       
    96 */
       
    97 inline TUint32 RoundUp(TUint32 aVal, TUint32 aGranularityLog2)
       
    98     {
       
    99     ASSERT(aGranularityLog2 < 32);
       
   100                                          
       
   101     if( (aVal & ((1<<aGranularityLog2)-1)) == 0)
       
   102         return aVal;    
       
   103 
       
   104     aVal >>= aGranularityLog2;
       
   105     aVal++;
       
   106     aVal <<= aGranularityLog2;
       
   107 
       
   108     return aVal;
       
   109     }
       
   110 
       
   111 //-----------------------------------------------------------------------------
       
   112 
       
   113 /**
       
   114     @return Boolean exclusive OR between a1 and a2
       
   115     This function should be used on C-style TBool, which is, actually TInt type; Its '0' value means "False" and _any_ non-zero means "True"
       
   116     E.g: BoolXor(0x17, 0x4a) == EFalse;
       
   117 */
       
   118 TBool BoolXOR(TBool a1, TBool a2)
       
   119     {
       
   120     if(!a1 && !a2)        
       
   121         return EFalse;
       
   122     else if(a1 && a2)
       
   123         return EFalse;
       
   124     else
       
   125         return ETrue;
       
   126     }
       
   127 
       
   128 //-----------------------------------------------------------------------------
       
   129 
       
   130 /**
       
   131     Calculates the log2 of a number
       
   132     This is the explicitly inlined version. Extensive using it may result in a code bloat.
       
   133 
       
   134     @param aNum Number to calulate the log two of
       
   135     @return The log two of the number passed in
       
   136 */
       
   137 inline TUint32 Log2_inline(TUint32 aVal)
       
   138     {
       
   139     __ASSERT_COMPILE(sizeof(TUint32) == 4);
       
   140     ASSERT(aVal);
       
   141 
       
   142     TUint32 bitPos=31;
       
   143 
       
   144     if(!(aVal >> 16)) {bitPos-=16; aVal<<=16;}
       
   145     if(!(aVal >> 24)) {bitPos-=8;  aVal<<=8 ;}
       
   146     if(!(aVal >> 28)) {bitPos-=4;  aVal<<=4 ;}
       
   147     if(!(aVal >> 30)) {bitPos-=2;  aVal<<=2 ;}
       
   148     if(!(aVal >> 31)) {bitPos-=1;}
       
   149     
       
   150     return bitPos;
       
   151     }
       
   152 
       
   153 
       
   154 //-----------------------------------------------------------------------------
       
   155 /**
       
   156     Calculates number of '1' bits in the aVal
       
   157     This is the explicitly inlined version. Extensive using it may result in a code bloat.
       
   158 
       
   159     @param aVal some value
       
   160     @return number of '1' bits in the aVal
       
   161 */
       
   162 inline TUint32 Count1Bits_inline(TUint32 aVal)
       
   163     {
       
   164     if(!aVal)
       
   165         return 0;
       
   166 
       
   167     if(aVal == 0xFFFFFFFF)
       
   168         return 32;
       
   169 
       
   170     aVal = aVal - ((aVal >> 1) & 0x55555555);
       
   171     aVal = (aVal & 0x33333333) + ((aVal >> 2) & 0x33333333);
       
   172     aVal = (aVal + (aVal >> 4)) & 0x0f0f0f0f;
       
   173     aVal = aVal + (aVal >> 8);
       
   174     aVal = aVal + (aVal >> 16);
       
   175 
       
   176     return aVal & 0x3f;
       
   177     }
       
   178 
       
   179 
       
   180 
       
   181 
       
   182 
       
   183 
       
   184 #endif //__FILESYSTEM_UTILS_INL__
       
   185 
       
   186 
       
   187 
       
   188 
       
   189 
       
   190 
       
   191 
       
   192 
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 
       
   199 
       
   200 
       
   201