OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 1:42 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Ternary move instructions
PostPosted: Sat Mar 11, 2023 4:06 am 
Offline
Member
Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 509
Is anyone aware of any ISA that implements the ternary conditional operator as an instruction? Basically, this would be a three operand instruction:

MOVcc x, y, z

That tests some condition, and moves x to z if the condition is true, and y to z if the condition is false?

Of course, conditional branches could be regarded as a special case of this:

jne offset

could be regarded as equivalent to something like the following in an ISA with a ternary move:

movne IP+offset, IP, IP

But I'm not aware of any ISA that has a more general implementation of this concept.


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Sat Mar 11, 2023 6:38 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
So my first thought was "if any, it would be ARM". Not quite, but ARM has a two-operand move instruction and allows you to put conditions on all instructions, so you could
Code:
MOVScc Rx, Ry
MOVSncc Rx, Rz
where cc and ncc are opposite conditions.

Come to think of it, you could do the same with CMOV in x86 as well.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Sat Mar 11, 2023 3:13 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
Do AVX V(P)BLEND instructions count?


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Sat Mar 11, 2023 9:59 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
The traditional MIPS ISA has a few conditional moves:
  • MOVN rd, rs, rt # if (rt != 0) rd = rs;
  • MOVZ rd, rs, rt # if (rt == 0) rd = rs;
  • MOVN.fmt fd, fs, rt # if (rt != 0) fd = fs;
  • MOVZ.fmt fd, fs, rt # if (rt == 0) fd = fs;
  • MOVT.fmt fd, fs, cc # if (FCC[cc] != 0) fd = fs;
  • MOVF.fmt fd, fs, cc # if (FCC[cc] == 0) fd = fs;

EDIT to add:
MIPS Release 6 has these instead:
  • SELNEZ rd, rs, rt # rd = rt ? rs : 0;
  • SELEQZ rd, rs, rt # rd = rt ? 0 : rs;
  • SEL.fmt fd, fs, ft # fd = fd.bit0 ? ft : fs;


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Sun Mar 12, 2023 6:51 pm 
Offline

Joined: Fri Mar 03, 2023 11:34 pm
Posts: 2
ARMv8 A64's CSEL does pretty much exactly that:

CSEL Rdest, Rtrue, Rfalse, cond


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Mon Mar 13, 2023 12:43 am 
Offline
Member
Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 509
nullplan wrote:
So my first thought was "if any, it would be ARM". Not quite, but ARM has a two-operand move instruction and allows you to put conditions on all instructions, so you could
Code:
MOVScc Rx, Ry
MOVSncc Rx, Rz
where cc and ncc are opposite conditions.

Come to think of it, you could do the same with CMOV in x86 as well.


I'm specifically looking for ISAs that have a single instruction directly equivalent to the ternary operator. Mostly just out of curiousity.


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Mon Mar 13, 2023 12:45 am 
Offline
Member
Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 509
Octocontrabass wrote:
Do AVX V(P)BLEND instructions count?


They are very much (in a bitwise fashion) in the same spirit as the type of instruction I'm looking for, but what I'm specifically looking for is instructions that test a one-bit condition and move one of two values to a destination based on that condition.


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Mon Mar 13, 2023 12:52 am 
Offline
Member
Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 509
alexfru wrote:
The traditional MIPS ISA has a few conditional moves:
  • MOVN rd, rs, rt # if (rt != 0) rd = rs;
  • MOVZ rd, rs, rt # if (rt == 0) rd = rs;
  • MOVN.fmt fd, fs, rt # if (rt != 0) fd = fs;
  • MOVZ.fmt fd, fs, rt # if (rt == 0) fd = fs;
  • MOVT.fmt fd, fs, cc # if (FCC[cc] != 0) fd = fs;
  • MOVF.fmt fd, fs, cc # if (FCC[cc] == 0) fd = fs;

EDIT to add:
MIPS Release 6 has these instead:
  • SELNEZ rd, rs, rt # rd = rt ? rs : 0;
  • SELEQZ rd, rs, rt # rd = rt ? 0 : rs;
  • SEL.fmt fd, fs, ft # fd = fd.bit0 ? ft : fs;


SEL.fmt appears to be exactly the kind of instruction I'm looking for.


Top
 Profile  
 
 Post subject: Re: Ternary move instructions
PostPosted: Mon Mar 13, 2023 11:03 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
linguofreak wrote:
I'm specifically looking for ISAs that have a single instruction directly equivalent to the ternary operator. Mostly just out of curiousity.

Well, the only other thing I remember that sort-of fits is PowerPC's fsel instruction
Code:
fsel fd,ft,fa,fb
which does:
fd = ft >= 0? fa : fb
But that is floating-point only, and an optional instruction.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 35 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group