ReaperUnreal
ReaperUnreal

Reputation: 990

Returning a pair of items from a function

I know all the normal ways of doing this, what I'm looking for is an article I read a long long time ago that showed how to write some crazy helper functions using inline assembly and __naked functions to return a pair of items.

I've tried googling for it endlessly, but to no avail, I'm hoping someone else knows the article I'm talking about and has a link to it.

Upvotes: 4

Views: 951

Answers (2)

nategoose
nategoose

Reputation: 12392

The __naked that I know of is

#define __naked __attribute__((naked))

Which is a GCC attribute that is documented here.

It is not supported on all targets. Doing a google code search will turn up some uses of it, though.

The reasons that it is done as a macro is so that it can be defined as empty for targets that don't support it or if the headers are being used with some other compiler.

I do remember (I think) seeing some examples of this for the division and modulation (divmod) functions for avr_gcc headers. This returned a struct that had both return values in it, but stored the whole thing in registers rather than on the stack.

I don't know if __naked had anything to do with the ability to return both parts of the result (which were in a struct) in registers (rather than on the stack), but it did allow the inline function to consist entirely of a call to one helper function.

Upvotes: -1

pmg
pmg

Reputation: 108938

No assembly necessary

struct PairOfItems {int item1;double item2;};
struct PairOfItems crazyhelperfunction(void){
    struct PairOfItems x = {42, -0.42};
    return x;
}

I don't know about the article.

Upvotes: 11

Related Questions