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
#ifdef C_SUN
kusano 2b45e8
#define	__asm__ __asm
kusano 2b45e8
#define	__volatile__
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef HAVE_SSE2
kusano 2b45e8
#define MB   __asm__ __volatile__ ("mfence");
kusano 2b45e8
#define WMB  __asm__ __volatile__ ("sfence");
kusano 2b45e8
#else
kusano 2b45e8
#define MB
kusano 2b45e8
#define WMB
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
static __inline BLASULONG rpcc(void){
kusano 2b45e8
  BLASULONG a, d;
kusano 2b45e8
kusano 2b45e8
  __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
kusano 2b45e8
  
kusano 2b45e8
  return ((BLASULONG)a + ((BLASULONG)d << 32));  
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
#define RPCC64BIT
kusano 2b45e8
kusano 2b45e8
static __inline BLASULONG getstackaddr(void){
kusano 2b45e8
  BLASULONG addr;
kusano 2b45e8
kusano 2b45e8
  __asm__ __volatile__ ("movq %%rsp, %0"
kusano 2b45e8
			 : "=r"(addr) : : "memory");
kusano 2b45e8
kusano 2b45e8
  return addr;  
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
static __inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
kusano 2b45e8
kusano 2b45e8
        __asm__ __volatile__("cpuid"
kusano 2b45e8
			     : "=a" (*eax),
kusano 2b45e8
			     "=b" (*ebx),
kusano 2b45e8
			     "=c" (*ecx),
kusano 2b45e8
			     "=d" (*edx)
kusano 2b45e8
			     : "0" (op));
kusano 2b45e8
}
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 CORE_BARCELONA
kusano 2b45e8
#define IFLUSH		gotoblas_iflush()
kusano 2b45e8
#define IFLUSH_HALF	gotoblas_iflush_half()
kusano 2b45e8
#endif
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__("movsd %%xmm1, %0" : "=m"(res) : : "memory")
kusano 2b45e8
#else
kusano 2b45e8
#define GET_IMAGE(res)  __asm__ __volatile__("movss %%xmm1, %0" : "=m"(res) : : "memory")
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#define GET_IMAGE_CANCEL
kusano 2b45e8
kusano 2b45e8
#ifdef SMP
kusano 2b45e8
#ifdef USE64BITINT
kusano 2b45e8
static __inline blasint blas_quickdivide(blasint x, blasint y){
kusano 2b45e8
  return x / y;
kusano 2b45e8
}
kusano 2b45e8
#else
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
#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	( 2 << 20)
kusano 2b45e8
kusano 2b45e8
#define BUFFER_SIZE	(32 << 20)
kusano 2b45e8
kusano 2b45e8
#define SEEK_ADDRESS
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_G77
kusano 2b45e8
#define RETURN_BY_STACK
kusano 2b45e8
#define NEED_F2CCONV
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_G95
kusano 2b45e8
#define RETURN_BY_PACKED
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_GFORT
kusano 2b45e8
#ifdef OS_WINDOWS
kusano 2b45e8
#ifndef DOUBLE
kusano 2b45e8
#define RETURN_BY_REGS
kusano 2b45e8
#else
kusano 2b45e8
#define RETURN_BY_STACK
kusano 2b45e8
#endif
kusano 2b45e8
#else
kusano 2b45e8
#define RETURN_BY_PACKED
kusano 2b45e8
#endif
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_INTEL
kusano 2b45e8
#define RETURN_BY_STACK
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_FUJITSU
kusano 2b45e8
#define RETURN_BY_STACK
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_PGI
kusano 2b45e8
#define RETURN_BY_STACK
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_PATHSCALE
kusano 2b45e8
#define RETURN_BY_PACKED
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef F_INTERFACE_SUN
kusano 2b45e8
#define RETURN_BY_PACKED
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef ASSEMBLER
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
#define BRANCH		.byte 0x3e
kusano 2b45e8
#define NOBRANCH	.byte 0x2e
kusano 2b45e8
#define PADDING		.byte 0x66
kusano 2b45e8
kusano 2b45e8
#ifdef OS_WINDOWS
kusano 2b45e8
#define ARG1	%rcx
kusano 2b45e8
#define ARG2	%rdx
kusano 2b45e8
#define ARG3	%r8
kusano 2b45e8
#define ARG4	%r9
kusano 2b45e8
#else
kusano 2b45e8
#define ARG1	%rdi
kusano 2b45e8
#define ARG2	%rsi
kusano 2b45e8
#define ARG3	%rdx
kusano 2b45e8
#define ARG4	%rcx
kusano 2b45e8
#define ARG5	%r8
kusano 2b45e8
#define ARG6	%r9
kusano 2b45e8
#endif
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(%rsp);\
kusano 2b45e8
	movl	$0,  4096 * 3(%rsp);\
kusano 2b45e8
	movl	$0,  4096 * 2(%rsp);\
kusano 2b45e8
	movl	$0,  4096 * 1(%rsp);
kusano 2b45e8
#elif LOCAL_BUFFER_SIZE > 12288
kusano 2b45e8
#define STACK_TOUCHING \
kusano 2b45e8
	movl	$0,  4096 * 3(%rsp);\
kusano 2b45e8
	movl	$0,  4096 * 2(%rsp);\
kusano 2b45e8
	movl	$0,  4096 * 1(%rsp);
kusano 2b45e8
#elif LOCAL_BUFFER_SIZE > 8192
kusano 2b45e8
#define STACK_TOUCHING \
kusano 2b45e8
	movl	$0,  4096 * 2(%rsp);\
kusano 2b45e8
	movl	$0,  4096 * 1(%rsp);
kusano 2b45e8
#elif LOCAL_BUFFER_SIZE > 4096
kusano 2b45e8
#define STACK_TOUCHING \
kusano 2b45e8
	movl	$0,  4096 * 1(%rsp);
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
#if defined(CORE2)
kusano 2b45e8
#define movapd	movaps
kusano 2b45e8
#define andpd	andps
kusano 2b45e8
#define movlpd	movlps
kusano 2b45e8
#define movhpd	movhps
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
#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
#ifdef OS_WINDOWS
kusano 2b45e8
#define SAVEREGISTERS \
kusano 2b45e8
	subq	$256, %rsp;\
kusano 2b45e8
	movups	%xmm6,    0(%rsp);\
kusano 2b45e8
	movups	%xmm7,   16(%rsp);\
kusano 2b45e8
	movups	%xmm8,   32(%rsp);\
kusano 2b45e8
	movups	%xmm9,   48(%rsp);\
kusano 2b45e8
	movups	%xmm10,  64(%rsp);\
kusano 2b45e8
	movups	%xmm11,  80(%rsp);\
kusano 2b45e8
	movups	%xmm12,  96(%rsp);\
kusano 2b45e8
	movups	%xmm13, 112(%rsp);\
kusano 2b45e8
	movups	%xmm14, 128(%rsp);\
kusano 2b45e8
	movups	%xmm15, 144(%rsp)
kusano 2b45e8
kusano 2b45e8
#define RESTOREREGISTERS \
kusano 2b45e8
	movups	   0(%rsp), %xmm6;\
kusano 2b45e8
	movups	  16(%rsp), %xmm7;\
kusano 2b45e8
	movups	  32(%rsp), %xmm8;\
kusano 2b45e8
	movups	  48(%rsp), %xmm9;\
kusano 2b45e8
	movups	  64(%rsp), %xmm10;\
kusano 2b45e8
	movups	  80(%rsp), %xmm11;\
kusano 2b45e8
	movups	  96(%rsp), %xmm12;\
kusano 2b45e8
	movups	 112(%rsp), %xmm13;\
kusano 2b45e8
	movups	 128(%rsp), %xmm14;\
kusano 2b45e8
	movups	 144(%rsp), %xmm15;\
kusano 2b45e8
	addq	$256, %rsp
kusano 2b45e8
#else
kusano 2b45e8
#define SAVEREGISTERS
kusano 2b45e8
#define RESTOREREGISTERS
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#if defined(OS_WINDOWS) && !defined(C_PGI)
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__) || defined(C_PGI)
kusano 2b45e8
#define PROLOGUE \
kusano 2b45e8
	.text; \
kusano 2b45e8
	.align 512; \
kusano 2b45e8
	.globl REALNAME ;\
kusano 2b45e8
       .type REALNAME, @function; \
kusano 2b45e8
REALNAME:
kusano 2b45e8
kusano 2b45e8
#ifdef PROFILE
kusano 2b45e8
#define PROFCODE call *mcount@GOTPCREL(%rip)
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
#endif
kusano 2b45e8
kusano 2b45e8
#ifdef XDOUBLE
kusano 2b45e8
#define FLD	fldt
kusano 2b45e8
#define FST	fstpt
kusano 2b45e8
#define MOVQ	movq
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 MOVSD	movsd
kusano 2b45e8
#define MULSD	mulsd
kusano 2b45e8
#define MULPD	mulpd
kusano 2b45e8
#define CMPEQPD	cmpeqpd
kusano 2b45e8
#define COMISD	comisd
kusano 2b45e8
#define PSRLQ	psrlq
kusano 2b45e8
#define ANDPD	andpd
kusano 2b45e8
#define ADDPD	addpd
kusano 2b45e8
#define ADDSD	addsd
kusano 2b45e8
#define SUBPD	subpd
kusano 2b45e8
#define SUBSD	subsd
kusano 2b45e8
#define MOVQ	movq
kusano 2b45e8
#define MOVUPD	movupd
kusano 2b45e8
#define XORPD	xorpd
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 MOVSD	movss
kusano 2b45e8
#define MULSD	mulss
kusano 2b45e8
#define MULPD	mulps
kusano 2b45e8
#define CMPEQPD	cmpeqps
kusano 2b45e8
#define COMISD	comiss
kusano 2b45e8
#define PSRLQ	psrld
kusano 2b45e8
#define ANDPD	andps
kusano 2b45e8
#define ADDPD	addps
kusano 2b45e8
#define ADDSD	addss
kusano 2b45e8
#define SUBPD	subps
kusano 2b45e8
#define SUBSD	subss
kusano 2b45e8
#define MOVQ	movd
kusano 2b45e8
#define MOVUPD	movups
kusano 2b45e8
#define XORPD	xorps
kusano 2b45e8
#endif
kusano 2b45e8
kusano 2b45e8
#define HALT	hlt
kusano 2b45e8
kusano 2b45e8
#ifdef OS_DARWIN
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
kusano 2b45e8
kusano 2b45e8
#endif