Thread: C Programming CPU clock cycled Help

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    16

    C Programming CPU clock cycled Help

    I need some help how to find out cpu clock cycles.

    The value ex can be computed using the series approximation:
    ex = 1 + x + x2/2! + x3/3! + x4/4! + x5/5! + …


    what I need is

    code to display the number of CPU clock cycles required to execute exp32 versus that required to execute exp64. To get accurate cycle counts, you must temporarily disable interrupts during the execution of exp32 and exp64 using libc functions disable and enable (e.g., disable, exp32, enable). What is the
    percentage increase in time to compute exp64 relative to exp32?

    Here is the code for the program...

    [I]#include <dos.h>
    #include "libepc.h"

    char * Fixed32ToString(FIXED32) ;
    char * Fixed64ToString(FIXED64) ;

    FIXED32 exp32(FIXED32) ;
    FIXED64 exp64(FIXED64) ;

    int main(int argc, char *argv[])
    {
    FIXED32 e32 ;
    FIXED64 e64 ;

    enable() ;
    ClearScreen(0x07) ;
    SetCursorVisible(FALSE) ;

    e32 = exp32(0x00010000L) ;
    SetCursorPosition(10, 30) ;
    PutString("16.16 Fixed Pt: e = ") ;
    PutString(Fixed32ToString(e32)) ;
    SetCursorPosition(11, 30) ;
    PutString("Correct Result: e = 0002.B7E1") ;

    e64 = exp64(0x0000000100000000L) ;
    SetCursorPosition(13, 30) ;
    PutString("32.32 Fixed Pt: e = ") ;
    PutString(Fixed64ToString(e64)) ;
    SetCursorPosition(14, 30) ;
    PutString("Correct Result: e = 00000002.B7E15162\n") ;

    return 0 ;
    }

    char *Fixed32ToString(FIXED32 f)
    {
    WORD16 whole_part, fract_part ;
    static char result[10] ;

    fract_part = ((WORD16 *) &f)[0] ;
    whole_part = ((WORD16 *) &f)[1] ;

    FormatUnsigned(&result[0], whole_part, 16, 4, '0') ;
    result[4] = '.' ;
    FormatUnsigned(&result[5], fract_part, 16, 4, '0') ;

    return result ;
    }

    char *Fixed64ToString(FIXED64 f)
    {
    DWORD32 whole_part, fract_part ;
    static char result[18] ;

    fract_part = ((DWORD32 *) &f)[0] ;
    whole_part = ((DWORD32 *) &f)[1] ;

    FormatUnsigned(&result[0], whole_part, 16, 8, '0') ;
    result[8] = '.' ;
    FormatUnsigned(&result[9], fract_part, 16, 8, '0') ;

    return result ;
    }

    FIXED32 exp32(FIXED32 x)
    {
    static FIXED32 btm[] =
    {
    1 << 16, 2 << 16, 6 << 16, 24 << 16,
    120 << 16, 720 << 16, 5040 << 16, 40320 << 16
    } ;
    static FIXED32 one_point_zero = 1 << 16 ;
    FIXED32 result, top ;
    int i ;

    top = result = one_point_zero ;
    for (i = 0; i < 8; i++)
    {
    top = Product32(top, x) ;
    result += Quotient32(top, btm) ;
    }

    return result ;
    }

    FIXED64 exp64(FIXED64 x)
    {

    /* Insert your code here */

    return 0 ;
    }




    ANd here is the libepc.h file

    /* ================================================== ========== */
    /* File: LIBEPC.H */
    /* */
    /* Copyright (C) 2001, Daniel W. Lewis and Prentice-Hall */
    /* */
    /* Purpose: Various #defines, structures, and function */
    /* prototypes needed to use the corresponding library LIBEPC.A. */
    /* */
    /* Designed for use with the DJGPP port of the GNU C/C++ */
    /* protected mode 386 compiler. */
    /* */
    /* Modification History: */
    /* */
    /* ================================================== ========== */

    #ifndef _LIBEPC_H_ /* Avoid multiple inclusions */
    #define _LIBEPC_H_

    /* ------------------------------------------------------------ */
    /* A few datatypes to make the operand size more obvious. */
    /* ------------------------------------------------------------ */
    typedef int BOOL ;
    typedef unsigned char BYTE8 ;
    typedef unsigned short int WORD16 ;
    typedef unsigned long int DWORD32 ;
    typedef unsigned long long int QWORD64 ;
    typedef signed long int FIXED32 ; /* 16.16 Fixed-Point */
    typedef signed long long int FIXED64 ; /* 32.32 Fixed-Point */
    typedef void (*ISR)(void) ; /* Pointer to an ISR */

    /* ------------------------------------------------------------ */
    /* Constants for use with datatype BOOL (above). */
    /* ------------------------------------------------------------ */
    #ifndef TRUE
    #define TRUE 1
    #endif
    #ifndef FALSE
    #define FALSE 0
    #endif

    /* ------------------------------------------------------------ */
    /* Macros to extract the LSByte and MSByte of a WORD16 value */
    /* ------------------------------------------------------------ */
    #define LSB(u) ((u) & 0xFF)
    #define MSB(u) ((u) >> 8)

    /* ------------------------------------------------------------ */
    /* Returns number of elements in an array. (Use in for loops.) */
    /* ------------------------------------------------------------ */
    #define ENTRIES(a) (sizeof(a)/sizeof(a[0]))

    /* ------------------------------------------------------------ */
    /* Declaration prefix to hide an object from the linker. */
    /* ------------------------------------------------------------ */
    #define PRIVATE static

    /* ------------------------------------------------------------ */
    /* Define a NULL pointer. */
    /* ------------------------------------------------------------ */
    #ifndef NULL
    #define NULL ((void *) 0)
    #endif

    /* ------------------------------------------------------------ */
    /* 386 instructions needed when writing ISR's. Note that IRET */
    /* pops the pointer to the stack frame that was established by */
    /* code that the compiler generates at every function entry. */
    /* ------------------------------------------------------------ */
    #define PUSHCS __asm__ __volatile__ ("PUSHL %CS") ;
    #define PUSHF __asm__ __volatile__ ("PUSHFL")
    #define POPF __asm__ __volatile__ ("POPFL")
    #define STI __asm__ __volatile__ ("STI")
    #define CLI __asm__ __volatile__ ("CLI")
    #define PUSHA __asm__ __volatile__ ("PUSHAL")
    #define POPA __asm__ __volatile__ ("POPAL")
    #define ENTER __asm__ __volatile__ ("ENTER $0,$0")
    #define LEAVE __asm__ __volatile__ ("LEAVE")
    #define IRET __asm__ __volatile__ ("IRET")

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in IO.ASM */
    /* ------------------------------------------------------------ */
    void outportb(WORD16, BYTE8) ;
    BYTE8 inportb(WORD16) ;
    void exit(int) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in INIT-CRT.C */
    /* ------------------------------------------------------------ */
    void * LastMemoryAddress(void) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in INIT-IDT.C */
    /* ------------------------------------------------------------ */
    #define IRQ_TICK 0
    #define IRQ_KYBD 1
    #define IRQ_COM2_COM4 3
    #define IRQ_COM1_COM3 4
    #define IRQ_FLOPPY 6
    #define IRQ_PAR_PORT 7
    #define IRQ_RTC 8
    #define IRQ_PS2_MOUSE 12
    #define IRQ_HARD_DISK 14

    int IRQ2INT(int irq) ;
    ISR GetISR(int int_numb) ;
    void SetISR(int int_numb, ISR isr) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in KEYBOARD.C */
    /* ------------------------------------------------------------ */
    BYTE8 GetScanCode(void) ;
    BOOL ScanCodeRdy(void) ;
    BOOL SetsKybdState(BYTE8) ;
    WORD16 ScanCode2Ascii(BYTE8 code) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in SPEAKER.C */
    /* ------------------------------------------------------------ */
    void Sound(int hertz) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in CYCLES.ASM */
    /* ------------------------------------------------------------ */
    QWORD64 CPU_Clock_Cycles(void) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in TIMER.C */
    /* ------------------------------------------------------------ */
    DWORD32 Milliseconds(void) ;
    DWORD32 Now_Plus(int seconds) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in DISPLAY.C */
    /* ------------------------------------------------------------ */
    WORD16 * Cell(int row, int col) ;
    void ClearScreen(BYTE8 attb) ;
    char * FormatUnsigned
    (char *bfr, unsigned val, int base, int width, char fill) ;
    int GetCursorCol(void) ;
    int GetCursorRow(void) ;
    void PutAttb(BYTE8 attb, int cells) ;
    void PutChar(char ch) ;
    void PutCharAt(char ch, int row, int col) ;
    void PutString(char *string) ;
    void PutUnsigned(unsigned val, int base, int width) ;
    void SetCursorPosition(int row, int col) ;
    void SetCursorVisible(BOOL visible) ;
    char * Unsigned2Ascii(char *bfr, unsigned val, int base) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in WINDOW.C */
    /* ------------------------------------------------------------ */
    typedef struct ROWCOL
    {
    int first ;
    int last ;
    int cursor ;
    } ROWCOL ;

    typedef struct WINDOW
    {
    ROWCOL row ;
    ROWCOL col ;
    char title[1] ;
    } WINDOW ;

    WINDOW * WindowCreate(char *title, int row_first, int row_last,
    int col_first, int col_last) ;
    void WindowErase(WINDOW *w) ;
    void WindowPutChar(WINDOW *w, char ch) ;
    void WindowPutString(WINDOW *w, char *str) ;
    void WindowSelect(WINDOW *w) ;
    void WindowSetCursor(WINDOW *w, int row, int col) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in HEAP.C */
    /* ------------------------------------------------------------ */
    void * malloc(long unsigned int) ;
    void free(void *) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in QUEUE.C */
    /* ------------------------------------------------------------ */
    typedef struct QUEUE
    {
    int item_size ;
    int max_items ;
    int item_count ;
    int nq_index ;
    int dq_index ;
    char bfr[0] ;
    } QUEUE ;

    QUEUE * QueueCreate(int numb_items, int item_size) ;
    BOOL QueueInsert(QUEUE *q, void *data) ;
    BOOL QueueRemove(QUEUE *q, void *data) ;

    /* ------------------------------------------------------------ */
    /* Support for functions implemented in FIXEDPT.ASM */
    /* ------------------------------------------------------------ */
    #define FIXED32_ONE 65536L
    #define FIXED32_PI 205887L
    #define FIXED32_2PI 411775L
    #define FIXED32_E 178144L
    #define FIXED32_ROOT2 74804L
    #define FIXED32_ROOT3 113512L
    #define FIXED32_GOLDEN 106039L

    #define FLOAT32(x) ((FIXED32) ((x) << 16))
    #define TRUNC32(x) ((int) ((x) >> 16))
    #define ROUND32(x) ((int) (((x) + 0x8000) >> 16))

    FIXED32 Product32(FIXED32 multiplier, FIXED32 multiplicand) ;
    FIXED32 Quotient32(FIXED32 dividend, FIXED32 divisor) ;
    FIXED32 Inverse32(FIXED32 n) ;
    FIXED32 Sqrt32(FIXED32 n) ;
    FIXED64 Product64(FIXED64 multiplier, FIXED64 multiplicand) ;

    #endif

    Any help will be very much appreciated, thanx in advance

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The interrupt part is easy, but the rest is pure speculation. You have a function called CPU_Clock_Cycles(), but what does it do? Do you have any documentation for it? Here's a guess.
    Code:
    int main(int argc, char *argv[])
    {
    FIXED32 e32 ;
    FIXED64 e64 ;
    QWORD64 start, finish;
    
    enable() ;
    ClearScreen(0x07) ;
    SetCursorVisible(FALSE) ;
    
    disable() ;
    start = CPU_Clock_Cycles();
    e32 = exp32(0x00010000L) ;
    finish = CPU_Clock_Cycles();
    enable() ;
    
    SetCursorPosition(10, 30) ;
    PutString("16.16 Fixed Pt: e = ") ;
    PutString(Fixed32ToString(e32)) ;
    SetCursorPosition(11, 30) ;
    PutString("Correct Result: e = 0002.B7E1") ;
    
    PutString("Clock cycles elapsed: ");
    PutString(Fixed64ToString(finish-start)) ;
    
    disable() ;
    start = CPU_Clock_Cycles();
    e64 = exp64(0x0000000100000000L) ;
    finish = CPU_Clock_Cycles();
    enable() ;
    
    SetCursorPosition(13, 30) ;
    PutString("32.32 Fixed Pt: e = ") ;
    PutString(Fixed64ToString(e64)) ;
    SetCursorPosition(14, 30) ;
    PutString("Correct Result: e = 00000002.B7E15162\n") ;
    
    PutString("Clock cycles elapsed: ");
    PutString(Fixed64ToString(finish-start)) ;
    
    return 0 ;
    }
    If you have an example of how to use CPU_Clock_Cycles(), this would be helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 05:41 PM
  4. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  5. CPU times and clock ticks
    By ActionMan in forum C Programming
    Replies: 3
    Last Post: 04-28-2002, 08:32 PM