+/* Copyright (C) 2009 Pierre-Marc Fournier
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
#ifndef UST_PROCESSOR_H
#define UST_PROCESSOR_H
-#include <stddef.h>
-
-#ifdef X86_32
+#define ____cacheline_aligned __attribute__((aligned(CAA_CACHE_LINE_SIZE)))
-struct registers {
- long eax;
- long ebx;
- long ecx;
- long edx;
- long ebp;
- long esp;
- long esi;
- long edi;
- int xds;
- int xes;
- int xfs;
- int xgs;
- long eip;
- int xcs;
- long eflags;
- int xss;
-};
+#ifdef __i386
-static inline save_registers(struct registers *regs)
+static inline int fls(int x)
{
+ int r;
+ asm("bsrl %1,%0\n\t"
+ "cmovzl %2,%0"
+ : "=&r" (r) : "rm" (x), "rm" (-1));
+ return r + 1;
}
-#define RELATIVE_ADDRESS(__rel_label__) __rel_label__
+#elif defined(__x86_64)
-#define _ASM_PTR ".long "
+static inline int fls(int x)
+{
+ int r;
+ asm("bsrl %1,%0\n\t"
+ "cmovzl %2,%0"
+ : "=&r" (r) : "rm" (x), "rm" (-1));
+ return r + 1;
+}
-#else
+#elif defined(__PPC__)
-struct registers {
- unsigned long rax;
- unsigned long rbx;
- unsigned long rcx;
- unsigned long rdx;
- unsigned long rbp;
- unsigned long rsp;
- unsigned long rsi;
- unsigned long rdi;
- unsigned long r8;
- unsigned long r9;
- unsigned long r10;
- unsigned long r11;
- unsigned long r12;
- unsigned long r13;
- unsigned long r14;
- unsigned long r15;
- int cs;
- int ss;
- unsigned long rflags;
-};
+static __inline__ int fls(unsigned int x)
+{
+ int lz;
-#define save_registers(regsptr) \
- asm ("movq %%rax,%c[rax_off](%[regs])\n\t" \
- "movq %%rbx,%c[rbx_off](%[regs])\n\t" \
- "movq %%rcx,%c[rcx_off](%[regs])\n\t" \
- "movq %%rdx,%c[rdx_off](%[regs])\n\t" \
- "movq %%rbp,%c[rbp_off](%[regs])\n\t" \
- "movq %%rsp,%c[rsp_off](%[regs])\n\t" \
- "movq %%rsi,%c[rsi_off](%[regs])\n\t" \
- "movq %%rdi,%c[rdi_off](%[regs])\n\t" \
- "movq %%r8,%c[r8_off](%[regs])\n\t" \
- "movq %%r9,%c[r9_off](%[regs])\n\t" \
- "movq %%r10,%c[r10_off](%[regs])\n\t" \
- "movq %%r11,%c[r11_off](%[regs])\n\t" \
- "movq %%r12,%c[r12_off](%[regs])\n\t" \
- "movq %%r13,%c[r13_off](%[regs])\n\t" \
- "movq %%r14,%c[r14_off](%[regs])\n\t" \
- "movq %%r15,%c[r15_off](%[regs])\n\t" \
- "movw %%cs,%c[cs_off](%[regs])\n\t" \
- "movw %%ss,%c[ss_off](%[regs])\n\t" \
- /* deal with rflags */ \
- "pushfq\n\t" /* push rflags on stack */ \
- "popq %c[rflags_off](%[regs])\n\t" \
- : \
- : [regs] "r" (regsptr), \
- [rax_off] "i" (offsetof(struct registers, rax)), \
- [rbx_off] "i" (offsetof(struct registers, rbx)), \
- [rcx_off] "i" (offsetof(struct registers, rcx)), \
- [rdx_off] "i" (offsetof(struct registers, rdx)), \
- [rbp_off] "i" (offsetof(struct registers, rbp)), \
- [rsp_off] "i" (offsetof(struct registers, rsp)), \
- [rsi_off] "i" (offsetof(struct registers, rsi)), \
- [rdi_off] "i" (offsetof(struct registers, rdi)), \
- [r8_off] "i" (offsetof(struct registers, r8)), \
- [r9_off] "i" (offsetof(struct registers, r9)), \
- [r10_off] "i" (offsetof(struct registers, r10)), \
- [r11_off] "i" (offsetof(struct registers, r11)), \
- [r12_off] "i" (offsetof(struct registers, r12)), \
- [r13_off] "i" (offsetof(struct registers, r13)), \
- [r14_off] "i" (offsetof(struct registers, r14)), \
- [r15_off] "i" (offsetof(struct registers, r15)), \
- [cs_off] "i" (offsetof(struct registers, cs)), \
- [ss_off] "i" (offsetof(struct registers, ss)), \
- [rflags_off] "i" (offsetof(struct registers, rflags)) \
- );
+ asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
+ return 32 - lz;
+}
+
+#else /* arch-agnostic */
-/* Macro to insert the address of a relative jump in an assembly stub,
- * in a relocatable way. On x86-64, this uses a special (%rip) notation. */
-#define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip)
+static __inline__ int fls(unsigned int x)
+{
+ int r = 32;
-#define _ASM_PTR ".quad "
+ if (!x)
+ return 0;
+ if (!(x & 0xFFFF0000U)) {
+ x <<= 16;
+ r -= 16;
+ }
+ if (!(x & 0xFF000000U)) {
+ x <<= 8;
+ r -= 8;
+ }
+ if (!(x & 0xF0000000U)) {
+ x <<= 4;
+ r -= 4;
+ }
+ if (!(x & 0xC0000000U)) {
+ x <<= 2;
+ r -= 2;
+ }
+ if (!(x & 0x80000000U)) {
+ x <<= 1;
+ r -= 1;
+ }
+ return r;
+}
#endif