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

  • SEARCH
  • Home
  • 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 7595999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:48:44+00:00 2026-05-30T21:48:44+00:00

The code for x86 does this (n can only be 1 through 4, unknown

  • 0

The code for x86 does this (n can only be 1 through 4, unknown at compile time):

static const uint32_t wordmask[] = {
  0u, 0xffu, 0xffffu, 0xffffffu, 0xffffffffu
};
static inline uint32_t get_unaligned_le_x86(const void *p, uint32_t n) {
  uint32_t ret = *(const uint32_t *)p & wordmask[n];
  return ret;
}

For architectures that don’t have unaligned 32bit little endian loads I have two variants:

static uint32_t get_unaligned_le_v1(const void *p, uint32_t n) {
  const uint8_t *b = (const uint8_t *)p;
  uint32_t ret;
  ret = b[0];
  if (n > 1) {
    ret |= b[1] << 8;
    if (n > 2) {
      ret |= b[2] << 16;
      if (n > 3) {
        ret |= b[3] << 24;
      }
    }
  }
  return ret;
}

static uint32_t get_unaligned_le_v2(const void *p, uint32_t n) {
  const uint8_t *b = (const uint8_t *)p;
  uint32_t ret = b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
  ret &= wordmask[n];
  return ret;
}

Which would be better on read hardware (I’m using qemu for development) and can you suggest a faster alternative? If it’s much faster, I’m game with using assembly.

  • 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-30T21:48:45+00:00Added an answer on May 30, 2026 at 9:48 pm

    Conditional execution on the ARM is your best bet for improved performance. Table lookups (masks) will definitely be slower on ARM. Here is my ARMv5 implementation:

    // When called from C, r0 = first parameter, r1 = second parameter
    // r0-r3 and r12 can get trashed by C functions
    unaligned_read:
      ldrb r2,[r0],#1      ; byte 0 is always read (n=1..4)
      cmp r1,#2
      ldrgeb r3,[r0],#1   ; byte 1, n >= 2
      ldrgtb r12,[r0],#1  ; byte 2, n > 2
      orrge r2,r2,r3,LSL #8
      orrgt r2,r2,r12,LSL #16
      cmp r1,#4
      ldreqb r3,[r0],#1   ; byte 3, n == 4
      movne r0,r2         ; recoup wasted cycle
      orreq r0,r2,r3,LSL #24
      mov pc,lr           ; or "bx lr" for thumb compatibility
    

    Update: fixed ldreqb to be ldrgeb

    Update 2: shaved off another cycle by inserting an instruction between last ldr/orr

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

Sidebar

Related Questions

This code does not compile in GHC 7.0.3: import System.IO main = do z
For a school assignment I have to write x86 assembly code, except I can't
I have some code that effectively does this : File file = new File(C:\\Program
Does anyone know how I can get rid of the following assembler warning? Code
I'm looking for a library that will disassemble x86 code into some sort of
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code like this often happens: l = [] while foo: # baz l.append(bar) #
I have a Fortran program, which I can compile using f77, f95 or ifort
When you compile windows application in .NET you can set Platform Target to be
This does not always happen, but sometimes when I try to build a (large)

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.