[HN Gopher] AVX Bitwise ternary logic instruction busted
       ___________________________________________________________________
        
       AVX Bitwise ternary logic instruction busted
        
       Author : msephton
       Score  : 131 points
       Date   : 2024-10-06 18:38 UTC (4 hours ago)
        
 (HTM) web link (arnaud-carre.github.io)
 (TXT) w3m dump (arnaud-carre.github.io)
        
       | Sniffnoy wrote:
       | Oh, I thought the title was saying that the instruction doesn't
       | work properly! (The article actually just explains how it works.)
        
         | SomeHacker44 wrote:
         | Agreed on initial interpretation. Terrible title!
        
           | foota wrote:
           | Amusingly, I had a third interpretation, which is "busted" as
           | being too strong. I realized that when the author started
           | talking about the Amiga though that's probably not what they
           | meant (as busted is a fairly modern gaming term, I'd be
           | surprised to see someone as old as to be familiar with Amiga
           | to use it. Sorry to anyone that feels personally attacked by
           | this description :P)
        
             | thfuran wrote:
             | Is broken not cool enough anymore?
        
       | 486sx33 wrote:
       | it's fundamentally just a lookup table
        
       | kens wrote:
       | I'll point out that this is the same way that FPGAs implement
       | arbitrary logic functions, as lookup tables (LUTs).
        
         | tr352 wrote:
         | So does the 74181 ALU.
        
           | monocasa wrote:
           | I don't think the 74181 is implemented with a LUT.
           | 
           | http://www.righto.com/2017/01/die-photos-and-reverse-
           | enginee...
        
         | kevin_thibedeau wrote:
         | Most but not all. Actel/Microsemi use a small tree of muxes and
         | gates.
        
       | Lerc wrote:
       | My teenage self did not write "CRAP!" on that page of the
       | hardware manual, but I stared at it for so long trying to figure
       | it out.
       | 
       | In the end I did what pretty much everyone else did, Found the
       | BLTCON0 for Bobs and straight copies and then pretended I newer
       | saw the thing.
       | 
       | I did however get an A+ in computational logic at university
       | years later, so maybe some of the trauma turned out to be
       | beneficial.
        
       | Findecanor wrote:
       | I didn't have the official Amiga hardware manual, but instead the
       | book "Mapping the Amiga". It said the same thing in a slight more
       | verbose way. I don't remember which minterms I used back then but
       | I _think_ I managed to work things out from this book to do
       | shadebobs, bobs, XOR 3D line drawing and other things.
       | 
       | The page in Mapping the Amiga:
       | https://archive.org/details/1993-thomson-randy-rhett-anderso...
        
       | mmozeiko wrote:
       | There is a simple way to get that immediate from expression you
       | want to calculate. For example, if you want to calculate
       | following expression:                   (NOT A) OR ((NOT B) XOR
       | (C AND A))
       | 
       | then you simply write                   ~_MM_TERNLOG_A |
       | (~_MM_TERNLOG_B ^ (_MM_TERNLOG_C & _MM_TERNLOG_A))
       | 
       | Literally the expression you want to calculate. It evaluates to
       | immediate from _MM_TERNLOG_A/B/C constants defined in intrinsic
       | headers, at least for gcc & clang:                   typedef enum
       | {           _MM_TERNLOG_A = 0xF0,           _MM_TERNLOG_B = 0xCC,
       | _MM_TERNLOG_C = 0xAA         } _MM_TERNLOG_ENUM;
       | 
       | For MSVC you define them yourself.
        
       | notfed wrote:
       | Couldn't every Boolean operation be "busted" as a lookup table?
        
         | ekimekim wrote:
         | Yes! But your lookup table will need 2^N bits for a function
         | with N inputs. In this way you can easily enumerate all
         | possible functions from N bits to 1 bit.
         | 
         | As a fun exercise, you can do this for all 2-bit -> 1-bit
         | functions. There's only 16 of them, and most of them have very
         | well known names like "and" (LUT 1000) or "xor" (LUT 0110).
         | Some of them don't depend on some of the inputs (eg. LUT 1100 /
         | 1010 which is "return A" and "return B" respectively) or even
         | any of them (eg. LUT 0000 which always returns 0).
        
       | cubefox wrote:
       | About the title: "Ternary logic" usually means "logic with three
       | truth values". But this piece covers a compiler instruction which
       | handles all binary logic gates with three inputs.
        
         | dzaima wrote:
         | The x86 instruction is named 'ternlog', and intrinsic -
         | 'ternarylogic' though; while perhaps unfortunate, the title is
         | appropriate. (and even then 'bitwise' already sort of takes
         | place of what 'ternary'-as-three-valued would, and 'ternary' is
         | also very often three-input, so much so that 'a ? b : c' is
         | often called _the_ ternary operator (and in fact ternlog can
         | simulate this ternary operation; and in fact the article is
         | even about exactly that))
        
       | transfire wrote:
       | Great little article! Thank you.
        
       | londons_explore wrote:
       | Do compilers actually output this instruction?
       | 
       | So many super-clever instructions are next to impossible for
       | compilers to automatically use.
        
       | leogao wrote:
       | Nvidia SASS has a similar instruction too (LOP3.LUT)
        
       | fallingsquirrel wrote:
       | Another example of packing bitwise ops into an integer is win32's
       | GDI ROP codes: https://learn.microsoft.com/en-
       | us/windows/win32/gdi/ternary-...
        
       ___________________________________________________________________
       (page generated 2024-10-06 23:00 UTC)