/*
 * Copyright (c) 2021, Jeffrey Lee
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef SCALAR_H
#define SCALAR_H

#include "genmath.h"
#include "vector.h"

namespace scalar {

typedef vector<float,2> vec2f;
typedef vector<float,3> vec3f;
typedef vector<float,4> vec4f;
typedef vector<int,2> vec2i;
typedef vector<int,3> vec3i;
typedef vector<int,4> vec4i;
typedef vector<unsigned int,2> vec2u;
typedef vector<unsigned int,3> vec3u;
typedef vector<unsigned int,4> vec4u;
typedef float vec1f;
typedef int vec1i;
typedef unsigned int vec1u;

const int scalar_elem = 1;

inline vec1f vec1f_spread(float f,float step)
{
	return f;
}

// Should be handled by genmath
//inline vec1f recp(vec1f f)
//{
//	return 1/f;
//}
//
//inline vec1f inversesqrt(vec1f f)
//{
//	return 1/SQRT(f);
//}

inline vec1i revelem(vec1i i)
{
	return i;
}

inline vec1i rgb(vec3f col)
{
	vec1i col3 = 0;
	col = saturate(col);
	for(int i=2;i>=0;i--)
	{
		vec1i col2 = vec1i(col[i]*255.0f);
		col3 += col2 * (1<<((2-i) << 3));
	}
	return col3;
}

inline vec1i rgb_fast(vec3f col)
{
	vec1i col3 = 0;
	for(int i=2;i>=0;i--)
	{
		vec1i col2 = vec1i(col[i]*255.0f);
		col3 += col2 * (1<<((2-i) << 3));
	}
	return col3;
}

inline vec1u revelem(vec1u i)
{
	return i;
}

template<typename T>
static inline T select(bool mask,T a,T b)
{
	return (mask ? a : b);
}

} /* namespace scalar */

#endif

