C ++:仿真固定点除法/乘法 [英] C++: Emulated Fixed Point Division/Multiplication

查看:316
本文介绍了C ++:仿真固定点除法/乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个Fixedpoint类,但是遇到了一个陷阱的位...乘法,除法部分,我不知道如何模拟。我对分部运营商采取了一个非常粗鲁的刺,但我相信它是错误的。以下是它到目前为止的样子:

I'm writing a Fixedpoint class, but have ran into bit of a snag... The multiplication, division portions, I am not sure how to emulate. I took a very rough stab at the division operator but I am sure it's wrong. Here's what it looks like so far:

class Fixed
{
    Fixed(short int _value, short int _part) : 
        value(long(_value + (_part >> 8))), part(long(_part & 0x0000FFFF)) {};

    ...

    inline Fixed operator -() const  // example of some of the bitwise it's doing
    {
        return Fixed(-value - 1, (~part)&0x0000FFFF);
    };

    ...

    inline Fixed operator / (const Fixed & arg) const // example of how I'm probably doing it wrong
    {
        long int tempInt = value<<8 | part;
        long int tempPart = tempInt;
        tempInt  /= arg.value<<8 | arg.part;
        tempPart %= arg.value<<8 | arg.part;
        return Fixed(tempInt, tempPart);
    };

    long int value, part; // members
};

我...不是一个很好的程序员,哈哈!

I... am not a very good programmer, haha!

类的'part'是16位宽(但表示为一个长32位,因为我想象它需要在可能的溢出之前,他们被固定的空间),同样的'value'是整数部分。当其中一个操作的部分超过0xFFFF时,最高的16位被添加到值,然后该部分被屏蔽,因此只剩下最低的16位。

The class's 'part' is 16 bits wide (but expressed as a long 32bit since I imagine it'd need the room for possible overflows before they're fixed) and the same goes for 'value' which is the integer part. When the 'part' goes over 0xFFFF in one of it's operations, the highest 16 bits are added to 'value', and then the part is masked so only it's lowest 16 bits remain. That's done in the init list.

我讨厌问,但是如果有人知道我在哪里可以找到类似这样的文档,甚至只是技巧如何做那两个操作符,我会很高兴的吧!我是一个昏暗的数学,我知道有人不得不这样/问这个以前,但搜索谷歌已经一次没有带我到承诺的土地...

I hate to ask, but if anyone would know where I could find documentation for something like this, or even just the 'trick' or how to do those two operators, I would be very happy for it! I am a dimwit when it comes to math, and I know someone has had to do/ask this before, but searching google has for once not taken me to the promised land...

推荐答案

像Jan说的,使用一个整数。因为它看起来像你指定的16位整数和小数部分,你可以做一个简单的32位整数。

As Jan says, use a single integer. Since it looks like you're specifying 16 bit integer and fractional parts, you could do this with a plain 32 bit integer.

技巧是实现发生了什么到数字的格式,当你做操作它。您的格式将被描述为16.16。当您添加或减少时,格式保持不变。当乘法,你得到32.32 - 所以你需要一个64位的临时值的结果。然后你做一个>> 16位移动到下降到48.16格式,然后采取底部的32位得到你的答案在16.16。

The "trick" is to realise what happens to the "format" of the number when you do operations on it. Your format would be described as 16.16. When you add or subtract, the format stays the same. When you multiply, you get 32.32 -- So you need a 64 bit temporary value for the result. Then you do a >>16 shift to get down to 48.16 format, then take the bottom 32 bits to get your answer in 16.16.

我有点生锈在DSP中,我在哪里学到这个东西,我们尽可能避免(昂贵的)部门!

I'm a little rusty on the division -- In DSP, where I learned this stuff, we avoided (expensive) division wherever possible!

这篇关于C ++:仿真固定点除法/乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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