+
1 2 12
1 10 17
  1. #1
    morskaya_svinka       morskaya_svinka
    26.12.2006
    1,182
    ()
    0
    : 0 (: 0).

    Exclamation !!! !

    . 32- . 346.
    . 346=101011010. 8.
    0 1 2 3 4 5 6 7 8 - 101011010.
    . . .
    , - !

  2. #2
    MuratMusic       MuratMusic
    24.10.2006
    10
    ()
    0
    : 0 (: 0).

    Wink Re: !!! !

    . , ...

    , , , main()

    int a = 346; // ( , , ++)

    int i = 0; //
    //
    int i = -1; //

    while(a) // a
    {
    a = a >> 1; // 1
    i++;
    }

    printf(" %d ", i);
    MuratMusic; 08.02.2007 03:30. :

  3. #3
    morskaya_svinka       morskaya_svinka
    26.12.2006
    1,182
    ()
    0
    : 0 (: 0).

     Re: !!! !

    !

    - , ffffffffff , .
    morskaya_svinka; 09.02.2007 20:58. :
    , - !

  4. #4
    Nairo       Nairo
    03.11.2006
    503
    ()
    0
    : 0 (: 0).

     Re: !!! !

    - , ffffffffff , .
    , ..

    - Hacker's Delight( ., - , ) , - ,

    52
    , O(lgN)

    :
    unsigned int v; //     
    int r; //  
    
    static const int MPos[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
    
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    v = (v >> 1) + 1;
    
    r = MPos[(v * 0x077CB531UL) >> 27];
    Nairo; 12.02.2007 16:36.

  5. #5
    morskaya_svinka       morskaya_svinka
    26.12.2006
    1,182
    ()
    0
    : 0 (: 0).

     Re: !!! !

    ... , "Expression syntax" asm. .

    unsigned long ClockCycles ()
    {
    asm {rdtsc}
    }
    ClockCycles qnx.
    , - !

  6. #6
    Nairo       Nairo
    03.11.2006
    503
    ()
    0
    : 0 (: 0).

     Re: !!! !

    .. - :

    :
    #define RDTSC(cpu_c) \
    {	asm("rdtsc"); 	\
    	asm("mov %%eax, %0" : "=m" ((cpu_c).int32.lo) ); \
    	asm("mov %%edx, %0" : "=m" ((cpu_c).int32.hi) ); \
    }


    :
    #define RDTSC(cpu_c)   \
    {       __asm rdtsc    \
            __asm mov (cpu_c).int32.lo,eax  \
            __asm mov (cpu_c).int32.hi,edx  \
    }

  7. #7
    morskaya_svinka       morskaya_svinka
    26.12.2006
    1,182
    ()
    0
    : 0 (: 0).

     Re: !!! !

    - ...
    #define , { } . ...
    Declaration terminated incorrectly!
    , - !

  8. #8
    Nairo       Nairo
    03.11.2006
    503
    ()
    0
    : 0 (: 0).

     Re: !!! !

    #define .

    :
    typedef union {
      int64_t int64;
      union {
        int32_t lo;
        int32_t hi;
      } int32;
    }abc;
    
    #define RDTSC(cpu_c) \
    {	asm("rdtsc"); 	\
    	asm("mov %%eax, %0" : "=m" ((cpu_c).int32.lo) ); \
    	asm("mov %%edx, %0" : "=m" ((cpu_c).int32.hi) ); \
    }
    
    int main() {
      abc xxx;
      RDTSC(xxx);
    }
    3
    - gcc.. C99 ..
    , , ..

  9. #9
    morskaya_svinka       morskaya_svinka
    26.12.2006
    1,182
    ()
    0
    : 0 (: 0).

     Re: !!! !

    ?
    , - !

  10. #10
    Nairo       Nairo
    03.11.2006
    503
    ()
    0
    : 0 (: 0).

     Re: !!! !

    :
    #define COUNTER_LO(a) ((a).int32.lo)
    #define COUNTER_HI(a) ((a).int32.hi)
    #define COUNTER_VAL(a) ((a).int64)
    
    #define COUNTER_DIFF(a,b) \
    	(COUNTER_VAL(a)-COUNTER_VAL(b))
    
    /* ==================== GNU C and possibly other UNIX compilers ===================== */
    #ifndef WIN32
    
    #ifdef __GNUC__
    #define VOLATILE __volatile__
    #define ASM __asm__
    #else
    /* we can at least hope the following works, it probably won't */
    #define ASM asm
    #define VOLATILE 
    #endif
    
    #define INT64 unsigned long long
    #define INT32 unsigned int
    
    typedef union
    {       INT64 int64;
            struct {INT32 lo, hi;} int32;
    } tsc_counter;
    
    #define RDTSC(cpu_c) \
     ASM VOLATILE ("rdtsc" : "=a" ((cpu_c).int32.lo), "=d"((cpu_c).int32.hi))
    #define CPUID(x) \
     ASM VOLATILE ("cpuid" : "=a" (x) : "0" (x) : "bx", "cx", "dx" )
    
    int rdtsc_works() {
        tsc_counter dummy;
        RDTSC(dummy);
        /* I don't know how to do this right */
        return 1;
    }
    
    /* ======================== WIN32 ======================= */
    #else
    
    #define INT64 signed __int64
    #define INT32 unsigned __int32
    
    typedef union
    {       INT64 int64;
            struct {INT32 lo, hi;} int32;
    } tsc_counter;
    
    #define RDTSC(cpu_c)   \
    {       __asm rdtsc    \
            __asm mov (cpu_c).int32.lo,eax  \
            __asm mov (cpu_c).int32.hi,edx  \
    }
    
    #define CPUID(x) \
    { \
        __asm mov eax, x \
        __asm cpuid \
        __asm mov x, eax \
    }
    
    int rdtsc_works()
    {
        tsc_counter dummy;
        __try {
    	RDTSC(dummy);
        } __except ( 1) {
    	return 0;
        }
        return 1;
    }
    #endif
    
    /*
    #define RDTSC(cpu_c) \
    {	asm("rdtsc"); 	\
    	asm("mov %%eax, %0" : "=m" ((cpu_c).int32.lo) ); \
    	asm("mov %%edx, %0" : "=m" ((cpu_c).int32.hi) ); \
    }
    */
    
    /*	Read Time Stamp Counter
    	Read PMC 
    #define RDPMC0(cpu_c) \
    {		     	\
            __asm xor ecx,ecx	\
    	__asm rdpmc	\
    	__asm mov (cpu_c).int32.lo,eax	\
    	__asm mov (cpu_c).int32.hi,edx	\
    }
    */
    1. rdtsc.h
    2.
    #include "rdtsc.h"
    3.
    tsc_counter t0, t1, t_min;
    4.
    RDTSC(t0);
    ... - , -
    RDTSC(t1);
    5.
    COUNTER_VAL(t_min) = MIN(COUNTER_VAL(t_min), COUNTER_DIFF(t1, t0));
    6.

    9
    , - ..
    : QueryPerformanceCounter QueryPerformanceFrequency ..