What does script operation "OP_IFDUP" do?

2

According to the wiki page, OP_IFDUP will duplicate the top of the stack "if the input is true or false". It seems to me that everything is true or false when interpreted as a boolean.

According to the source code:

case OP_IFDUP:
    [...]
    valtype vch = stacktop(-1);
    if (CastToBool(vch))
        stack.push_back(vch);

That suggests the wiki is incorrect. What would be a better way of wording it, and what is the operation for?

Chris Moore

Posted 2012-03-19T21:00:46.540

Reputation: 14 317

Answers

2

It duplicates the top stack item if the top stack item is not 0 and not negative 0. I'm not sure where this would be useful.

theymos

Posted 2012-03-19T21:00:46.540

Reputation: 8 416

Thanks. I fixed the wiki accordingly. I copied the description from the OP_IF operation, since the code is the same. – Chris Moore – 2012-03-19T21:46:15.307

1

Another use said there is not a well known use, but I think that OP_IFDUP is super nifty. Adding my response (years later) to share the knowledge in case someone else comes across this post.

Consider the following script:

OP_DUP
OP_0
<2>
OP_WITHIN
OP_VERIFY

OP_IFDUP
OP_NOTIF
... sub program 0
OP_0
OP_ENDIF
OP_1SUB

OP_IFDUP
OP_NOTIF
... sub program 1
OP_0
OP_ENDIF
OP_1SUB

OP_IFDUP
OP_NOTIF
... sub program 2
OP_ENDIF
OP_1

This program emulates a switch!

switch(x) {
case 0:
   // program 0
case 1:
  // program 1
case 2:
 // program 2
default:
  assert(0);
}

But is also slightly more powerful because the last OP_0 per branch can specify if any future branches should also execute (can be used for fall-through behavior, if there is a logical clause common to a group!).

Compare to the more "straightforward" solutions using OP_IF and OP_EQUAL, there's a few more opcodes per branch, and more overall when n = 3. Further the need to push the number on the stack in the code can contribute more bytes for even bigger switches.

OP_DUP
<0>
OP_EQUAL
OP_IF
OP_DROP
... sub program 0
OP_0
OP_ENDIF

OP_DUP
<1>
OP_EQUAL
OP_IF
OP_DROP
... sub program 1
OP_0
OP_ENDIF

OP_DUP
<2>    
OP_EQUAL
OP_IF
OP_DROP
... sub program 2
OP_ENDIF
OP_1

Confused_Coder

Posted 2012-03-19T21:00:46.540

Reputation: 176