Live data from Hacker News

Finding the average of two unsigned integers without overflow

devblogs.microsoft.com

211–220 of 220 posts

Re: Finding the average of two unsigned integers without overflow

#211
post #152

> gcc doesn’t have a rotation intrinsic, so I couldn’t try it there Gcc and Clang both recognize the pattern of shifts and OR that reproduce a rotation, and substitute the actual instruction, no intrinsic needed. I bet MSVC does too.

They recognize how to do a rotation of an unsigned integer value, but they do not recognize how to do the rotation of that value concatenated with the carry bit, which is needed here.

Re: Finding the average of two unsigned integers without overflow

#213

Earlier quoted context omitted.

The obvious form of the code with a comparison still produces a conditional branch on latest gcc [1]. It's extremely doubtful that you'll find a version that uses any comparison that consistently performs as quickly as any version that uses a little bit-twiddling, no matter what modern CPU you're talking about. Many of your statements are misleading in context. Implying that you can't know or deduce things about the…

The code generated by gcc in this case is just bad. It is known that gcc fails to do "if conversion" in many cases when it should. The correct code using the CMOV instruction and only a single computation of the expression could easily be written with inline assembly. However, if inline assembly is used, there is a much simpler solution using the carry flag, which was presented at the end of the article that started…

And I'll reiterate the original point, the pointer-operations (with comparison) solution is not going to be the best in most any case; you admit that it's not the fastest for inline assembly, which you claim is the right solution. It's not going to vary from CPU to CPU - comparisons in most compilers will become branches and cause a stall and the best case (cmov) is still going to be slower than a shift-from-carry or the (a&b)+(a^b)/2 version.

We don't need to admit defeat in optimizing such a simple case. A comparison is unnecessary and will pessimize, compared to a little bit-manipulation.

Re: Finding the average of two unsigned integers without overflow

#214

Earlier quoted context omitted.

This thread is full of people who challenged themselves to solve it and then failed to come up with the 'obvious' 1-cycle solution. It's clearly non-obvious, as this thread shows. The actual patent system failure here is the patent is not useful -- it's not valuable. If you needed this solution, you could sit down and derive it in less than an hour. That's not because it's obvious, but because the scope is so small.…

> This thread is full of people who challenged themselves to solve it and then failed to come up with the 'obvious' 1-cycle solution. It's clearly non-obvious, as this thread shows. If a significant fraction of people come up with it on the spot, it's obvious. And they did.

I don't even see a single comment mentioning doing this in 1 cycle except from those who read the patent, much less reusing existing functional units to do so, so it's not clear to me any commenter came up with an equivalent to the patented solution or even identified the problem solved by it.

Keep in mind this solution was to support MPEG-1 video encoding in the olden days when state of the art processors were 100 MHz and 800 nm process. Doing this in 1 cycle while reusing already existing function units seems like a clever solution to me -- not patent-worthy, not difficult, but clever.

Re: Finding the average of two unsigned integers without overflow

#215

Earlier quoted context omitted.

> This thread is full of people who challenged themselves to solve it and then failed to come up with the 'obvious' 1-cycle solution. It's clearly non-obvious, as this thread shows. If a significant fraction of people come up with it on the spot, it's obvious. And they did.

I don't even see a single comment mentioning doing this in 1 cycle except from those who read the patent, much less reusing existing functional units to do so, so it's not clear to me any commenter came up with an equivalent to the patented solution or even identified the problem solved by it. Keep in mind this solution was to support MPEG-1 video encoding in the olden days when state of the art processors were 100 M…

Are you very sure that patent would never get threatened toward a software implementation that doesn't know anything about cycles?

If so then the technique in the post isn't actually patented.

If that C code would get threatened, then the 1 cycle thing is a red herring.

Also "Doing this in 1 cycle while reusing already existing function units"? In hardware you can use a normal adder without any special technique...

Re: Finding the average of two unsigned integers without overflow

#216

Earlier quoted context omitted.

I don't even see a single comment mentioning doing this in 1 cycle except from those who read the patent, much less reusing existing functional units to do so, so it's not clear to me any commenter came up with an equivalent to the patented solution or even identified the problem solved by it. Keep in mind this solution was to support MPEG-1 video encoding in the olden days when state of the art processors were 100 M…

Are you very sure that patent would never get threatened toward a software implementation that doesn't know anything about cycles? If so then the technique in the post isn't actually patented. If that C code would get threatened, then the 1 cycle thing is a red herring. Also "Doing this in 1 cycle while reusing already existing function units"? In hardware you can use a normal adder without any special technique...

I've already said twice now that it's not patent-worthy, so it seems we're in agreement on that point.

Re: Finding the average of two unsigned integers without overflow

#217

Earlier quoted context omitted.

It's not the solution that's patented, but it's the implementation in a single CPU cycle in hardware.

So the compiler developers had to pay fees? Or Intel and AMD?

If the compiler developers were developing their own circuits, yes.

But likely, Intel/AMD would have had to pay fees if they were implementing a similar solution in their hardware.

Re: Finding the average of two unsigned integers without overflow

#218

Earlier quoted context omitted.

Are you very sure that patent would never get threatened toward a software implementation that doesn't know anything about cycles? If so then the technique in the post isn't actually patented. If that C code would get threatened, then the 1 cycle thing is a red herring. Also "Doing this in 1 cycle while reusing already existing function units"? In hardware you can use a normal adder without any special technique...

I've already said twice now that it's not patent-worthy, so it seems we're in agreement on that point.

That's a response to you calling it clever.

Re: Finding the average of two unsigned integers without overflow

#219

Having done computer architecture and bit twiddling x86 in the ye olden days, I immediately, independently converged on the patented solution (code / circuit / Verilog, more or less the same thing). It goes to show how broken the USPTO is because it's obvious to anyone in the field. Patents are supposed to be nonobvious. (35 USC 103) https://patentdefenses.klarquist.com/obviousness-sec-103/

This is bizarre. I wonder how many of us saw that title, thought "That's a really simple problem, surely?" came up with a solution and then were shocked when their coffee-lacking brain actually came up with the patented solution? I mean... ignoring the bitwise arithmentic (which this only obvious to people used to doing binary operations) this is the kind of maths that an 11yo could do. That said, the patented soluti…

We should end legal protection of ideas as soon as possible.

Re: Finding the average of two unsigned integers without overflow

#220

Having done computer architecture and bit twiddling x86 in the ye olden days, I immediately, independently converged on the patented solution (code / circuit / Verilog, more or less the same thing). It goes to show how broken the USPTO is because it's obvious to anyone in the field. Patents are supposed to be nonobvious. (35 USC 103) https://patentdefenses.klarquist.com/obviousness-sec-103/

What is obvious today might not have been obvious in 1996. Our experiences and training has changed dramatically over the past 26 years.

I can assure you I would have come up with the patented solution just as fast in 1996 when I was a teenager and dabbled in 6502 assembler on Atari computer. Because I solved it now on the basis of exactly the expeirience and knowledge I acquired back then.
Post reply on HN