decodeLogicalImmediate, using division instead of a table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdbool.h> | |
#define DECODE_FAILURE 0 // not a valid logical immediate | |
static inline int nonzeroCountLeadingZeros32(uint32_t n) { | |
return __builtin_clz(n); | |
} | |
static inline uint64_t rotateRight64(uint64_t v, int n) { | |
return (v >> (n & 63)) | (v << (-n & 63)); | |
} | |
// Division expression by @zwegner: | |
// https://twitter.com/zwegner/status/1454543857023062016 | |
uint64_t decodeLogicalImmediate64(int N, int immr, int imms) { | |
unsigned pattern = (N << 6) | (~imms & 0x3F); | |
if (!(pattern & (pattern - 1))) return DECODE_FAILURE; | |
unsigned size = 0x80000000 >> nonzeroCountLeadingZeros32(pattern); | |
uint64_t mask = ~0ull / ((1ull << (size & 63)) | 1); | |
unsigned ones = (imms + 1) & (size - 1); | |
return rotateRight64(mask ^ (mask << ones), immr); | |
} | |
uint32_t decodeLogicalImmediate32(int N, int immr, int imms) { | |
if (N) return DECODE_FAILURE; | |
return (uint32_t)decodeLogicalImmediate64(N, immr, imms); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment