gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Why doesn't C/C++ have...

#17373 - LOst? - Sun Mar 07, 2004 12:15 am

Why doesn't C/C++ have a rotate operator? I love the ROR, ROL instructions that most assembly languages have, but C/C++ only have the operator>> and operator<<.
C/C++ even have the typical assembly "NOT" instruction as operator~.

Why doesn't it have rotate operators?

#17374 - tepples - Sun Mar 07, 2004 12:47 am

The compiler will optimize (a << 20) | (a >> 12) into the assembly code that represents (a ROL 20). You're free to make a static inline function for that.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#17376 - LOst? - Sun Mar 07, 2004 1:03 am

tepples wrote:
The compiler will optimize (a << 20) | (a >> 12) into the assembly code that represents (a ROL 20). You're free to make a static inline function for that.


How can a shift become a rol? With a static inline function like?

#17378 - tepples - Sun Mar 07, 2004 2:02 am

Bit-rotation is equivalent to the OR of two shifts, and the compiler should recognize it if you do it right.

You can learn what inline functions are in any relatively recent book about C.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#17380 - LOst? - Sun Mar 07, 2004 2:25 am

tepples wrote:
Bit-rotation is equivalent to the OR of two shifts, and the compiler should recognize it if you do it right.

You can learn what inline functions are in any relatively recent book about C.


Oh, thanks for the info! ^_^

#17407 - Paul Shirley - Sun Mar 07, 2004 2:56 pm

removed

Last edited by Paul Shirley on Sun Mar 28, 2004 9:03 pm; edited 1 time in total

#17425 - LOst? - Sun Mar 07, 2004 8:56 pm

Paul Shirley wrote:
tepples wrote:
The compiler will optimize (a << 20) | (a >> 12) into the assembly code that represents (a ROL 20). You're free to make a static inline function for that.

Beware: that only works for unsigned values. Its essential you code this as an inline function(s) with unsigned parameter or it will trip you up later.


Thanks for the info. I will take that in mind

#17439 - maleu - Mon Mar 08, 2004 5:43 am

yes, but that doesn't let you use this nice little construct:

ror r0, #2
bne 0f // jump if bit 0 of r0 is set
bcs 1f // jump if bit 1 of r0 is set
// fall trough otherwise

very useful e.g. for keypad input. no wat to do this in c, right?

other question: how to call bios functions in c (gcc) without jumping to a dummy function that contains nothing but the swi?

#17451 - Paul Shirley - Mon Mar 08, 2004 4:23 pm

removed

Last edited by Paul Shirley on Sun Mar 28, 2004 9:03 pm; edited 1 time in total

#17455 - tepples - Mon Mar 08, 2004 5:41 pm

Wait for C--, a cross between C and assembly language that abstracts common features of 32-bit CPUs (such as a carry flag) to make it useful as a compiler's code generator.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#17456 - Paul Shirley - Mon Mar 08, 2004 6:35 pm

removed

Last edited by Paul Shirley on Sun Mar 28, 2004 9:03 pm; edited 1 time in total

#17458 - Miked0801 - Mon Mar 08, 2004 8:06 pm

What no Carry? Arghg, I'm melting! All those years of GBC porgramming with only C and Z. Wasted, wasted ;)