Skip to content

Commit a9951e4

Browse files
committed
Add a function related to fp submodule
1 parent a28134e commit a9951e4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

python/src/simuhw/fp/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
__all__ += [
4949
'TininessMode', 'RoundingMode', 'ExceptionFlag',
50-
'Float', 'Float16', 'Float32', 'Float64', 'Float128',
50+
'Float', 'Float16', 'Float32', 'Float64', 'Float128', 'dsize_to_dtype',
5151
'FPState', 'FPUnaryOperator', 'FPBinaryOperator', 'FPTernaryOperator',
5252
'SIMD_FPUnaryOperator', 'SIMD_FPBinaryOperator', 'SIMD_FPTernaryOperator',
5353
'FPNegator', 'SIMD_FPNegator',
@@ -70,7 +70,7 @@
7070

7171
from softfloatpy import TininessMode, RoundingMode, ExceptionFlag
7272
from ._operator import (
73-
Float, Float16, Float32, Float64, Float128,
73+
Float, Float16, Float32, Float64, Float128, dsize_to_dtype,
7474
FPState, FPUnaryOperator, FPBinaryOperator, FPTernaryOperator,
7575
SIMD_FPUnaryOperator, SIMD_FPBinaryOperator, SIMD_FPTernaryOperator
7676
)

python/src/simuhw/fp/_operator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ class Float128(sf.Float128, metaclass=ABCMeta):
5555
Float = type[Float16 | Float32 | Float64 | Float128]
5656

5757

58+
def dsize_to_dtype(dsize: int) -> Float:
59+
"""Returns a floating-point type that has the specified data size.
60+
61+
Args:
62+
dsize: The data size in bits.
63+
64+
Returns:
65+
A floating-point type that has the specified data size.
66+
67+
Raises:
68+
ValueError: If no floating-point type that has the specified data size.
69+
70+
"""
71+
if dsize == 16:
72+
return Float16
73+
if dsize == 32:
74+
return Float32
75+
if dsize == 64:
76+
return Float64
77+
if dsize == 128:
78+
return Float128
79+
raise ValueError(f'No floating-point type with {dsize} bits')
80+
81+
5882
class FPState(metaclass=ABCMeta):
5983
"""A class that manipulates the floating-point states."""
6084

0 commit comments

Comments
 (0)