//----------------------------------------------------------------------
// Copyright (C) 2006-2007 Electronic Arts
//----------------------------------------------------------------------

#include "job_util/batch_desc.h"

namespace EA {
    namespace Jobs {

//----------------------------------------------------------------------
BatchDesc::BatchDesc() :
    ForEachDesc(sizeof(BatchDesc), sBatchDescIdent),
    mNumElements(0),
    mElementsPerBatch(0),
    mElementMultiplePerBatch(1)
{}

//----------------------------------------------------------------------
BatchDesc::BatchDesc(size_t numElements) :
    ForEachDesc(sizeof(BatchDesc), sBatchDescIdent),
    mNumElements(static_cast<int32_t>(numElements)),
    mElementsPerBatch(0),
    mElementMultiplePerBatch(1)
{}

//----------------------------------------------------------------------
BatchDesc::BatchDesc(int numElements) :
    ForEachDesc(sizeof(BatchDesc), sBatchDescIdent),
    mNumElements(static_cast<int32_t>(numElements)),
    mElementsPerBatch(0),
    mElementMultiplePerBatch(1)
{}

//----------------------------------------------------------------------
int BatchDesc::NumElements() const
{
    return mNumElements;
}

//----------------------------------------------------------------------
void BatchDesc::SetElementsPerBatch(int batchSize)
{
    mElementsPerBatch = static_cast<int32_t>(batchSize);
}

//----------------------------------------------------------------------
void BatchDesc::SetElementMultiplePerBatch(int elementMultiple)
{
    mElementMultiplePerBatch = static_cast<int32_t>(elementMultiple);
}

//----------------------------------------------------------------------
int BatchDesc::GetElementMultiplePerBatch() const
{
    return static_cast<int>(mElementMultiplePerBatch);
}

//----------------------------------------------------------------------
int BatchDesc::GetElementsPerBatch() const
{
    return static_cast<int>(mElementsPerBatch);
}

//----------------------------------------------------------------------
void BatchDesc::SetArray(int index, void const * inPtr, void * outPtr, int sizeOfEachElement)
{
    EA_ASSERT(index >= 0 && index < sMaxArrays);

    if (!inPtr && !outPtr)
    {
        mArray[index].mInPtr = 0;
        mArray[index].mOutPtr = 0;
        mArray[index].mElementSize = 0;
        return;
    }

    EA_ASSERT(0 == ((sizeOfEachElement * mNumElements) % 16));
    EA_ASSERT(0 == ((uintptr_t)inPtr % 16));
    EA_ASSERT(0 == ((uintptr_t)outPtr % 16));

    if (inPtr)
        mArray[index].mInPtr = reinterpret_cast<char const *>(inPtr);

    if (outPtr)
        mArray[index].mOutPtr = reinterpret_cast<char *>(outPtr);

    mArray[index].mElementSize = sizeOfEachElement;
}



//----------------------------------------------------------------------
BatchDesc::ArrayDesc & BatchDesc::GetArrayDesc(int index)
{
    EA_ASSERT(index >= 0 && index < sMaxArrays);
    return mArray[index];
}

bool BatchDesc::Verify() const
{
    return ForEachDesc::Verify(sizeof(BatchDesc), sBatchDescIdent);
}

}}
