view src/impl/ppc/altivec.c @ 23:e26874655738

*: huge refactor, new major release (hahaha) I keep finding things that are broken... The problem NOW was that vec would unintentionally build some functions with extended instruction sets, which is Bad and would mean that for all intents and purposes the CPU detection was completely broken. Now vec is no longer header only either. Boohoo. However this gives a lot more flexibility to vec since we no longer want or need to care about C++ crap. The NEON and Altivec implementations have not been updated which means they won't compile hence why they're commented out in the cmake build file.
author Paper <paper@tflc.us>
date Sun, 24 Nov 2024 02:52:40 -0500
parents
children 92156fe32755
line wrap: on
line source

/**
 * vec - a tiny SIMD vector library in C99
 * 
 * Copyright (c) 2024 Paper
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
**/

#include "vec/impl/ppc/altivec.h"

#include <altivec.h>

/* GCC 4.2.1 on Mac OS X doesn't have these for some reason */
#ifdef vec_mul
# define VEC_ALTIVEC_DEFINE_MUL(sign, csign, bits, size) \
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_mul(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_mul(vec1.altivec, vec2.altivec); \
		return vec; \
	}
# define VEC_ALTIVEC_STRUCT_MUL(sign, csign, bits, size) \
	v##sign##int##bits##x##size##_altivec_mul
#else
# define VEC_ALTIVEC_DEFINE_MUL(sign, csign, bits, size)
# define VEC_ALTIVEC_STRUCT_MUL(sign, csign, bits, size) NULL
#endif

#ifdef vec_splats
# define VEC_ALTIVEC_DEFINE_SPLAT(sign, csign, bits, size) \
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_splat(vec_##sign##int##bits x) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_splats(x); \
		return vec; \
	}
# define VEC_ALTIVEC_STRUCT_SPLAT(sign, csign, bits, size) \
	v##sign##int##bits##x##size##_altivec_splat
#else
# define VEC_ALTIVEC_DEFINE_SPLAT(sign, csign, bits, size)
# define VEC_ALTIVEC_STRUCT_SPLAT(sign, csign, bits, size) NULL
#endif

#define VEC_ALTIVEC_uRSHIFT vec_sr
#define VEC_ALTIVEC_RSHIFT vec_sra

#define VEC_ALTIVEC_DEFINE_uLRSHIFT(sign, csign, bits, size) \
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_lrshift(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_sr(vec1.altivec, vec2.altivec); \
		return vec; \
	}
#define VEC_ALTIVEC_STRUCT_uLRSHIFT(sign, csign, bits, size) \
	v##sign##int##bits##x##size##_altivec_lrshift

#define VEC_ALTIVEC_DEFINE_LRSHIFT(sign, csign, bits, size)
#define VEC_ALTIVEC_STRUCT_LRSHIFT(sign, csign, bits, size) NULL

#define VEC_ALTIVEC_CAST_BOOL_8 (vector signed char)
#define VEC_ALTIVEC_CAST_BOOL_U8 (vector unsigned char)
#define VEC_ALTIVEC_CAST_BOOL_16 (vector signed short)
#define VEC_ALTIVEC_CAST_BOOL_U16 (vector unsigned short)
#define VEC_ALTIVEC_CAST_BOOL_32 (vector signed int)
#define VEC_ALTIVEC_CAST_BOOL_U32 (vector unsigned int)

/* Since altivec conveniently made their API super user friendly, we can just use
 * one giant macro to define literally everything */
#define VEC_DEFINE_OPERATIONS_SIGN(sign, csign, bits, size) \
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_load_aligned(const vec_##sign##int##bits in[size]) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_ld(0, in); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_load(const vec_##sign##int##bits in[size]) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_perm(vec_ld(0, in), vec_ld(15, in), vec_lvsl(0, in)); \
		return vec; \
	} \
	\
	static void v##sign##int##bits##x##size##_altivec_store_aligned(v##sign##int##bits##x##size vec, vec_##sign##int##bits out[size]) \
	{ \
		vec_st(vec.altivec, 0, out); \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_add(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_add(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_sub(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_sub(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	VEC_ALTIVEC_DEFINE_MUL(sign, csign, bits, size) \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_lshift(v##sign##int##bits##x##size vec1, vuint##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_sl(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_rshift(v##sign##int##bits##x##size vec1, vuint##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = VEC_ALTIVEC_##sign##RSHIFT(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	VEC_ALTIVEC_DEFINE_##sign##LRSHIFT(sign, csign, bits, size) \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_avg(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_avg(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_and(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_and(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_or(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_or(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_xor(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = vec_xor(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	VEC_ALTIVEC_DEFINE_SPLAT(sign, csign, bits, size) \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_cmplt(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = VEC_ALTIVEC_CAST_BOOL_##csign##bits  vec_cmplt(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_cmple(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = VEC_ALTIVEC_CAST_BOOL_##csign##bits vec_or(vec_cmplt(vec1.altivec, vec2.altivec), vec_cmpeq(vec1.altivec, vec2.altivec)); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_cmpeq(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = VEC_ALTIVEC_CAST_BOOL_##csign##bits vec_cmpeq(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_cmpge(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = VEC_ALTIVEC_CAST_BOOL_##csign##bits vec_or(vec_cmpgt(vec1.altivec, vec2.altivec), vec_cmpeq(vec1.altivec, vec2.altivec)); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size v##sign##int##bits##x##size##_altivec_cmpgt(v##sign##int##bits##x##size vec1, v##sign##int##bits##x##size vec2) \
	{ \
		v##sign##int##bits##x##size vec; \
		vec.altivec = VEC_ALTIVEC_CAST_BOOL_##csign##bits vec_cmpgt(vec1.altivec, vec2.altivec); \
		return vec; \
	} \
	\
	static v##sign##int##bits##x##size##_impl v##sign##int##bits##x##size##_impl_altivec = { \
		VEC_ALTIVEC_STRUCT_SPLAT(sign, csign, bits, size), \
		v##sign##int##bits##x##size##_altivec_load_aligned, \
		v##sign##int##bits##x##size##_altivec_load, \
		v##sign##int##bits##x##size##_altivec_store_aligned, \
		/* .store = */ NULL, \
		v##sign##int##bits##x##size##_altivec_add, \
		v##sign##int##bits##x##size##_altivec_sub, \
		VEC_ALTIVEC_STRUCT_MUL(sign, csign, bits, size), \
		/* .div = */ NULL, \
		v##sign##int##bits##x##size##_altivec_avg, \
		v##sign##int##bits##x##size##_altivec_and, \
		v##sign##int##bits##x##size##_altivec_or, \
		v##sign##int##bits##x##size##_altivec_xor, \
		/* .not = */ NULL, \
		v##sign##int##bits##x##size##_altivec_lshift, \
		v##sign##int##bits##x##size##_altivec_rshift, \
		VEC_ALTIVEC_STRUCT_##sign##LRSHIFT(sign, csign, bits, size), \
		v##sign##int##bits##x##size##_altivec_cmplt, \
		v##sign##int##bits##x##size##_altivec_cmple, \
		v##sign##int##bits##x##size##_altivec_cmpeq, \
		v##sign##int##bits##x##size##_altivec_cmpge, \
		v##sign##int##bits##x##size##_altivec_cmpgt, \
	};

#define VEC_DEFINE_OPERATIONS(bits, size) \
	VEC_DEFINE_OPERATIONS_SIGN( ,  , bits, size) \
	VEC_DEFINE_OPERATIONS_SIGN(u, U, bits, size)

VEC_DEFINE_OPERATIONS(8, 16)
VEC_DEFINE_OPERATIONS(16, 8)
VEC_DEFINE_OPERATIONS(32, 4)