MIPS bitfield rotation instruction?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

MIPS bitfield rotation instruction?

Post by jsgf »

I just updated the Wiki page talking about swizzling with a description of what swizzling means in terms of bit-level transformations on the offset. It's very simple: swizzling rotates the bits in the field between bit 4 and bit log2(width)+3. http://wiki.ps2dev.org/psp:ge_faq.

This is pretty easy to do in C with some masking and shifting, but I wonder if the PSP's MIPS has some bitfield rotation instructions which can simplify this operation? I seem to remember it has something like this.
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

The ALLEGREX CPU supports MIPS rotr and rotrv. GCC does a pretty good job of detecting a rotate, if you have code like this:

Code: Select all

val = &#40;val >> shift&#41; | &#40;val << 32 - shift&#41;
the compiler will convert that to to the appropiate rotr instruction.
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

Post by jsgf »

Yeah, but it isn't a whole-word rotate. It's a rotate of the range of bits 4-N (N=5-11). It isn't very RISCy, but I was wondering if there were some instruction for doing this kind of rotate, or maybe some bitfield extraction/insertion instructions which might be helpful.

Is there any kind of available instruction set documentation for Allegrex or an Allegrex-like CPU?
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

If you want bitfield extraction and insertion, check out ext and ins. The syntax for ext and ins are:

Code: Select all

ext    rt, rs, pos, size  // Extract size bits from position pos in rs and store in rt
ins    rt, rs, pos, size  // Insert size bits starting from the LSB of rs into rt starting at position pos
The opcode table that our assembler supports was back-engineered from the "official" PSP GCC compiler binaries floating around the net. Your best bet for real docs on ALLEGREX instructions is in various MIPS manuals for CPUs that also include those same instructions.

I do need to get to updating the Wiki with info on the ALLGREX instruction builtins that our GCC supports... here's a short list:

Code: Select all

u32 __builtin_allegrex_bitrev&#40;u32&#41;; // Reverse the bits in the given word
u32 __builtin_allegrex_wsbh&#40;u32&#41;; // Swap halfwords &#40;htons&#41;
u32 __builtin_allegrex_wsbw&#40;u32&#41;; // Swap words &#40;htonl&#41;
u32 __builtin_allegrex_clz&#40;u32&#41;; // Count leading zeros
u32 __builtin_allegrex_clo&#40;u32&#41;; // Count leading ones
u32 __builtin_allegrex_rotr&#40;u32, u32&#41;; // Rotate right
u32 __builtin_allegrex_rotl&#40;u32, u32&#41;; // Rotate left
u32 __builtin_allegrex_seb&#40;u8&#41;; // Sign extend byte
u32 __builtin_allegrex_seh&#40;u16&#41;; // Sign extend halfword
u32 __builtin_allegrex_min&#40;u32, u32&#41;; // Minimum
u32 __builtin_allegrex_max&#40;u32, u32&#41;; // Maximum
...
ext and ins builtins are missing because I haven't figured out how Sony implemented the builtins in their GCC.
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

Post by jsgf »

mrbrown wrote:If you want bitfield extraction and insertion, check out ext and ins. The syntax for ext and ins are:

Code: Select all

ext    rt, rs, pos, size  // Extract size bits from position pos in rs and store in rt
ins    rt, rs, pos, size  // Insert size bits starting from the LSB of rs into rt starting at position pos
Hm, might be useful. What happens if pos+size > 31?
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

jsgf wrote:Hm, might be useful. What happens if pos+size > 31?
No one knows :).
Post Reply