MAINT: improved overflow check to avoid undefined behavior (#21658)
MAINT: improved overflow check to avoid undefined behavior (#21658)

* MAINT: improved overflow check to avoid undefined behavior

* Fixed template

* Simplified check
rgommers/numpy,endolith/numpy,endolith/numpy,anntzer/numpy,endolith/numpy,numpy/numpy,pdebuyl/numpy,rgommers/numpy,mhvk/numpy,mattip/numpy,mhvk/numpy,charris/numpy,numpy/numpy,mhvk/numpy,anntzer/numpy,charris/numpy,pdebuyl/numpy,pdebuyl/numpy,mattip/numpy,anntzer/numpy,mhvk/numpy,mattip/numpy,pdebuyl/numpy,charris/numpy,mhvk/numpy,anntzer/numpy,endolith/numpy,charris/numpy,rgommers/numpy,numpy/numpy,rgommers/numpy,numpy/numpy,mattip/numpy