Coverage for tests/codegen/generated/variants/intrinsics.py: 92%

37 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-20 16:25 +0000

1import ctypes 

2from typing import Any 

3import wasmtime 

4 

5 

6def _store(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int, val: Any) -> None: 

7 ptr = (base & 0xffffffff) + offset 

8 if ptr + ctypes.sizeof(ty) > mem.data_len(store): 

9 raise IndexError('out-of-bounds store') 

10 raw_base = mem.data_ptr(store) 

11 c_ptr = ctypes.POINTER(ty)( 

12 ty.from_address(ctypes.addressof(raw_base.contents) + ptr) 

13 ) 

14 c_ptr[0] = val 

15 

16 

17def _clamp(i: int, min: int, max: int) -> int: 

18 if i < min or i > max: 

19 raise OverflowError(f'must be between {min} and {max}') 

20 return i 

21 

22_i32_to_f32_i32 = ctypes.pointer(ctypes.c_int32(0)) 

23_i32_to_f32_f32 = ctypes.cast(_i32_to_f32_i32, ctypes.POINTER(ctypes.c_float)) 

24 

25def _i32_to_f32(i: int) -> float: 

26 _i32_to_f32_i32[0] = i 

27 return _i32_to_f32_f32[0] 

28 

29_i64_to_f64_i64 = ctypes.pointer(ctypes.c_int64(0)) 

30_i64_to_f64_f64 = ctypes.cast(_i64_to_f64_i64, ctypes.POINTER(ctypes.c_double)) 

31 

32def _i64_to_f64(i: int) -> float: 

33 _i64_to_f64_i64[0] = i 

34 return _i64_to_f64_f64[0] 

35 

36 

37def _load(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int) -> Any: 

38 ptr = (base & 0xffffffff) + offset 

39 if ptr + ctypes.sizeof(ty) > mem.data_len(store): 

40 raise IndexError('out-of-bounds store') 

41 raw_base = mem.data_ptr(store) 

42 c_ptr = ctypes.POINTER(ty)( 

43 ty.from_address(ctypes.addressof(raw_base.contents) + ptr) 

44 ) 

45 return c_ptr[0] 

46 

47 

48def _f32_to_i32(i: float) -> int: 

49 _i32_to_f32_f32[0] = i 

50 return _i32_to_f32_i32[0] 

51 

52 

53def _f64_to_i64(i: float) -> int: 

54 _i64_to_f64_f64[0] = i 

55 return _i64_to_f64_i64[0] 

56