zoukankan      html  css  js  c++  java
  • Fixed-point multiplication (C166 A*B/B)

    I want to multiply two fixed point numbers. After the multiplication I have to shift the result so that the binary point is correct.
    Example:
    int a;
    int b;
    int c;
    c = (a * b) >> 10
    

    The multiplication a*b produces a long int value in the MD register. After the shift operation the int result (the lower byte) should be stored in c.
    My problem is how to use then 32-bit MD register. When I look at the assembler code only the lower byte of the MD register is used for the shift operation, but I first I want to shift the whole register and then take only the lower byte.
    How can I implement that in C? Who knows an application note about fixed-point arithmetic with the C166?
     
    Read-Only
    Author
    Andrew Neil
    Posted
    22-Oct-2003 09:34 GMT
    Toolset
    C166
    New! RE: Fixed-point multiplication
    I don't know the '166, but I suspect this is a 'C' issue rather than a processor issue.

    You could look up the promotion rules in K&R, or you could just try experimenting with casting a, b, and/or the product to long.
     
    Read-Only
    Author
    Mike Kleshov
    Posted
    22-Oct-2003 09:56 GMT
    Toolset
    C166
    New! RE: Fixed-point multiplication
    As Andrew said, this is a C issue. Take your favourite book on C and read about types and expressions. There are a few pitfalls there.
    If both operands a and b are of type int, then the product (a * b) will be of type int, which is not what you are expecting. If you want the result to be of type long, try ((long)a * b): it will suffice to cast one of the operands to long.
    Make sure you take care of overflows and that you understand the differences between signed and unsigned arithmetics.

    - mike
     
    Read-Only
    Author
    Bruno Büsser
    Posted
    22-Oct-2003 10:45 GMT
    Toolset
    C166
    New! RE: Fixed-point multiplication
    That's it!
    The following line produces just what I wanted:
    c = ((long)a * b)>>10;

    The product a*b is stored as 32 bit value in MD register, then the MD register value is arithmetic shifted right by 10 and the lower 16 bits stored in c.
    Thanks to Andrew and Mike!

    http://www.keil.com/forum/3549/

  • 相关阅读:
    免费素材下载:淡蓝色的PSD格式UI套件
    分享一个CSS3的网格系统架构 ResponsiveAeon
    最新收集的超棒Mobile/Web UI和用户体验设计
    一个帮助你针对不同标签自动填入内容的轻量级javascript类库 fixiejs
    发现任何VB函数、插件、甚至按键精灵对“文件下载”窗口后台失效
    android 界面 滑入 效果
    分布式HeadLoop
    VB ListView
    android 下载保存图片
    网址
  • 原文地址:https://www.cnblogs.com/xihong2014/p/10092267.html
Copyright © 2011-2022 走看看