operator overloading

Discussion in 'C++' started by hamze, May 18, 2011.

  1. hamze

    hamze Guest

    hi,
    I have added a special hardware to my cpu and now I want to overload *
    for double type.
    usually [ double operator*(double, double) ] does not work because
    double is a predefined system type.
    changing all of code manually is not a good way for me so I want to
    find an automatic way.
    first solution I tried was to define a class with one double variable
    inside. then overload * for that class, but it is not good because it
    has a lot of timing overhead , moreover I should carefully change all
    of my code to replace double with new type.

    is there any way to overload * operator for type of Double? can macros
    help me ?
    hamze, May 18, 2011
    #1
    1. Advertising

  2. hamze

    Jorgen Grahn Guest

    On Wed, 2011-05-18, hamze wrote:
    > hi,
    > I have added a special hardware to my cpu and now I want to overload *
    > for double type.


    It's not "overloading" if the operator already exists. "Replacement",
    rather.

    What is this special hardware, if it's not the normal floating-point
    processor which your compiler can automatically make use of?

    > usually [ double operator*(double, double) ] does not work because
    > double is a predefined system type.
    > changing all of code manually is not a good way for me so I want to
    > find an automatic way.
    > first solution I tried was to define a class with one double variable
    > inside. then overload * for that class, but it is not good because it
    > has a lot of timing overhead ,


    If "timing overhead" means it runs slower than whatever alternative
    you have: are you sure this is the case, with a decent compiler with
    optimization turned on? My assumption (I haven't investigated deeply)
    is that you can easily get a struct Foo { double val; } to generate
    code that's just as fast as a naked double.

    > moreover I should carefully change all
    > of my code to replace double with new type.
    >
    > is there any way to overload * operator for type of Double? can macros
    > help me ?


    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, May 18, 2011
    #2
    1. Advertising

  3. On 5/18/2011 4:59 AM, hamze wrote:
    > I have added a special hardware to my cpu and now I want to overload *
    > for double type.


    Tough luck.

    > usually [ double operator*(double, double) ] does not work because
    > double is a predefined system type.


    Yes.

    > changing all of code manually is not a good way for me so I want to
    > find an automatic way.


    I feel your pain. Writing programs manually is not a good way either.
    If I could just find an automatic way to transmit my thoughts into the
    computer...

    > first solution I tried was to define a class with one double variable
    > inside. then overload * for that class, but it is not good because it
    > has a lot of timing overhead ,


    From what? How do you know there is the "timing overhead"?

    > moreover I should carefully change all
    > of my code to replace double with new type.


    Yes, that's the price you [usually] have to pay to use special hardware.

    > is there any way to overload * operator for type of Double? can macros
    > help me ?


    Not unless you write your own custom compiler.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 18, 2011
    #3
  4. hamze

    hamze Guest

    On May 18, 4:15 pm, Jorgen Grahn <> wrote:
    > On Wed, 2011-05-18, hamze wrote:
    > > hi,
    > > I have added a special hardware to my cpu and now I want to overload *
    > > for double type.

    >
    > It's not "overloading" if the operator already exists. "Replacement",
    > rather.
    >
    > What is this special hardware, if it's not the normal floating-point
    > processor which your compiler can automatically make use of?


    it does not matter but if you know it is a softprocessor on FPGA, it
    does not have hardware for double precision (64bit )
    so I add a custom hardware on bus of system, it gives me a lot of
    speed compare to C++ library implementation of double precision
    operations,
    but if I dont find a good way to redirect using of my code ( which
    works with that hardware) instead of C++ library code ,for double
    operations , its timing overhead will compensate its speed!
    difficulty of changing operations in all of my code to a new function
    call is impossilbe because code is too big.


    >
    > > usually [ double operator*(double, double) ] does not work because
    > > double is a predefined system type.
    > > changing all of code manually is not a good way for me so I want to
    > > find an automatic way.
    > > first solution I tried was to define a class with one double variable
    > > inside. then overload * for that class, but it is not good because it
    > > has a lot of timing overhead ,

    >
    > If "timing overhead" means it runs slower than whatever alternative
    > you have: are you sure this is the case, with a decent compiler with
    > optimization turned on?  My assumption (I haven't investigated deeply)
    > is that you can easily get a struct Foo { double val; } to generate
    > code that's just as fast as a naked double.
    >

    I am sure about my timing measurement, optimization is off to exact
    measurement and a hardware timer is used to measure code runtime.....
    "timing overhead" means time elapsed to use my hardware, for example
    writing to that new hardware and reading result or waiting for
    completion of computation,
    I did what you say, but I used class instead of struct, ( i will try
    struct ) but after deeply measurement, I saw referring to a double
    type is faster than a class t Foo { double val; },

    is there anyway to replace library of C++ with my code? I want to
    replace c++ implementation of double*double with my code, then every
    thin will solve!!
    thanks



    > > moreover I should carefully change all
    > > of my code to replace double with new type.

    >
    > > is there any way to overload * operator for type of Double? can macros
    > > help me ?

    >
    > /Jorgen
    >
    > --
    >   // Jorgen Grahn <grahn@  Oo  o.   .  .
    > \X/     snipabacken.se>   O  o   .
    hamze, May 19, 2011
    #4
  5. hamze

    hamze Guest

    On May 18, 5:09 pm, Victor Bazarov <> wrote:
    > On 5/18/2011 4:59 AM, hamze wrote:
    >
    > > I have added a special hardware to my cpu and now I want to overload *
    > > for double type.

    >
    > Tough luck.
    >
    > > usually [ double operator*(double, double) ] does not work because
    > > double is a predefined system type.

    >
    > Yes.
    >
    > > changing all of code manually is not a good way for me so I want to
    > > find an automatic way.

    >
    > I feel your pain.  Writing programs manually is not a good way either.
    > If I could just find an automatic way to transmit my thoughts into the
    > computer...
    >
    > > first solution I tried was to define a class with one double variable
    > > inside. then overload * for that class, but it is not good because it
    > > has a lot of timing overhead ,

    >
    >  From what?  How do you know there is the "timing overhead"?
    >
    >  > moreover I should carefully change all
    >
    > > of my code to replace double with new type.

    >
    > Yes, that's the price you [usually] have to pay to use special hardware.
    >
    > > is there any way to overload * operator for type of Double? can macros
    > > help me ?

    >
    > Not unless you write your own custom compiler.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask



    NO!!! please answer!! please!!!
    hamze, May 19, 2011
    #5
  6. hamze

    Jorgen Grahn Guest

    On Thu, 2011-05-19, hamze wrote:
    > On May 18, 4:15 pm, Jorgen Grahn <> wrote:
    >> On Wed, 2011-05-18, hamze wrote:
    >> > hi,
    >> > I have added a special hardware to my cpu and now I want to overload *
    >> > for double type.

    >>
    >> It's not "overloading" if the operator already exists. "Replacement",
    >> rather.
    >>
    >> What is this special hardware, if it's not the normal floating-point
    >> processor which your compiler can automatically make use of?

    >
    > it does not matter but if you know it is a softprocessor on FPGA, it
    > does not have hardware for double precision (64bit )
    > so I add a custom hardware on bus of system, it gives me a lot of
    > speed compare to C++ library implementation of double precision
    > operations,
    > but if I dont find a good way to redirect using of my code ( which
    > works with that hardware) instead of C++ library code ,for double
    > operations , its timing overhead will compensate its speed!
    > difficulty of changing operations in all of my code to a new function
    > call is impossilbe because code is too big.


    Ah, if the alternative is your compiler's non-FPU library functions,
    then you might be able to provide your own implementation of the
    floating-point routines, replacing the ones in the C library.

    >> > usually [ double operator*(double, double) ] does not work because
    >> > double is a predefined system type.
    >> > changing all of code manually is not a good way for me so I want to
    >> > find an automatic way.
    >> > first solution I tried was to define a class with one double variable
    >> > inside. then overload * for that class, but it is not good because it
    >> > has a lot of timing overhead ,

    >>
    >> If "timing overhead" means it runs slower than whatever alternative
    >> you have: are you sure this is the case, with a decent compiler with
    >> optimization turned on?  My assumption (I haven't investigated deeply)
    >> is that you can easily get a struct Foo { double val; } to generate
    >> code that's just as fast as a naked double.
    >>

    > I am sure about my timing measurement, optimization is off to exact
    > measurement


    Then it's not surprising that your code is slower! It's the optimizer
    and nothing but the optimizer which removes the overhead (e.g. by
    inlining function calls).

    > and a hardware timer is used to measure code runtime.....
    > "timing overhead" means time elapsed to use my hardware, for example
    > writing to that new hardware and reading result or waiting for
    > completion of computation,
    > I did what you say, but I used class instead of struct, ( i will try
    > struct )


    Struct and class is the same thing, for these purposes.

    but after deeply measurement, I saw referring to a double
    > type is faster than a class t Foo { double val; },
    >
    > is there anyway to replace library of C++ with my code? I want to
    > replace c++ implementation of double*double with my code, then every
    > thin will solve!!


    Impossible to say without knowing which compiler you use. I don't even
    know how gcc implements soft-float ... I only know the ancient Lattice
    C compiler for the Commodore-Amiga had library routines with funny
    names for operating on floats and doubles. (You could even choose
    between IEEE versions and some Commodore- or Motorola-specific
    representation.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, May 19, 2011
    #6
  7. hamze

    hamze Guest

    On May 19, 6:01 pm, Jorgen Grahn <> wrote:
    > On Thu, 2011-05-19, hamze wrote:
    > > On May 18, 4:15 pm, Jorgen Grahn <> wrote:
    > >> On Wed, 2011-05-18, hamze wrote:
    > >> > hi,
    > >> > I have added a special hardware to my cpu and now I want to overload*
    > >> > for double type.

    >
    > >> It's not "overloading" if the operator already exists. "Replacement",
    > >> rather.

    >
    > >> What is this special hardware, if it's not the normal floating-point
    > >> processor which your compiler can automatically make use of?

    >
    > > it does not matter but if you know it is a softprocessor on FPGA, it
    > > does not have hardware for double precision (64bit )
    > > so I add a custom hardware on bus of system, it gives me a lot of
    > > speed compare to C++ library implementation of double precision
    > > operations,
    > > but if I dont find a good way to redirect using of my code ( which
    > > works with that hardware) instead of C++ library code ,for double
    > > operations , its timing overhead will compensate its speed!
    > > difficulty of changing operations in all of my code to a new function
    > > call is impossilbe because code is too big.

    >
    > Ah, if the alternative is your compiler's non-FPU library functions,
    > then you might be able to provide your own implementation of the
    > floating-point routines, replacing the ones in the C library.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > >> > usually [ double operator*(double, double) ] does not work because
    > >> > double is a predefined system type.
    > >> > changing all of code manually is not a good way for me so I want to
    > >> > find an automatic way.
    > >> > first solution I tried was to define a class with one double variable
    > >> > inside. then overload * for that class, but it is not good because it
    > >> > has a lot of timing overhead ,

    >
    > >> If "timing overhead" means it runs slower than whatever alternative
    > >> you have: are you sure this is the case, with a decent compiler with
    > >> optimization turned on?  My assumption (I haven't investigated deeply)
    > >> is that you can easily get a struct Foo { double val; } to generate
    > >> code that's just as fast as a naked double.

    >
    > > I am sure about my timing measurement, optimization is off to exact
    > > measurement

    >
    > Then it's not surprising that your code is slower! It's the optimizer
    > and nothing but the optimizer which removes the overhead (e.g. by
    > inlining function calls).
    >
    > > and a hardware timer is used to measure code runtime.....
    > > "timing overhead" means time elapsed to use my hardware, for example
    > > writing to that new hardware and reading result or waiting for
    > > completion of computation,
    > > I did what you say, but I used class instead of struct, ( i will try
    > > struct )

    >
    > Struct and class is the same thing, for these purposes.
    >
    > but after deeply measurement, I saw referring to a double
    >
    > > type is faster than a class t Foo { double val; },

    >
    > > is there anyway to replace library of C++ with my code? I want to
    > > replace c++ implementation of double*double with my code, then every
    > > thin will solve!!

    >
    > Impossible to say without knowing which compiler you use. I don't even
    > know how gcc implements soft-float ... I only know the ancient Lattice
    > C compiler for the Commodore-Amiga had library routines with funny
    > names for operating on floats and doubles. (You could even choose
    > between IEEE versions and some Commodore- or Motorola-specific
    > representation.)
    >
    > /Jorgen
    >
    > --
    >   // Jorgen Grahn <grahn@  Oo  o.   .  .
    > \X/     snipabacken.se>   O  o   .




    thanks,
    hamze, May 22, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    417
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    727
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    725
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    557
  5. Replies:
    2
    Views:
    306
Loading...

Share This Page