FMA(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

🌸 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 gentype means the function can take float, float2, float3, float4, float8, float16, double, double2, double3, double4, double8, or double16 as the type for the arguments.
  • if extended with cl_khr_fp16(3clc), generic type name gentype may indicate half and half{2|3|4|8|16} as arguments and return values. (yes, half precision got an invite to the party πŸ’œ)
  • the generic type name gentypef means the function can take float, float2, float3, float4, float8, or float16 as the type for the arguments.
  • the generic type name gentyped means the function can take double, double2, double3, double4, double8, or double16 as 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 Β© 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 πŸ“

  1. 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