Giter VIP home page Giter VIP logo

ternary-logic's Introduction

Ternary functions for SSE and AVX2

Introduction

AVX512F introduced instruction vpternlog (intrinsic _mm512_ternarylogic_epi{32,64}), which evaluates a three-argument boolean function for each bit of operands. The function is given as an integer number from 0 to 255.

This repository contains a kind of library which allows a programmer to use ternary functions in SSE, XOP, AVX2 and x86 in similar manner, by choosing the function by its number.

In native AVX512F code you would write:

// 0x96 = A xor B xor C
const __m512i res = _mm512_ternarylogic_epi32(A, B, C, 0x96);

With help of the library you can write an AVX2 program:

const __m256i res = ternarylogic::avx2::ternary<0x96>(A, B, C);

or a SSE program:

const __m128i res = ternarylogic::sse::ternary<0x96>(A, B, C);

Details

The function number comes from the result column of the function's truth table. The number is formed from bits a, b, ... h. You can find more details and examples in my article.

A B C function
0 0 0

a

0 0 1

b

0 1 0

c

0 1 1

d

1 0 0

e

1 0 1

f

1 1 0

g

1 1 1

h

Intel has published list of all functions in form of logical expression in their monumental document "Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2 (2A, 2B, 2C & 2D): Instruction Set Reference, A-Z".

I extracted these functions and created application which generates SSE and AVX2 code. List of function is in the file py/data/intel.txt, I also put there more details. Later I manually optimized some of these function. And in the end I developed a program which found many better representations of functions for both Intel and AMD flavours of SIMD ISA.

Usage

You need python and make.

Type make, then following files will be generated:

  • ternary_sse.cpp,
  • ternary_avx2.cpp,
  • ternary_avx512.cpp (use only two-argument logic instructions),
  • ternary_xop.cpp,
  • ternary_x86_32.cpp,
  • ternary_x86_64.cpp.

You can include them directly into your application.

Programs validate_sse, validate_avx2 and validate_xop test if all generated functions are correct.

See also

ternary-logic's People

Contributors

wojciechmula avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.