25#ifndef _SOURCE_TF_UTIL_H_
26#define _SOURCE_TF_UTIL_H_
28#include "TissueForge_private.h"
30#include <Magnum/Magnum.h>
31#include <Magnum/Math/Color.h>
36#include <unordered_map>
37#include <unordered_set>
41#define _USE_MATH_DEFINES
51 typedef std::mt19937 RandomType;
52 RandomType &randomEngine();
68 enum class PointsType :
unsigned int {
113 const PointsType &kind,
114 const FloatP_t &dr=0,
115 const FloatP_t &phi0=0,
116 const FloatP_t &phi1=M_PI
132 const PointsType &kind,
134 const FloatP_t &dr=0,
135 const FloatP_t &phi0=0,
136 const FloatP_t &phi1=M_PI
148 CPPAPI_FUNC(std::vector<FVector3>)
points(
const PointsType &kind,
const unsigned int &n=1);
163 const unsigned int &nParticlesX=2,
164 const unsigned int &nParticlesY=2,
165 const unsigned int &nParticlesZ=2
189 const int subdivisions,
192 std::vector<FVector3> &verts,
193 std::vector<int32_t> &inds
215 typename std::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
216 almost_equal(T x, T y,
int ulp = 2)
220 return std::fabs(x-y) <= std::numeric_limits<T>::epsilon() * std::fabs(x+y) * ulp
222 || std::fabs(x-y) < std::numeric_limits<T>::min();
227 inline void* aligned_Malloc(
size_t size,
size_t alignment) {
228 return _aligned_malloc(size, alignment);
231 inline void aligned_Free(
void *mem) {
232 return _aligned_free(mem);
237 inline void* aligned_Malloc(
size_t size,
size_t alignment)
240 void_size =
sizeof(
void*)
245 if (alignment < void_size) {
246 alignment = void_size;
249 if (::posix_memalign(&p, alignment, size) != 0) {
255 inline void aligned_Free(
void *mem) {
261 inline void* aligned_Malloc(
size_t size,
size_t alignment) {
262 return aligned_alloc(alignment, size);
265 inline void aligned_Free(
void *mem) {
275 extern const char* color3_Names[];
277 Magnum::Color3 Color3_Parse(
const std::string &str);
290 template<
typename XType,
typename DivType> XType
mod(XType x, DivType divisor)
292 return (divisor + (x%divisor)) % divisor;
306 template<
typename TA,
typename TN>
307 TA flooredDivision(TA a, TN n) {
309 if ((a%n < 0 && n > 0) || (a%n > 0 && n < 0)) --q;
324 template<
typename TA,
typename TN>
325 TA flooredModulo(TA a, TN n) {
326 return a - n * flooredDivision(a, n);
329 template<
typename TA,
typename TN>
330 TA loopIndex(TA index, TN range) {
331 return mod(index + range, range);
338 template<
typename Vec,
typename Val>
340 int result = std::find(vec.begin(), vec.end(), val) - vec.begin();
341 return result < vec.size() ? result : -1;
344 template<
typename ContainerType,
typename SizeType>
345 typename ContainerType::value_type wrappedAt(ContainerType &container, SizeType index) {
346 SizeType wrappedIndex = loopIndex(index, container.size());
347 return container.at(wrappedIndex);
350 template <
typename Type,
typename Klass>
351 inline constexpr size_t offset_of(Type Klass::*member) {
352 constexpr Klass
object {};
353 return size_t(&(
object.*member)) - size_t(&
object);
356 enum InstructionSetFlags : std::int64_t {
358 IS_3DNOWEXT = 1ll << 1,
364 IS_AVX512CD = 1ll << 7,
365 IS_AVX512ER = 1ll << 8,
366 IS_AVX512F = 1ll << 9,
367 IS_AVX512PF = 1ll << 10,
370 IS_CLFSH = 1ll << 13,
371 IS_CMPXCHG16B = 1ll << 14,
376 IS_FSGSBASE = 1ll << 19,
379 IS_INVPCID = 1ll << 23,
381 IS_LZCNT = 1ll << 25,
383 IS_MMXEXT = 1ll << 27,
384 IS_MONITOR = 1ll << 28,
385 IS_MOVBE = 1ll << 28,
387 IS_OSXSAVE = 1ll << 30,
388 IS_PCLMULQDQ = 1ll << 31,
389 IS_POPCNT = 1ll << 32,
390 IS_PREFETCHWT1 = 1ll << 33,
391 IS_RDRAND = 1ll << 34,
392 IS_RDSEED = 1ll << 35,
393 IS_RDTSCP = 1ll << 36,
400 IS_SSE41 = 1ll << 43,
401 IS_SSE42 = 1ll << 44,
402 IS_SSE4a = 1ll << 45,
403 IS_SSSE3 = 1ll << 46,
404 IS_SYSCALL = 1ll << 47,
407 IS_XSAVE = 1ll << 50,
410 #if defined(__x86_64__) || defined(_M_X64)
418 static inline void __tf_cpuid(
int *eax,
int *ebx,
int *ecx,
int *edx)
420 #if defined(__x86_64__)
426 :
"0" (*eax),
"2" (*ecx));
429 int regs[] = {*eax, *ebx, *ecx, *edx};
451 static __declspec(noinline)
int tf_cpuid(
int a[4],
int b)
455 __tf_cpuid(&a[0], &a[1], &a[2], &a[3]);
459 static __declspec(noinline)
int tf_cpuidex(
int a[4],
int b,
int c)
463 __tf_cpuid(&a[0], &a[1], &a[2], &a[3]);
469 static void tf_cpuid(
int a[4],
int b)
473 __tf_cpuid(&a[0], &a[1], &a[2], &a[3]);
476 static void tf_cpuidex(
int a[4],
int b,
int c)
480 __tf_cpuid(&a[0], &a[1], &a[2], &a[3]);
485 class CAPI_EXPORT InstructionSet
490 typedef iVector4 VectorType;
492 class InstructionSet_Internal
495 InstructionSet_Internal();
503 std::bitset<32> f_1_ECX_;
504 std::bitset<32> f_1_EDX_;
505 std::bitset<32> f_7_EBX_;
506 std::bitset<32> f_7_ECX_;
507 std::bitset<32> f_81_ECX_;
508 std::bitset<32> f_81_EDX_;
509 std::vector<VectorType> data_;
510 std::vector<VectorType> extdata_;
513 InstructionSet_Internal CPU_Rep;
518 std::string Vendor(
void);
519 std::string Brand(
void);
521 inline bool SSE3(
void);
522 inline bool PCLMULQDQ(
void);
523 inline bool MONITOR(
void);
524 inline bool SSSE3(
void);
525 inline bool FMA(
void);
526 inline bool CMPXCHG16B(
void);
527 inline bool SSE41(
void);
528 inline bool SSE42(
void);
529 inline bool MOVBE(
void);
530 inline bool POPCNT(
void);
531 inline bool AES(
void);
532 inline bool XSAVE(
void);
533 inline bool OSXSAVE(
void);
534 inline bool AVX(
void);
535 inline bool F16C(
void);
536 inline bool RDRAND(
void);
537 inline bool MSR(
void);
538 inline bool CX8(
void);
539 inline bool SEP(
void);
540 inline bool CMOV(
void);
541 inline bool CLFSH(
void);
542 inline bool MMX(
void);
543 inline bool FXSR(
void);
544 inline bool SSE(
void);
545 inline bool SSE2(
void);
546 inline bool FSGSBASE(
void);
547 inline bool BMI1(
void);
548 inline bool HLE(
void);
549 inline bool AVX2(
void);
550 inline bool BMI2(
void);
551 inline bool ERMS(
void);
552 inline bool INVPCID(
void);
553 inline bool RTM(
void);
554 inline bool AVX512F(
void);
555 inline bool RDSEED(
void);
556 inline bool ADX(
void);
557 inline bool AVX512PF(
void);
558 inline bool AVX512ER(
void);
559 inline bool AVX512CD(
void);
560 inline bool SHA(
void);
561 inline bool PREFETCHWT1(
void);
562 inline bool LAHF(
void);
563 inline bool LZCNT(
void);
564 inline bool ABM(
void);
565 inline bool SSE4a(
void);
566 inline bool XOP(
void);
567 inline bool TBM(
void);
568 inline bool SYSCALL(
void);
569 inline bool MMXEXT(
void);
570 inline bool RDTSCP(
void);
571 inline bool _3DNOWEXT(
void);
572 inline bool _3DNOW(
void);
574 std::unordered_map<std::string, bool> featuresMap;
579 CPPAPI_FUNC(std::unordered_map<std::string, bool>) getFeaturesMap();
583 CPPAPI_FUNC(std::unordered_map<std::string, bool>) getFeaturesMap();
589 std::unordered_map<std::string, unsigned int> flags;
590 std::list<std::string> flagNames;
597 const std::list<std::string> getFlags();
598 const int getFlag(
const std::string &_flag);
603 CPPAPI_FUNC(
double) wallTime();
605 CPPAPI_FUNC(
double) CPUTime();
623 CPPAPI_FUNC(uint64_t) nextPrime(
const uint64_t &start_prime);
632 CPPAPI_FUNC(std::vector<uint64_t>)
findPrimes(
const uint64_t &start_prime,
int n);
636 FloatP_t (*func)(FloatP_t);
637 FloatP_t xmin, xmax, inc_cf = 1e-3;
639 Differentiator(FloatP_t (*f)(FloatP_t),
const FloatP_t &xmin,
const FloatP_t &xmax,
const FloatP_t &inc_cf=1e-3);
641 FloatP_t fnp(
const FloatP_t &x,
const unsigned int &order=0);
642 FloatP_t operator() (
const FloatP_t &x);
653 template <
typename T>
654 std::vector<T>
unique(
const std::vector<T> &vec) {
655 std::unordered_set<T> result_us(vec.begin(), vec.end());
656 return std::vector<T>(result_us.begin(), result_us.end());
Definition tfVector4.h:33
std::vector< T > unique(const std::vector< T > &vec)
Get the unique elements of a vector.
Definition tf_util.h:654
std::vector< uint64_t > findPrimes(const uint64_t &start_prime, int n)
Get prime numbers, beginning with a starting prime number.
XType mod(XType x, DivType divisor)
Definition tf_util.h:290
int indexOf(const Vec &vec, const Val &val)
Definition tf_util.h:339
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode.
Definition tfAngleConfig.h:26
FVector3 randomVector(FloatP_t mean, FloatP_t std)
Generates a randomly oriented vector with random magnitude with given mean and standard deviation acc...
std::vector< FVector3 > points(const PointsType &kind, const unsigned int &n=1)
Get the coordinates of uniform points in a kind of shape.
std::vector< FVector3 > filledCubeRandom(const FVector3 &corner1, const FVector3 &corner2, const int &nParticles)
Get the coordinates of a randomly filled cube.
FVector4 planeEquation(const FVector3 &normal, const FVector3 &point)
Get the coefficients of a plane equation for a normal vector and point.
HRESULT icosphere(const int subdivisions, FloatP_t phi0, FloatP_t phi1, std::vector< FVector3 > &verts, std::vector< int32_t > &inds)
Get the coordinates of an icosphere.
std::vector< FVector3 > randomPoints(const PointsType &kind, const int &n=1, const FloatP_t &dr=0, const FloatP_t &phi0=0, const FloatP_t &phi1=M_PI)
Get the coordinates of random points in a kind of shape.
std::vector< FVector3 > filledCubeUniform(const FVector3 &corner1, const FVector3 &corner2, const unsigned int &nParticlesX=2, const unsigned int &nParticlesY=2, const unsigned int &nParticlesZ=2)
Get the coordinates of a uniformly filled cube.
std::vector< std::string > color3Names()
Get the names of all available colors.
HRESULT setSeed(const unsigned int *_seed=0)
Set the current seed for the pseudo-random number generator.
FVector3 randomUnitVector()
Generates a randomly oriented unit vector.
unsigned int getSeed()
Get the current seed for the pseudo-random number generator.
FVector3 randomPoint(const PointsType &kind, const FloatP_t &dr=0, const FloatP_t &phi0=0, const FloatP_t &phi1=M_PI)
Get the coordinates of a random point in a kind of shape.
int32_t HRESULT
Definition tf_port.h:255