kusano 2b45e8
/*********************************************************************/
kusano 2b45e8
/* Copyright 2009, 2010 The University of Texas at Austin.           */
kusano 2b45e8
/* All rights reserved.                                              */
kusano 2b45e8
/*                                                                   */
kusano 2b45e8
/* Redistribution and use in source and binary forms, with or        */
kusano 2b45e8
/* without modification, are permitted provided that the following   */
kusano 2b45e8
/* conditions are met:                                               */
kusano 2b45e8
/*                                                                   */
kusano 2b45e8
/*   1. Redistributions of source code must retain the above         */
kusano 2b45e8
/*      copyright notice, this list of conditions and the following  */
kusano 2b45e8
/*      disclaimer.                                                  */
kusano 2b45e8
/*                                                                   */
kusano 2b45e8
/*   2. Redistributions in binary form must reproduce the above      */
kusano 2b45e8
/*      copyright notice, this list of conditions and the following  */
kusano 2b45e8
/*      disclaimer in the documentation and/or other materials       */
kusano 2b45e8
/*      provided with the distribution.                              */
kusano 2b45e8
/*                                                                   */
kusano 2b45e8
/*    THIS  SOFTWARE IS PROVIDED  BY THE  UNIVERSITY OF  TEXAS AT    */
kusano 2b45e8
/*    AUSTIN  ``AS IS''  AND ANY  EXPRESS OR  IMPLIED WARRANTIES,    */
kusano 2b45e8
/*    INCLUDING, BUT  NOT LIMITED  TO, THE IMPLIED  WARRANTIES OF    */
kusano 2b45e8
/*    MERCHANTABILITY  AND FITNESS FOR  A PARTICULAR  PURPOSE ARE    */
kusano 2b45e8
/*    DISCLAIMED.  IN  NO EVENT SHALL THE UNIVERSITY  OF TEXAS AT    */
kusano 2b45e8
/*    AUSTIN OR CONTRIBUTORS BE  LIABLE FOR ANY DIRECT, INDIRECT,    */
kusano 2b45e8
/*    INCIDENTAL,  SPECIAL, EXEMPLARY,  OR  CONSEQUENTIAL DAMAGES    */
kusano 2b45e8
/*    (INCLUDING, BUT  NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE    */
kusano 2b45e8
/*    GOODS  OR  SERVICES; LOSS  OF  USE,  DATA,  OR PROFITS;  OR    */
kusano 2b45e8
/*    BUSINESS INTERRUPTION) HOWEVER CAUSED  AND ON ANY THEORY OF    */
kusano 2b45e8
/*    LIABILITY, WHETHER  IN CONTRACT, STRICT  LIABILITY, OR TORT    */
kusano 2b45e8
/*    (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY WAY OUT    */
kusano 2b45e8
/*    OF  THE  USE OF  THIS  SOFTWARE,  EVEN  IF ADVISED  OF  THE    */
kusano 2b45e8
/*    POSSIBILITY OF SUCH DAMAGE.                                    */
kusano 2b45e8
/*                                                                   */
kusano 2b45e8
/* The views and conclusions contained in the software and           */
kusano 2b45e8
/* documentation are those of the authors and should not be          */
kusano 2b45e8
/* interpreted as representing official policies, either expressed   */
kusano 2b45e8
/* or implied, of The University of Texas at Austin.                 */
kusano 2b45e8
/*********************************************************************/
kusano 2b45e8
kusano 2b45e8
#ifndef COMMON_X86
kusano 2b45e8
#define COMMON_X86
kusano 2b45e8
kusano 2b45e8
#ifndef ASSEMBLER
kusano 2b45e8
kusano 2b45e8
#define MB
kusano 2b45e8
#define WMB
kusano 2b45e8
kusano 2b45e8
#ifdef C_SUN
kusano 2b45e8
#define	__asm__ __asm
kusano 2b45e8
#define	__volatile__
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
static void __inline blas_lock(volatile BLASULONG *address){
kusano 2b45e8
kusano 2b45e8
  int ret;
kusano 2b45e8
kusano 2b45e8
  do {
kusano 2b45e8
    while (*address) {YIELDING;};
kusano 2b45e8
    
kusano 2b45e8
    __asm__ __volatile__(
kusano 2b45e8
			 "xchgl %0, %1\n"
kusano 2b45e8
			 : "=r"(ret), "=m"(*address)
kusano 2b45e8
			 : "0"(1), "m"(*address)
kusano 2b45e8
			 : "memory");
kusano 2b45e8
kusano 2b45e8
  } while (ret);
kusano 2b45e8
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
static __inline unsigned long long rpcc(void){
kusano 2b45e8
  unsigned int a, d;
kusano 2b45e8
kusano 2b45e8
  __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
kusano 2b45e8
  
kusano 2b45e8
  return ((unsigned long long)a + ((unsigned long long)d << 32));  
kusano 2b45e8
};
kusano 2b45e8
kusano 2b45e8
static __inline unsigned long getstackaddr(void){
kusano 2b45e8
  unsigned long addr;
kusano 2b45e8
kusano 2b45e8
  __asm__ __volatile__ ("mov %%esp, %0"
kusano 2b45e8
			 : "=r"(addr) : : "memory");
kusano 2b45e8
kusano 2b45e8
  return addr;  
kusano 2b45e8
};
kusano 2b45e8
kusano 2b45e8
kusano 2b45e8
static __inline long double sqrt_long(long double val) {
kusano 2b45e8
  long double result;
kusano 2b45e8
kusano 2b45e8
  __asm__ __volatile__ ("fldt %1\n"
kusano 2b45e8
		    "fsqrt\n"
kusano 2b45e8
		    "fstpt %0\n" : "=m" (result) : "m"(val));
kusano 2b45e8
  return result;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
#define SQRT(a)  sqrt_long(a)
kusano 2b45e8
kusano 2b45e8
/* This is due to gcc's bug */
kusano 2b45e8
void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
kusano 2b45e8
kusano 2b45e8
#define WHEREAMI
kusano 2b45e8
kusano 2b45e8
static inline int WhereAmI(void){
kusano 2b45e8
  int eax, ebx, ecx, edx;
kusano 2b45e8
  int apicid;
kusano 2b45e8
kusano 2b45e8
  cpuid(1, &eax, &ebx, &ecx, &edx);
kusano 2b45e8
  apicid  = BITMASK(ebx, 24, 0xff);
kusano 2b45e8
kusano 2b45e8
  return apicid;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
#ifdef ENABLE_SSE_EXCEPTION
kusano 2b45e8
kusano 2b45e8
#define IDEBUG_START \
kusano 2b45e8
{ \
kusano 2b45e8
  unsigned int fp_sse_mode, new_fp_mode; \
kusano 2b45e8
  __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
kusano 2b45e8
  new_fp_mode = fp_sse_mode & ~0xd00; \
kusano 2b45e8
  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
kusano 2b45e8
kusano 2b45e8
#define IDEBUG_END \
kusano 2b45e8
  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef XDOUBLE
kusano 2b45e8
#define GET_IMAGE(res)  __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
kusano 2b45e8
#elif defined(DOUBLE)
kusano 2b45e8
#define GET_IMAGE(res)  __asm__ __volatile__("fstpl %0" : "=m"(res) : : "memory")
kusano 2b45e8
#else
kusano 2b45e8
#define GET_IMAGE(res)  __asm__ __volatile__("fstps %0" : "=m"(res) : : "memory");
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#define GET_IMAGE_CANCEL	__asm__ __volatile__ ("ffree %st")
kusano 2b45e8
kusano 2b45e8
#ifdef SMP
kusano 2b45e8
extern unsigned int blas_quick_divide_table[];
kusano 2b45e8
kusano 2b45e8
static __inline int blas_quickdivide(unsigned int x, unsigned int y){
kusano 2b45e8
kusano 2b45e8
  unsigned int result;
kusano 2b45e8
kusano 2b45e8
  if (y <= 1) return x;
kusano 2b45e8
kusano 2b45e8
  y = blas_quick_divide_table[y];
kusano 2b45e8
kusano 2b45e8
  __asm__ __volatile__  ("mull %0" :"=d" (result) :"a"(x), "0" (y));
kusano 2b45e8
kusano 2b45e8
  return result;
kusano 2b45e8
}
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef PAGESIZE
kusano 2b45e8
#define PAGESIZE	( 4 << 10)
kusano 2b45e8
#endif
kusano 2b45e8
#define HUGE_PAGESIZE	( 4 << 20)
kusano 2b45e8
kusano 2b45e8
#define BUFFER_SIZE	(16 << 20)
kusano 2b45e8
kusano 2b45e8
#define SEEK_ADDRESS
kusano 2b45e8
kusano 2b45e8
#if defined(DOUBLE) || defined(XDOUBLE)
kusano 2b45e8
#define	MMXLOAD		movq
kusano 2b45e8
#define MMXSTORE	movq
kusano 2b45e8
#else
kusano 2b45e8
#define MMXLOAD		movd
kusano 2b45e8
#define MMXSTORE	movd
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(HAVE_3DNOW)
kusano 2b45e8
#define EMMS	femms
kusano 2b45e8
#elif defined(HAVE_MMX)
kusano 2b45e8
#define EMMS	emms
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef EMMS
kusano 2b45e8
#define EMMS
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(CORE2) || defined(PENTIUM4)
kusano 2b45e8
#define movapd	movaps
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#define BRANCH		.byte 0x3e
kusano 2b45e8
#define NOBRANCH	.byte 0x2e
kusano 2b45e8
#define PADDING		.byte 0x66;
kusano 2b45e8
#define HALT		hlt
kusano 2b45e8
kusano 2b45e8
#ifndef COMPLEX
kusano 2b45e8
#ifdef XDOUBLE
kusano 2b45e8
#define LOCAL_BUFFER_SIZE  QLOCAL_BUFFER_SIZE
kusano 2b45e8
#elif defined DOUBLE
kusano 2b45e8
#define LOCAL_BUFFER_SIZE  DLOCAL_BUFFER_SIZE
kusano 2b45e8
#else
kusano 2b45e8
#define LOCAL_BUFFER_SIZE  SLOCAL_BUFFER_SIZE
kusano 2b45e8
#endif
kusano 2b45e8
#else
kusano 2b45e8
#ifdef XDOUBLE
kusano 2b45e8
#define LOCAL_BUFFER_SIZE  XLOCAL_BUFFER_SIZE
kusano 2b45e8
#elif defined DOUBLE
kusano 2b45e8
#define LOCAL_BUFFER_SIZE  ZLOCAL_BUFFER_SIZE
kusano 2b45e8
#else
kusano 2b45e8
#define LOCAL_BUFFER_SIZE  CLOCAL_BUFFER_SIZE
kusano 2b45e8
#endif
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(OS_WINDOWS)
kusano 2b45e8
#if   LOCAL_BUFFER_SIZE > 16384
kusano 2b45e8
#define STACK_TOUCHING \
kusano 2b45e8
	movl	$0,  4096 * 4(%esp);\
kusano 2b45e8
	movl	$0,  4096 * 3(%esp);\
kusano 2b45e8
	movl	$0,  4096 * 2(%esp);\
kusano 2b45e8
	movl	$0,  4096 * 1(%esp);
kusano 2b45e8
#elif LOCAL_BUFFER_SIZE > 12288
kusano 2b45e8
#define STACK_TOUCHING \
kusano 2b45e8
	movl	$0,  4096 * 3(%esp);\
kusano 2b45e8
	movl	$0,  4096 * 2(%esp);\
kusano 2b45e8
	movl	$0,  4096 * 1(%esp);
kusano 2b45e8
#elif LOCAL_BUFFER_SIZE > 8192
kusano 2b45e8
#define STACK_TOUCHING \
kusano 2b45e8
	movl	$0,  4096 * 2(%esp);\
kusano 2b45e8
	movl	$0,  4096 * 1(%esp);
kusano 2b45e8
#elif LOCAL_BUFFER_SIZE > 4096
kusano 2b45e8
#define STACK_TOUCHING \
kusano 2b45e8
	movl	$0,  4096 * 1(%esp);
kusano 2b45e8
#else
kusano 2b45e8
#define STACK_TOUCHING
kusano 2b45e8
#endif
kusano 2b45e8
#else
kusano 2b45e8
#define STACK_TOUCHING
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef F_INTERFACE
kusano 2b45e8
#define REALNAME ASMNAME
kusano 2b45e8
#else
kusano 2b45e8
#define REALNAME ASMFNAME
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(F_INTERFACE_PATHSCALE) || defined(F_INTERFACE_OPEN64)
kusano 2b45e8
#define RETURN_BY_STRUCT
kusano 2b45e8
#elif defined(F_INTERFACE_GFORT) || defined(F_INTERFACE_G95)
kusano 2b45e8
#define RETURN_BY_COMPLEX
kusano 2b45e8
#else
kusano 2b45e8
#define RETURN_BY_STACK
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef OS_DARWIN
kusano 2b45e8
#define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
kusano 2b45e8
#define EPILOGUE	.subsections_via_symbols
kusano 2b45e8
#define PROFCODE
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INERIX)
kusano 2b45e8
#define SAVEREGISTERS \
kusano 2b45e8
	subl	$32, %esp;\
kusano 2b45e8
	movups	%xmm6,    0(%esp);\
kusano 2b45e8
	movups	%xmm7,   16(%esp)
kusano 2b45e8
kusano 2b45e8
#define RESTOREREGISTERS \
kusano 2b45e8
	movups	   0(%esp), %xmm6;\
kusano 2b45e8
	movups	  16(%esp), %xmm7;\
kusano 2b45e8
	addl	$32, %esp
kusano 2b45e8
#else
kusano 2b45e8
#define SAVEREGISTERS
kusano 2b45e8
#define RESTOREREGISTERS
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INERIX)
kusano 2b45e8
#define PROLOGUE \
kusano 2b45e8
	.text; \
kusano 2b45e8
	.align 16; \
kusano 2b45e8
	.globl REALNAME ;\
kusano 2b45e8
	.def REALNAME;.scl	2;.type	32;.endef; \
kusano 2b45e8
REALNAME:
kusano 2b45e8
kusano 2b45e8
#define PROFCODE
kusano 2b45e8
kusano 2b45e8
#define EPILOGUE .end	 REALNAME
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(OS_LINUX) || defined(OS_FreeBSD) || defined(OS_NetBSD) || defined(__ELF__)
kusano 2b45e8
#define PROLOGUE \
kusano 2b45e8
	.text; \
kusano 2b45e8
	.align 16; \
kusano 2b45e8
	.globl REALNAME ;\
kusano 2b45e8
       .type REALNAME, @function; \
kusano 2b45e8
REALNAME:
kusano 2b45e8
kusano 2b45e8
#ifdef PROFILE
kusano 2b45e8
#define PROFCODE call mcount
kusano 2b45e8
#else
kusano 2b45e8
#define PROFCODE
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#define EPILOGUE .size	 REALNAME, .-REALNAME
kusano 2b45e8
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef XDOUBLE
kusano 2b45e8
#define FLD	fldt
kusano 2b45e8
#define FST	fstpt
kusano 2b45e8
#define FSTU	fstt
kusano 2b45e8
#define FMUL	fmult
kusano 2b45e8
#define FADD	faddt
kusano 2b45e8
#define FSUB	fsubt
kusano 2b45e8
#define FSUBR	fsubrt
kusano 2b45e8
#elif defined(DOUBLE)
kusano 2b45e8
#define FLD	fldl
kusano 2b45e8
#define FST	fstpl
kusano 2b45e8
#define FSTU	fstl
kusano 2b45e8
#define FMUL	fmull
kusano 2b45e8
#define FADD	faddl
kusano 2b45e8
#define FSUB	fsubl
kusano 2b45e8
#define FSUBR	fsubrl
kusano 2b45e8
#else
kusano 2b45e8
#define FLD	flds
kusano 2b45e8
#define FST	fstps
kusano 2b45e8
#define FSTU	fsts
kusano 2b45e8
#define FMUL	fmuls
kusano 2b45e8
#define FADD	fadds
kusano 2b45e8
#define FSUB	fsubs
kusano 2b45e8
#define FSUBR	fsubrs
kusano 2b45e8
#endif
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef C_SUN
kusano 2b45e8
#define	ffreep	fstp
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef __APPLE__
kusano 2b45e8
#define ALIGN_2 .align 2
kusano 2b45e8
#define ALIGN_3 .align 3
kusano 2b45e8
#define ALIGN_4 .align 4
kusano 2b45e8
#define ffreep	fstp
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef ALIGN_2
kusano 2b45e8
#define ALIGN_2 .align 4
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef ALIGN_3
kusano 2b45e8
#define ALIGN_3 .align 8
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef ALIGN_4
kusano 2b45e8
#define ALIGN_4 .align 16
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef ALIGN_5
kusano 2b45e8
#define ALIGN_5 .align 32
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifndef ALIGN_6
kusano 2b45e8
#define ALIGN_6 .align 64
kusano 2b45e8
#endif