Do not want to use memset function

Discussion in 'C++' started by Immortal Nephi, May 16, 2012.

  1. I wrote my own Fill_Memory function because I want to add SSE2 code
    into it, but memset function does not have SSE2 code. Everything in
    my function works very well.
    I turned on the optimization of C++ Compiler. It mistakenly
    automatically invoked memset function (or memset function is inserted
    itself without my consult) as long as the bytes are between two and
    four after Fill_Memory function is called and processed more than 128
    bytes leaving two more bytes to be processed with memset function.
    How can I tell C++ Compiler to disable inserting memset function and
    allow Fill_Memory function to do the job itself?
     
    Immortal Nephi, May 16, 2012
    #1
    1. Advertising

  2. On 16.05.2012 06:49, Immortal Nephi wrote:
    > I wrote my own Fill_Memory function because I want to add SSE2 code
    > into it, but memset function does not have SSE2 code.


    This is /your/ statement.
    I have already seen SSE instructions in trivial operations like memcpy
    or even assignment of large objects.

    > How can I tell C++ Compiler to disable inserting memset function and
    > allow Fill_Memory function to do the job itself?


    You can't do that in a standard conform way because it requires changes
    to the runtime environment. Of course, you might provide your own memset
    implementation and force the linker to prefer /your/ implementation by
    using the same symbol. But replacing only parts of the runtime is
    undefined behavior in general, although it works from time to time.


    Marcel
     
    Marcel Müller, May 16, 2012
    #2
    1. Advertising

  3. Immortal Nephi

    gwowen Guest

    On May 16, 7:54 am, Marcel Müller <>
    wrote:
    > >    How can I tell C++ Compiler to disable inserting memset functionand
    > > allow Fill_Memory function to do the job itself?

    >
    > You can't do that in a standard conform way because it requires changes
    > to the runtime environment.


    True, but I think what is happening here is that the compiler is
    optimizing something to memset() that is not an explicit memset()
    call. For example gcc may optimize:

    my_memclear(char *ptr,size_t num)
    {
    for(size_t i=0; i<num; ++i) ptr = 0;
    }

    to

    my_memclear(char *ptr,size_t num)
    {
    __builtin_memset(ptr,0,100);
    }

    As you say, though, there's everychance that __builtin_memset() *does*
    use SSE when the architecture flags allow it.
     
    gwowen, May 16, 2012
    #3
  4. Immortal Nephi

    Jorgen Grahn Guest

    On Wed, 2012-05-16, Marcel Müller wrote:
    > On 16.05.2012 06:49, Immortal Nephi wrote:
    >> I wrote my own Fill_Memory function because I want to add SSE2 code
    >> into it, but memset function does not have SSE2 code.

    >
    > This is /your/ statement.
    > I have already seen SSE instructions in trivial operations like memcpy
    > or even assignment of large objects.


    Seems reasonable. It's important to realize that the compiler may not
    be genererating code specifically for the CPU it itself is running on.
    E.g. gcc on x86 will by default generate code which runs on the
    ancient 80386 and upwards. You have to tell it somehow that it's OK
    to use SSE2 instructions.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 16, 2012
    #4
  5. On May 16, 1:54 am, Marcel Müller <>
    wrote:
    > On 16.05.2012 06:49, Immortal Nephi wrote:
    >
    > >    I wrote my own Fill_Memory function because I want to add SSE2 code
    > > into it, but memset function does not have SSE2 code.

    >
    > This is /your/ statement.
    > I have already seen SSE instructions in trivial operations like memcpy
    > or even assignment of large objects.
    >
    > >    How can I tell C++ Compiler to disable inserting memset functionand
    > > allow Fill_Memory function to do the job itself?

    >
    > You can't do that in a standard conform way because it requires changes
    > to the runtime environment. Of course, you might provide your own memset
    > implementation and force the linker to prefer /your/ implementation by
    > using the same symbol. But replacing only parts of the runtime is
    > undefined behavior in general, although it works from time to time.


    I use MSVC++ 2010. I looked at memset's assembly code. It does not
    have any SSE2 code, but memcpy does have SSE2 code.

    Why should memset be called unless number of bytes are less than four?

    the tail loop looks like below.

    while( count > 0 )
    {
    char*& dst_8 = reinterpret_cast< char*& >( dst );

    *dst_8 = value;
    ++dst_8;
    --count;
    }

    This code will be replaced with memset no matter if count is less than
    4.

    If you add something differently, memset will not be inserted
    automatically.

    if( count > 0 )
    {
    do
    {
    char*& dst_8 = reinterpret_cast< char*& >( dst );

    *dst_8 = value;
    ++dst_8;
    --count;
    }
    while( count > 0 );
    }
     
    Nephi Immortal, May 16, 2012
    #5
  6. Immortal Nephi

    Adam Skutt Guest

    On May 16, 10:27 am, Nephi Immortal <> wrote:
    >
    > I use MSVC++ 2010.  I looked at memset's assembly code.  It does not
    > have any SSE2 code, but memcpy does have SSE2 code.


    I'm a little confused as to how you have looked at that code. Anyway,
    bugs like this: http://connect.microsoft.com/Visual...0304/memset-causes-corruption-using-arch-sse2
    suggest that you are incorrect and that the compiler will happily
    generate the instructions if you ask it to do so.

    My guess is you have not correctly asked it to do so, as it is not the
    default behavior. Regardless, the solution to this problem and your
    original problem are best asked of the compiler manual and not here.

    Adam
     
    Adam Skutt, May 16, 2012
    #6
    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. k-man
    Replies:
    4
    Views:
    4,564
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    8,987
    tom_usenet
    Aug 24, 2004
  3. Replies:
    17
    Views:
    6,615
    Greg Comeau
    Sep 22, 2004
  4. memset o/p not correct

    , Apr 19, 2008, in forum: C Programming
    Replies:
    39
    Views:
    867
    Keith Thompson
    Apr 22, 2008
  5. Replies:
    21
    Views:
    576
    Shao Miller
    Feb 17, 2013
Loading...

Share This Page