Scroll to navigation

HADD, RHADD(3clc) OpenCL Manual HADD, RHADD(3clc)


hadd,_rhadd - Returns (x + y) >> 1, or (x + y + 1) >> 1

gentype hadd(gentype x, gentype y);

gentype rhadd(gentype x, gentype y);


hadd returns (x+y) >> 1. The intermediate sum does not modulo overflow.

rhadd returns (x+y+1) >> 1. The intermediate sum does not modulo overflow.


Frequently vector operations need n + 1 bits temporarily to calculate a result. The rhadd instruction gives you an extra bit without needing to upsample and downsample. This can be a profound performance win.

Built-in integer functions take scalar or vector arguments. The vector versions of the integer functions operate component-wise. The description is per component.

We use the generic type name gentype to indicate that the function can take char, char{2|3|4|8|16}, uchar, uchar{2|3|4|8|16}, short, short{2|3|4|8|16}, ushort, ushort{2|3|4|8|16}, int, int{2|3|4|8|16}, uint, uint{2|3|4|8|16}, long, long{2|3|4|8|16}, ulong, or ulong{2|3|4|8|16} as the type for the arguments. We use the generic type name ugentype to refer to unsigned versions of gentype. For example, if gentype is char4, ugentype is uchar4.

We also use the generic type name sgentype to indicate that the function can take a scalar data type i.e. char, uchar, short, ushort, int, uint, long, or ulong as the type for the arguments. For built-in integer functions that take gentype and sgentype arguments, the gentype argument must be a vector or scalar version of the sgentype argument. For example, if sgentype is uchar, gentype must be uchar or uchar{2|3|4|8|16}. For vector versions, sgentype is implicitly widened to gentype as described in section 6.3.a of the OpenCL specification.

For any specific use of a function, the actual type has to be the same for all arguments and the return type unless otherwise specified.


OpenCL Specification[1]




The Khronos Group


Copyright © 2007-2011 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.


OpenCL Specification
page 256, section 6.12.3 - Integer Functions
01/14/2021 The Khronos Group