00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __MISC_HPP__
00021 #define __MISC_HPP__
00022
00023 #include <iostream>
00024 #include <cstdlib>
00025 #include <cstring>
00026
00027 #include <altivec.h>
00028
00029
00030
00031 #ifdef __powerpc__
00032 #ifndef __powerpc64__
00033 #define spu_ea(val) ((uint64_t) ((uint32_t) val))
00034 #else
00035 #define spu_ea(val) ((uint64_t) val)
00036 #endif
00037 #endif
00038
00039 namespace Frz {
00040
00042 struct trans {
00043 float a;
00044 float b;
00045 float c;
00046 float d;
00047 float x;
00048 float y;
00049 float z;
00050 float w;
00051 };
00052
00053
00054 vector float load_unaligned(const float *target);
00055
00056
00057 void store_unaligned(float *target, vector float src);
00058
00059
00060 void make_rotation(vector float rot[4], trans &t);
00061
00067 template <class type>
00068 class alignvec {
00069 type *ptr;
00070 int align;
00071 int size;
00072 int capacity;
00073 bool growable;
00074
00075 public:
00086 alignvec(int _align=128, int _capacity=0) :
00087 ptr(0), align(_align), size(0), capacity(0), growable(true) {
00088 if (_capacity)
00089 setFixedCapacity(_capacity);
00090 }
00092 virtual ~alignvec() {
00093 if (ptr)
00094 free(ptr);
00095 }
00097 void setFixedCapacity(int cap) {
00098 growup(cap);
00099 clear();
00100 growable = false;
00101 }
00107 int growup(int count = 1) {
00108 void *newptr;
00109 int oldsize;
00110 int newsize = size + count;
00111 if (newsize > capacity) {
00112 if (!growable) {
00113 std::cerr << "alignvec::growup() array is not growable (" << newsize
00114 << " > " << capacity << ")" << std::endl;
00115 exit(1);
00116 }
00117 if (capacity == 0)
00118 capacity = 1;
00119 while (newsize > capacity)
00120 capacity *= 2;
00121 if (posix_memalign(&newptr, align, capacity*sizeof(type)) != 0) {
00122 std::cerr << "alignvec::growup() allocation error" << std::endl;
00123 exit(1);
00124 }
00125 if (ptr) {
00126 memcpy(newptr, ptr, size*sizeof(type));
00127 free(ptr);
00128 }
00129 ptr = (type*)newptr;
00130 }
00131 oldsize = size;
00132 size = newsize;
00133 return oldsize;
00134 }
00136 void clear() {
00137 size = 0;
00138 }
00140 int getSize() const {
00141 return size;
00142 }
00144 int getCapacity() const {
00145 return capacity;
00146 }
00148 type & operator[](int i) {
00149 return ptr[i];
00150 }
00152 const type & operator[](int i) const {
00153 return ptr[i];
00154 }
00155 };
00156
00157 };
00158
00159 #endif
00160