Chapter 5. Kernel programming: operators and functions

 

This chapter covers

  • Operators for scalar and vector types
  • OpenCL’s built-in functions and their usage in code

Chapter 4 discussed data types and emphasized the different types of vectors available. Vectors can dramatically improve an application’s performance by making it possible to operate on multiple values at once. But before you can use them in a practical application, you need to know what operators and functions are available to process them. The goal of this chapter is to explain these operators and functions, and to provide example code to demonstrate their usage.

Most of these operators and functions relate to mathematics, and the OpenCL working group has wisely kept to the naming conventions set by math.h. For example, you can call sqrt to compute the square root of a floating-point vector and pow to compute xy. But OpenCL also provides new functions like mad_sat and mad_hi, which make it possible to perform math operations quickly and accurately.

The bulk of this chapter discusses OpenCL’s built-in functions. They’re called built-in because you don’t need to link additional libraries or include any special header files. There are many built-in functions available, so I’ve divided them into seven categories:

5.1. Operators

5.2. Work-item and work-group functions

5.3. Data transfer operations

5.4. Floating-point functions

5.5. Integer functions

5.6. Shuffle and select functions

5.7. Vector test functions

5.8. Geometric functions

5.9. Summary