Array question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sanjay

    Array question

    Hello i have this code,

    void f () {
    static const int array[2][4] = {
    {0,1,2,3},
    {4,5,6,7}};
    int a_bit;

    asm(
    "movl [array_ptr + 2*column + row], %%eax \n"
    "movl %%eax, %0 \n"
    : "=g" (a_bit)
    : "g" (array[0][0])
    : "ax", "memory"
    )

    }

    but this wont work. What I want that is fast f(), thats why `static' is
    (is it ok?), and the f() shoud do:
    `a_bit = array[column][row];'
    but I don't know what should I write in place of:
    `[array_ptr + 2*column + row]'.

    Can u advise thanx.
  • Keith Thompson

    #2
    Re: Array question

    sanjay <nospam@nospam. comwrites:
    Hello i have this code,
    >
    void f () {
    static const int array[2][4] = {
    {0,1,2,3},
    {4,5,6,7}};
    int a_bit;
    >
    asm(
    "movl [array_ptr + 2*column + row], %%eax \n"
    "movl %%eax, %0 \n"
    : "=g" (a_bit)
    : "g" (array[0][0])
    : "ax", "memory"
    )
    >
    }
    >
    but this wont work. What I want that is fast f(), thats why `static'
    is (is it ok?), and the f() shoud do:
    `a_bit = array[column][row];'
    but I don't know what should I write in place of:
    `[array_ptr + 2*column + row]'.
    If you want f() to do
    a_bit = array[column][row];
    why not just write
    a_bit = array[column][row];
    ? For such a simple operation, the code generated by the compiler is
    likely to be as fast as anything you can write manually in assembly
    language (and more likely to be correct).

    If you want to know what the assembly language for the assignment
    should look like, you can always write it in straight C and ask the
    compiler to generate an assembly listing. ("-S" is a common option
    for this.)

    But in most cases, there's no good reason you should care. If the
    code works and is not *measurably* too slow, just don't worry about
    it.

    Having said all that, if you still insist on using inline assembly,
    you'll need to ask elsewhere. Standard C doesn't provide this
    feature. If you're using gcc, try gnu.gcc.help; if you're using
    something else, you'll need to find a newsgroup that covers your
    compiler or system.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • Eric Sosman

      #3
      Re: Array question

      sanjay wrote:
      Hello i have this code,
      >
      void f () {
      static const int array[2][4] = {
      {0,1,2,3},
      {4,5,6,7}};
      int a_bit;
      >
      asm(
      "movl [array_ptr + 2*column + row], %%eax \n"
      "movl %%eax, %0 \n"
      : "=g" (a_bit)
      : "g" (array[0][0])
      : "ax", "memory"
      )
      >
      }
      >
      but this wont work. What I want that is fast f(), thats why `static' is
      (is it ok?), and the f() shoud do:
      `a_bit = array[column][row];'
      but I don't know what should I write in place of:
      `[array_ptr + 2*column + row]'.
      >
      Can u advise thanx.
      Write

      a_bit = array[column][row];

      .... and remember to define `column' and `row' somewhere.

      --
      Eric.Sosman@sun .com

      Comment

      • Antoninus Twink

        #4
        Re: Array question

        On 21 May 2008 at 21:43, sanjay wrote:
        What I want that is fast f(), thats why `static' is (is it ok?), and
        the f() shoud do:
        `a_bit = array[column][row];'
        but I don't know what should I write in place of:
        `[array_ptr + 2*column + row]'.
        Why don't you just do
        asm ("movl %1,%0" : "=r" (a_bit) : "m" (array[column][row]));

        But really, do you honestly think your compiler will do a bad job of
        translating this assignment statement??

        You also seem to have the row and column indexes backwards compared to
        most other people in the world...

        Comment

        Working...