https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100008

            Bug ID: 100008
           Summary: std::clamp generates suboptimal assembly for primitive
                    types
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vittorio.romeo at outlook dot com
  Target Milestone: ---

`std::clamp` generates poor assembly compared to hand-written counterpart for
primitive types like `float`, even with `-Ofast -ffast-math`:

stdclamp(float, float, float):
        comiss  xmm0, xmm1
        jb      .L2
        movaps  xmm1, xmm0
        minss   xmm1, xmm2
.L2:
        movaps  xmm0, xmm1
        ret

myclamp(float, float, float):
        maxss   xmm0, xmm1
        minss   xmm0, xmm2
        ret

Live example:
https://godbolt.org/z/5oxvocevK

More information on:
https://secret.club/2021/04/09/std-clamp.html

Reply via email to