Henry MP A,B Multiply & set CC BZ STMT .... STMT EQU *
________________________________ From: Programming <[email protected]> on behalf of Roger Hui <[email protected]> Sent: Monday, July 29, 2019 5:49:28 AM To: [email protected] <[email protected]> Subject: Re: [Jprogramming] A bit-twisting puzzle If you have thought about this for a year then the following can't be the answer, but here it is: There is either a popcnt instruction, or you can implement it with straight-line code without branching. Whence: if !(popcnt(a)&&popcnt(b)) stmt; On Sun, Jul 28, 2019 at 5:51 PM Henry Rich <[email protected]> wrote: > Many of the members of this Forum will remember the days of assembler > language > > ...and who could less than merry be > when writing out BXLE? > > AND, OR, XOR were our meat. Those days are returning. > > You have two integer variables a and b and you want to do something if > one or both of them are 0. In C, you might write > > if(a==0 || b==0)stmt; > > but that will generate the code > > cmp a,0 > bz stmt > cmp b,0 > bnz notstmt > stmt: > ... > notstmt: > > Here's the problem: your machine is very slow at branch instructions. > Each branch takes 30 times as long as a regular instruction. (I am > describing a state-of-the-art Intel CPU when it cannot predict the > branches effectively, perhaps because the data is... unpredictable). > > Obviously, you want to use only one branch instruction. You may use as > many arithmetic and logic instructions as you like, but only one > branch. The usual condition codes, ZNCV, are available. How tight can > you make the code? > > Example: suppose the problem were to execute stmt if one or both of a > and b is NOT zero. Then you would simply write > > or a,b > bnz notstmt > ... > > Checking for zero seems to be harder. > > No hurry. I have pondered this problem for over a year, and just today > I found a solution I consider acceptable. > > Henry Rich > > > > > > > > --- > This email has been checked for viruses by AVG. > https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.avg.com&data=02%7C01%7C%7Cca2848e5d75b40ab6a5408d713e036ef%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636999726027646237&sdata=%2B4IBQj8fG71DkGfMNH%2Bh%2Fvbqb5ZCTWkSOHlPEkkJJ3k%3D&reserved=0 > > ---------------------------------------------------------------------- > For information about J forums see > https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.jsoftware.com%2Fforums.htm&data=02%7C01%7C%7Cca2848e5d75b40ab6a5408d713e036ef%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636999726027646237&sdata=TbPHs06K5bFyFXBTz1NEaYLvz3ldTePocCR2SqPNUws%3D&reserved=0 > ---------------------------------------------------------------------- For information about J forums see https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.jsoftware.com%2Fforums.htm&data=02%7C01%7C%7Cca2848e5d75b40ab6a5408d713e036ef%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636999726027656230&sdata=0XC6WZJuioK380NC9g9YB7PXPQcu7UmFgroNvBuoyCU%3D&reserved=0 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
