Thread: project Euler problem 5 solution

  1. #31
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    Quote Originally Posted by Click_here View Post
    Code:
    #include "cycle_counting.h"
    Okay... You can keep your secretes :P
    No secret at all... nobody, yet, had the curiosity to ask me about this. Here's the code to measure clocks on SandyBridge and above processors (Intel) and most AMD ones, as well on ARM AArch64:
    Code:
    /* cycle_counting.h */
    #ifndef CYCLE_COUNTING_INCLUDED__
    #define CYCLE_COUNTING_INCLUDED__
    
    #ifndef __GNUC__
    # error Works only on GCC
    #endif
    
    /* ==========================================
        Quick & Dirty cycle counting...
    
        Usage:
    
          counter_T cnt;
    
          cnt = BEGIN_TSC();      
          f();
          END_TSC(&cnt);
    
        For i386 and x86-64 you can define SYNC_MEM if want a better
        processor serialization before measuring the cycles (requires SSE2).
       ========================================== */
    #include <stdint.h>
    
    // Non volatitle to avoid optimizations.
    typedef volatile uint64_t counter_T;
    
    #if defined(__x86_64__)
      inline counter_T BEGIN_TSC( void )
      {
        uint32_t a, d;
    
        __asm__ __volatile__ (
    # ifdef SYNC_MEM
          "mfence\n\t"
    # endif
          "xorl %%eax,%%eax\n\t"
          "cpuid\n\t"
          "rdtsc" : "=a" (a), "=d" (d) :: "%rbx", "%rcx"
        );
    
        return a | ((uint64_t)d << 32);
      }
    
      inline void END_TSC( counter_T *cptr )
      {
        uint32_t a, d;
    
        __asm__ __volatile__ (
          "rdtscp" : "=a" (a), "=d" (d) :: "%rcx"
        );
    
        *cptr = (a | ((uint64_t)d << 32)) - *cptr;;
      }
    #elif defined(__i386__)
      inline counter_T BEGIN_TSC( void )
      {
        uint32_t a, d;
    
        __asm__ __volatile__ (
    // mfence available only to SSE2 and above.
    # ifdef __SSE2__
    #   ifdef SYNC_MEM
          "mfence\n\t"
    #   endif
    # endif
          "xorl %%eax,%%eax\n\t"
          "cpuid\n\t"
          "rdtsc" : "=a" (a), "=d" (d) :: "%ebx", "%ecx"
        );
    
        return a | ((uint64_t)d << 32);
      }
    
      inline void END_TSC( counter_T *cptr )
      {
        uint32_t a, d;
    
        __asm__ __volatile__ (
          "rdtscp" : "=a" (a), "=d" (d) :: "%ecx"
        );
    
        *cptr = (a | ((uint64_t)d << 32)) - *cptr;
      }
    #elif defined(__aarch64__)
      inline counter_T BEGIN_TSC( void )
      {
        uint64_t count;
    
        __asm__ __volatile__ ( 
    # ifdef SYNC_MEM
        "dmb\n\t"
    # endif
        "mrs %0,cntvct_el0" : "=r" (count) );
    
        return count;
      }
    
      inline void END_TSC( counter_T *cptr )
      {
        uint64_t count;
    
        __asm__ __volatile__ ( "mrs %0,cntvct_el0" : "=r" (count) );
    
        *cptr = count - *cptr;
      }
    #else
    # error i386, x86-64 and AArch64 only.
    #endif
    
    #endif

  2. #32
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    @flp - "\033[31;" does not work well on Windows :P
    Fact - Beethoven wrote his first symphony in C

  3. #33
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    Quote Originally Posted by Click_here View Post
    @flp - "\033[31;" does not work well on Windows :P
    WTF is "Windows"?

  4. #34
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Quote Originally Posted by flp1969 View Post
    WTF is "Windows"?
    Best reponse ever - Haha
    Fact - Beethoven wrote his first symphony in C

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,507
    Quote Originally Posted by flp1969 View Post
    WTF is "Windows"?
    Noun. An opening in the wall or roof of a building or vehicle, fitted with glass in a frame to admit light or air and allow people to see out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    Quote Originally Posted by laserlight View Post
    Noun. An opening in the wall or roof of a building or vehicle, fitted with glass in a frame to admit light or air and allow people to see out.
    Hehehehe...

  7. #37
    Registered User
    Join Date
    May 2019
    Posts
    213
    Quote Originally Posted by flp1969 View Post
    WTF is "Windows"?
    The aperture through which Microsoft invades your privacy and attempts to control all you see and do, with partnerships invited through the Internet to observe and influence what you buy.

  8. #38
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    ok those last few comments made me spit choke on my cuppa lol

  9. #39
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    @ flp did you try running the for loop from max down to i=3 if so did it make any time difference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help!! Problem 3 of Project Euler
    By i++ in forum C Programming
    Replies: 8
    Last Post: 08-15-2013, 06:59 PM
  2. Project Euler Problem 2 Solution
    By i++ in forum C Programming
    Replies: 1
    Last Post: 08-10-2013, 09:24 AM
  3. Project Euler problem
    By nimitzhunter in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2012, 09:00 PM
  4. Project Euler Problem 8
    By deadrabbit in forum C Programming
    Replies: 2
    Last Post: 10-06-2011, 10:56 PM
  5. Project Euler problem 3
    By SELFMADE in forum C Programming
    Replies: 7
    Last Post: 09-16-2009, 03:33 AM

Tags for this Thread