在游戏引擎数学库中使用SIMD通过使用函数指针〜一个好主意? [英] Using SIMD in a Game Engine Math Library by using function pointers ~ A good idea?

查看:224
本文介绍了在游戏引擎数学库中使用SIMD通过使用函数指针〜一个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从14岁以来,我一直在阅读游戏引擎图书(当时我不明白一件事情:P)
现在几年后,我想开始为我的游戏引擎编写数学基础。我一直在想如何设计这个图书馆。 (我的意思是有组织的文件集)每隔几年新的SIMD说明书出来,我不想让他们浪费。 (告诉我,如果我错了这个。)



我想要至少有以下属性:




  • 使它能够检查它是否在运行时有SIMD,如果它有它使用SIMD,如果没有,使用正常的C ++版本。 (可能有一些调用开销,这是值得吗?)

  • 如果我们已经知道编译时的目标,它能够编译SIMD或普通C ++。调用可以内联并适合交叉优化,因为编译器知道是否使用SIMD或C ++。



编辑 - 我想让源代码可移植,所以它可以运行在其他deviced,然后x86(-64)太

$



所以我认为这将是一个很好的解决方案使用函数指针,我将使它静态和初始化在程序的开始。和其中的适合的函数(例如乘法Matrix / Vector)将调用。



你认为这个设计的优点和缺点

解决方案



重要的是要获得正确的粒度,在这个粒度上你可以决定调用哪个例程。如果你这样做的太低的水平,然后功能调度开销成为一个问题,例如。一个只有几条指令的小程序如果通过某种函数指针调度机制调用而不是简单地内联就会变得非常低效。理想情况下,特定于架构的例程应该处理合理数量的数据,以便功能分配成本可以忽略不计,而不会因为为每个受支持的体系结构编译额外的非体系结构特定的代码而导致重大的代码膨胀。


I have been reading Game Engine Books since I was 14 (At that time I didn't understand a thing:P) Now quite some years later I wanted to start programming the Mathmatical Basis for my Game Engine. I've been thinking long about how to design this 'library'. (Which I mean as "Organized set of files") Every few years new SIMD instructionsets come out, and I wouldn't want them to go to waste. (Tell me if I am wrong about this.)

I wanted to at least have the following properties:

  • Making it able to check if it has SIMD at runtime, and use SIMD if it has it and uses the normal C++ version if it doesn't. (Might have some call overhead, is this worth it?)
  • Making it able to compile for SIMD or normal C++ if we already know the target at compile time. The calls can get inlined and made suitable for Cross-Optimisation because the compiler knows if SIMD or C++ is used.

EDIT - I want to make the sourcecode portable so it can run on other deviced then x86(-64) too

So I thought it would be a good solution to use function pointers which I would make static and initialize at the start of the program. And which the suited functions(For example multiplication of Matrix/Vector) will call.

What do you think are the advantages and disadvantages(Which outweights more?) of this design and is it even possible to create it with both properties as described above?

Christian

解决方案

It's important to get the right granularity at which you make decision on which routine to call. If you do this at too low a level then function dispatch overhead becomes a problem, e.g. a small routine which just has a few instructions could become very inefficient if called via some kind of function pointer dispatch mechanism rather than say just being inlined. Ideally the architecture-specific routines should be processing a reasonable amount of data so that function dispatch cost is negligible, without being so large that you get significant code bloat due to compiling additional non-architecture-specific code for each supported architecture.

这篇关于在游戏引擎数学库中使用SIMD通过使用函数指针〜一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆