Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Yiwu Chen <[email protected]>
  • Loading branch information
soraros committed Oct 16, 2024
1 parent 8e7ace2 commit f5b8f1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
24 changes: 11 additions & 13 deletions stdlib/src/builtin/simd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1735,19 +1735,19 @@ struct SIMD[type: DType, size: Int](
alias integral_type = FPUtils[type].integral_type
return bitcast[dest_type, size](self.cast[integral_type]())

@always_inline
fn _float_to_bits[dest_type: DType](self) -> SIMD[dest_type, size]:
"""Bitcasts the floating-point value to an integer value.
# @always_inline
# fn _float_to_bits[dest_type: DType](self) -> SIMD[dest_type, size]:
# """Bitcasts the floating-point value to an integer value.

Parameters:
dest_type: DType to bitcast the input SIMD vector to.
# Parameters:
# dest_type: DType to bitcast the input SIMD vector to.

Returns:
An integer representation of the floating-point value.
"""
alias integral_type = FPUtils[type].integral_type
var v = bitcast[integral_type, size](self)
return v.cast[dest_type]()
# Returns:
# An integer representation of the floating-point value.
# """
# alias integral_type = FPUtils[type].integral_type
# var v = bitcast[integral_type, size](self)
# return v.cast[dest_type]()

# FIXME: `_integral_type_of` doesn't work with `DType.bool`.
@always_inline
Expand Down Expand Up @@ -3219,12 +3219,10 @@ fn _floor(x: SIMD) -> __type_of(x):
alias bias = FPUtils[x.type].exponent_bias()
alias shift_factor = bitwidth - exponent_width - 1

# var bits = bitcast[integral_type, x.size](x)
bits = x.to_bits()
var e = ((bits >> mantissa_width) & mask) - bias
bits = (e < shift_factor).select(
bits & ~((1 << (shift_factor - e)) - 1),
bits,
)
return __type_of(x)(from_bits=bits)
# return bitcast[x.type, x.size](bits)
16 changes: 8 additions & 8 deletions stdlib/src/math/math.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ fn exp[

@always_inline
fn _frexp_mask1[
simd_width: Int, type: DType, integral_type: DType
]() -> SIMD[integral_type, simd_width]:
simd_width: Int, type: DType
]() -> SIMD[_integral_type_of[type](), simd_width]:
@parameter
if type is DType.float16:
return 0x7C00
Expand All @@ -642,8 +642,8 @@ fn _frexp_mask1[

@always_inline
fn _frexp_mask2[
simd_width: Int, type: DType, integral_type: DType
]() -> SIMD[integral_type, simd_width]:
simd_width: Int, type: DType
]() -> SIMD[_integral_type_of[type](), simd_width]:
@parameter
if type is DType.float16:
return 0x3800
Expand Down Expand Up @@ -682,9 +682,9 @@ fn frexp[
alias zero = SIMD[type, simd_width](0)
alias max_exponent = FPUtils[type].max_exponent() - 2
alias mantissa_width = FPUtils[type].mantissa_width()
var mask1 = _frexp_mask1[simd_width, type, integral_type]()
var mask2 = _frexp_mask2[simd_width, type, integral_type]()
var x_int = x._float_to_bits[integral_type]()
var mask1 = _frexp_mask1[simd_width, type]()
var mask2 = _frexp_mask2[simd_width, type]()
var x_int = x.to_bits()
var selector = x != zero
var exp = selector.select(
(((mask1 & x_int) >> mantissa_width) - max_exponent).cast[type](),
Expand All @@ -693,7 +693,7 @@ fn frexp[
var frac = selector.select(
((x_int & ~mask1) | mask2)._bits_to_float[type](), zero
)
return StaticTuple[SIMD[type, simd_width], 2](frac, exp)
return StaticTuple[size=2](frac, exp)


# ===----------------------------------------------------------------------=== #
Expand Down

0 comments on commit f5b8f1a

Please sign in to comment.