11from numba import cuda , float32
22from math import cos , sin , tan , exp , log , log10 , log2 , pow
3+ import numpy as np
34from numba .cuda .testing import CUDATestCase , skip_on_cudasim
45import unittest
56
67
78class TestFastMathOption (CUDATestCase ):
9+ @skip_on_cudasim ('fast divide not available in CUDASIM' )
810 def test_kernel (self ):
9-
11+ # Test the cast of an int being used in fastmath divide
1012 def foo (arr , val ):
1113 i = cuda .grid (1 )
1214 if i < arr .size :
@@ -15,8 +17,8 @@ def foo(arr, val):
1517 fastver = cuda .jit ("void(float32[:], float32)" , fastmath = True )(foo )
1618 precver = cuda .jit ("void(float32[:], float32)" )(foo )
1719
18- self .assertIn ('div.full .ftz.f32' , fastver .ptx )
19- self .assertNotIn ('div.full .ftz.f32' , precver .ptx )
20+ self .assertIn ('div.approx .ftz.f32' , fastver .ptx )
21+ self .assertNotIn ('div.approx .ftz.f32' , precver .ptx )
2022
2123 @skip_on_cudasim ('fast cos not available in CUDASIM' )
2224 def test_cosf (self ):
@@ -105,6 +107,38 @@ def f8(r, x, y):
105107 self .assertIn ('lg2.approx.ftz.f32 ' , fastver .ptx )
106108 self .assertNotIn ('lg2.approx.ftz.f32 ' , slowver .ptx )
107109
110+ @skip_on_cudasim ('fast divide not available in CUDASIM' )
111+ def test_divf (self ):
112+ def f9 (r , x , y ):
113+ r [0 ] = x / y
114+
115+ fastver = cuda .jit ("void(float32[::1], float32, float32)" ,
116+ fastmath = True )(f9 )
117+ slowver = cuda .jit ("void(float32[::1], float32, float32)" )(f9 )
118+ self .assertIn ('div.approx.ftz.f32 ' , fastver .ptx )
119+ self .assertNotIn ('div.approx.ftz.f32 ' , slowver .ptx )
120+ self .assertIn ('div.rn.f32' , slowver .ptx )
121+ self .assertNotIn ('div.rn.f32' , fastver .ptx )
122+
123+ @skip_on_cudasim ('fast divide not available in CUDASIM' )
124+ def test_divf_exception (self ):
125+ def f10 (r , x , y ):
126+ r [0 ] = x / y
127+
128+ fastver = cuda .jit ("void(float32[::1], float32, float32)" ,
129+ fastmath = True , debug = True )(f10 )
130+ slowver = cuda .jit ("void(float32[::1], float32, float32)" ,
131+ debug = True )(f10 )
132+ nelem = 10
133+ ary = np .empty (nelem , dtype = np .float32 )
134+ with self .assertRaises (ZeroDivisionError ):
135+ slowver [1 , nelem ](ary , 10.0 , 0.0 )
136+
137+ try :
138+ fastver [1 , nelem ](ary , 10.0 , 0.0 )
139+ except ZeroDivisionError :
140+ self .fail ("Divide in fastmath should not throw ZeroDivisionError" )
141+
108142 def test_device (self ):
109143 # fastmath option is ignored for device function
110144 @cuda .jit ("float32(float32, float32)" , device = True )
0 commit comments