FMA(3clc)
πΈ FMA(3clc) β the OpenCL manual, bestie edition πΈ
okay so this is FMA(3clc) from the OpenCL Manual, and we are gonna talk about the single most underrated little function in the whole math library. sit down, hydrate, let's go π
β¨ NAME β¨
fma β Multiply and add, then round. that's it. that's the whole vibe. it's giving "one job, done perfectly."
and the signature, which you are going to copy EXACTLY, no typos, no freestyling:
gentype fma(gentype a, gentype b, gentype c);
π DESCRIPTION π
so what does it actually do. it returns the correctly rounded floating-point representation of the sum of c with the infinitely precise product of a and b.
read that again. the product of a and b is computed at infinite precision first, and only then does the whole thing get rounded. so it's like a * b + c but with main character energy and zero intermediate rounding.
no cap: rounding of intermediate products shall not occur. like, at all. not even a little. it's not a vibe, it's a guarantee.
and if you hit an edge case? edge case behavior is per the IEEE 754-2008 standard. periodt.
π NOTES π
the vector versions of the math functions operate component-wise, and the description is per-component. so every lane does its own lil thing, independently, slay.
the built-in math functions are not affected by the prevailing rounding mode in the calling environment, and they always return the same value as they would if called with the round to nearest even rounding mode. so like, whatever your environment thinks it's doing, these functions said "no thank you" and stayed consistent π«Ά
the built-in math functions take scalar or vector arguments. for any specific use of these functions, the actual type has to be the same for all arguments and the return type, unless otherwise specified. no mixing, bestie. matching types or nothing.
now the type lore, and this part is important so don't skim:
- the generic type name
gentypemeans the function can takefloat,float2,float3,float4,float8,float16,double,double2,double3,double4,double8, ordouble16as the type for the arguments. - if extended with
cl_khr_fp16(3clc), generic type namegentypemay indicatehalfandhalf{2|3|4|8|16}as arguments and return values. (yes, half precision got an invite to the party π) - the generic type name
gentypefmeans the function can takefloat,float2,float3,float4,float8, orfloat16as the type for the arguments. - the generic type name
gentypedmeans the function can takedouble,double2,double3,double4,double8, ordouble16as the type for the arguments.
π SPECIFICATION π
OpenCL Specification[1] β go read it, it's the source of truth, iykyk.
π¦ SEE ALSO π¦
(and yes, FP_CONTRACT(3clc) really is listed twice in the original and i am respectfully not touching that, it's iconic in its own way π)
π AUTHORS π
The Khronos Group
π COPYRIGHT π
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 following condition:
the above copyright notice and this permission notice shall be included in all copies or substantial portions of the Materials.
(lowkey the most chill license paragraph ever. it literally says "do whatever, just keep this notice." we stan permissive licensing π)
π NOTES π
- OpenCL Specification page 244, section 6.12.2 - Math Functions
aaaand that's the page!! tysm for reading, you now officially understand fma better than like 90% of people who use it every day πβ¨
The Khronos Group Β· 01/14/2021 Β· FMA(3clc) β mwah, take care of yourself, drink water, and remember: no intermediate rounding, ever π«Άπ πΈ
π¦ where this page comes from
- ubuntu jammy: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- ubuntu noble: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms