# atomicAdd function for an unsigned short value

**URL:** <https://forums.developer.nvidia.com/t/atomicadd-function-for-an-unsigned-short-value/173529>\
**Category:** CUDA Programming and Performance\
**Created:** [March 28, 2021, 3:26pm UTC](https://forums.developer.nvidia.com/t/atomicadd-function-for-an-unsigned-short-value/173529 "2021-03-28T15:26:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmitry.ostapchuk](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dmitry.ostapchuk](https://forums.developer.nvidia.com/u/dmitry.ostapchuk)\
**Post date:** [March 28, 2021, 3:26pm UTC](https://forums.developer.nvidia.com/t/atomicadd-function-for-an-unsigned-short-value/173529/1 "2021-03-28T15:26:31Z")

</div>

Hallo cuda fans,

I need a function to add two unsigned short values atomically. The atomicAdd function is only for a 32Bit integer but I use an unsigned short value.

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [March 28, 2021, 5:45pm UTC](https://forums.developer.nvidia.com/t/atomicadd-function-for-an-unsigned-short-value/173529/2 "2021-03-28T17:45:53Z")

</div>

Is there a particular reason you cannot simply substitute a 32-bit integer? Rule of thumb based on experience: Every integer wants to be an `int` unless there is a compelling reason for it to be some other integer type.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [March 29, 2021, 1:58pm UTC](https://forums.developer.nvidia.com/t/atomicadd-function-for-an-unsigned-short-value/173529/3 "2021-03-29T13:58:51Z")

</div>

You can do atomic operations on one byte and two byte quantities, using a 4-byte atomic operation plus some extra processing. [this thread](https://forums.developer.nvidia.com/t/how-to-use-atomiccas-to-implement-atomicadd-short-trouble-adapting-programming-guide-example/22712/12) may be of interest.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [March 29, 2021, 2:45pm UTC](https://forums.developer.nvidia.com/t/atomicadd-function-for-an-unsigned-short-value/173529/4 "2021-03-29T14:45:52Z")

</div>

with `unsigned short` support [available](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#atomiccas) in `atomicCAS` you can directly combine that with the [custom atomic example](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#atomic-functions) given in the programming guide to create a 16-bit atomic op. Any of these methods that I have mentioned will be less performant than doing a native 32-bit atomic op, so as hinted by njuffa that realization may be preferred for performance.
