[ Home | Programming Tips | Mail ]

Rotate on PPC


PowerPCでの Rotate命令

 C / C++には Shift operator(>> <<)はありますが Rotate operatorがありませんので、Data圧縮アルゴリズムや Pixel操作等で Rotateを駆使したい時解りにくい表記になったり、遅くなってしまったりします。
 CodeWarrior C++ Compilerで用意されている Intrinsic functionを使うと PowerPCの Rotate命令を直接使って簡単かつ高速な Codeが書けます。

#include    <stdio.h>
 
inline unsigned long    RotateLeft(unsigned long word,long nbits)
{
    return __rlwinm(word,nbits,0,31);
}
 
inline unsigned long    RotateRight(unsigned long word,long nbits)
{
    return __rlwinm(word,32-nbits,0,31);
}
 
void    main()
{
unsigned long   tmp = 0x12345678;
 
    printf("%08X %08X\n",RotateLeft(tmp,8),RotateRight(tmp,8));
}


この Pageは MacOS X + Radio UserLand で作っています。