Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:10:21+00:00 2026-05-23T04:10:21+00:00

I have written a program to process some data written to disk in big-endian

  • 0

I have written a program to process some data written to disk in big-endian format, so the program needs to swap bytes in order to do anything else. After profiling the code I found that my byte swapping function was taking 30% of the execution time. So I thought to myself, how can I speed this up?
So I decided to write a little piece inline assembly.

I would up replacing this:

void swapTwoByte(char* array, int numChunks)
{
    for(int i= (2*numChunks-1); i>=0; i-=2)
    {
        char temp=array[i];
        array[i]=array[i-1];
        array[i-1]=temp;
    }
}

with this:

void swapTwoByte(int16* array, int numChunks)
{
    for(int i= (numChunks-1); i>=0; --i)
    {
        asm("movw %1, %%ax;"
            "rorw %%ax;"
            "rorw %%ax;"
            "rorw %%ax;"
            "rorw %%ax;"
            "rorw %%ax;"
            "rorw %%ax;"
            "rorw %%ax;"
            "rorw %%ax;"
            "movw %%ax, %0;"
            : "=r" ( array[i] )
            : "r" (array[i])
            :"%ax"
        );
    }
}

Which does the intended job, but that is a lot of rotate operations.

So here is my question:
According to this source rorw can take two operands, and in the gas sytax the source operand should be the number of bits to rotate by, but every time I try to replace that list of 8 rotate rights with something like

".set rotate, 0x0008"
"rorw rotate, %%ax"

I get an assembler error stating:

"Error: number of operands mismatch for `ror'"

Why is this? What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:10:22+00:00Added an answer on May 23, 2026 at 4:10 am

    First of all, use

    #include <arpa/inet.h>
    little_endian = ntohs(big_endian);
    

    This will compile into optimal code on whatever system you are using, and it will even work if you happen to port your code to a big-endian platform.

    However, this will not fix your performance problem because I believe you have misidentified the problem. Nemo’s first rule of micro-optimization: “Math is fast; memory is slow”.

    Iterating through a large block of memory and swapping its bytes is extremely cache-unfriendly. A byte swap is one cycle; a memory read or write is hundreds of cycles unless it hits in the cache.

    So do not swap the bytes until you use them. My personal favorite approach is this:

    class be_uint16_t {
    public:
            be_uint16_t() : be_val_(0) {
            }
            be_uint16_t(const uint16_t &val) : be_val_(htons(val)) {
            }
            operator uint16_t() const {
                    return ntohs(be_val_);
            }
    private:
            uint16_t be_val_;
    } __attribute__((packed));
    

    This defines a two-byte class that represents a big-endian number in memory. It implicitly casts to and from uint16_t as needed. So cast your memory pointer to a be_uint16 * and just access it like an array; forget about the byte swapping because the class will do it for you:

    const be_uint16_t *p = (be_uint16 *)my_block;
    unsigned val = p[37];  // or whatever
    

    Note that you can even do things like this:

    be_uint16_t x = 12;
    x = x + 1;
    write(fd, &x, sizeof(x)); // writes 13 to file in big-endian form
    

    The overhead of swapping a value immediately before use is, in my experience, undetectable. Locality is the name of the game…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data acquisition program written in C++ (Visual Studio 6.0). Some clients
I have written a simple program that use some classes and procol buffers. These
I have written a program in C to parse large XML files and then
I have written a program that will etablish a network connection with a remote
I have written a program with both a Advanced Mode and a Beginner Mode
As a Christmas gift I have written a small program in Java to calculate
I'm using Visual Studio 2005. I have a program written in C#. When I
I have a console program written in C# that I am using to send
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have a fairly large program written in C. It spans several files, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.