Decode AArch64 Logical Immediate Bitmask Encodings
Last updated: November 29, 2026
Quick Overview
AArch64 instructions like AND and ORR encode a 64-bit immediate as three fields N, immr and imms that describe a rotated run of ones replicated across the register. Write a decoder that returns the bitmask or reports a reserved encoding, and use it to decide whether an arbitrary constant is encodable.
Arm
November 29, 20260
0
0 solved
AArch64 instructions like AND and ORR encode a 64-bit immediate as three fields N, immr and imms that describe a rotated run of ones replicated across the register. Write a decoder that returns the bitmask or reports a reserved encoding, and use it to decide whether an arbitrary constant is encodable.
This sits squarely in the work of Arm's compiler and toolchain engineers (LLVM, GCC, Arm Compiler), whose instruction selection must decide whether a constant fits a logical immediate or needs a MOVZ/MOVK sequence. The interviewer checks whether you can turn a compact hardware encoding into precise code and reason about its structure.
What the Interviewer Expects
- Find the element size from the highest set bit of N concatenated with NOT imms
- Build a run of S+1 ones and rotate it right by R within the element
- Replicate the element across 32 or 64 bits
- Reject reserved encodings: all-ones element, N=1 in 32-bit mode, empty length
- Discuss how to invert the mapping efficiently
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Write an O(1) or O(log n) encoder instead of brute force; how do you detect the element size of a value?
- Why can 0 and all-ones never be encoded, and what does a compiler emit instead?
- How many distinct 64-bit values are encodable, and how would you verify your count?
- How does this differ from the SVE DUPM and bitwise immediate forms?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs: n (1 bit), immr and imms (6 bits each), datasize of 32 or 64. Output: the mask, or None for a reserved encoding. The value is made of identical elements of size 2, 4, 8, 16, 32 or 64...
High-Level Design
- Concatenate
Nwith the bitwise NOT ofimmsinto a 7-bit number; the position of its highest set bit islen, andesize = 2^len. This is whyimmsuses leading ones as a size prefix (for exa...