/*=============================================================================
	CAVectorUnit.cpp
	
	$Log: CAVectorUnit.cpp,v $
	Revision 1.7  2006/05/20 00:31:58  asynth
	TARGET_CPU_X86_ANY
	
	Revision 1.6  2006/03/31 00:40:28  asynth
	4497156 TARGET_CPU_PPC_ANY
	
	Revision 1.5  2006/02/17 00:53:28  dwyatt
	why use gestalt when there's sysctl
	
	Revision 1.4  2006/01/11 21:11:20  dwyatt
	__LP64__
	
	Revision 1.3  2005/12/03 03:35:13  ealdrich
	Making things build and link on Windows
	
	Revision 1.2  2005/07/09 00:20:19  dwyatt
	don't use SUPPORT_AU_V1, fix uninitialized result in X86
	
	Revision 1.1  2005/07/08 00:12:54  asynth
	add CAVectorUnit
	
	
	created 07/06/05, James McCartney
	Copyright (c) 2005 Apple Computer, Inc.  All Rights Reserved

	$NoKeywords: $
=============================================================================*/

#include <CAConditionalMacros.h>
#include "CAVectorUnit.h"

#if !TARGET_OS_WIN32
	#include <sys/sysctl.h>
#endif

int CAVectorUnit::sVectorUnitType = kVecUninitialized;

SInt32	CAVectorUnit::CheckVectorUnit()
{
	int result = kVecNone;
	
#if !TARGET_OS_WIN32
#if DEBUG
	if (getenv("CA_NoVector")) {
		fprintf(stderr, "CA_NoVector set; Vector unit optimized routines will be bypassed\n");
		return result;
	} 
	else
#endif
	{
	#if TARGET_CPU_PPC_ANY
		int sels[2] = { CTL_HW, HW_VECTORUNIT };
		int vType = 0; //0 == scalar only
		size_t length = sizeof(vType);
		int error = sysctl(sels, 2, &vType, &length, NULL, 0);
		if (!error && vType > 0)
			result = kVecAltivec;
	#elif TARGET_CPU_X86_ANY
		int answer = 0;
		size_t length = sizeof(answer);
		int error = sysctlbyname("hw.optional.sse3", &answer, &length, NULL, 0);
		if (!error && answer)
			result = kVecSSE3;
		else {
			answer = 0;
			length = sizeof(answer);
			error = sysctlbyname("hw.optional.sse2", &answer, &length, NULL, 0);
			if (!error && answer)
				result = kVecSSE2;
		}
	#endif
	}
#endif
	sVectorUnitType = result;
	return result;
}

