Skip to content

Consistently use type t in unboxed libs #2562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 70 additions & 86 deletions ocaml/otherlibs/beta/float32.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,99 +18,86 @@

type t = float32

external float32_of_bits : int32 -> float32
external float32_of_bits : int32 -> t
= "caml_float32_of_bits_bytecode" "caml_float32_of_bits"
[@@unboxed] [@@noalloc]

external neg : (float32[@local_opt]) -> (float32[@local_opt]) = "%negfloat32"
external neg : (t[@local_opt]) -> (t[@local_opt]) = "%negfloat32"

external add :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external add : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%addfloat32"

external sub :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external sub : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%subfloat32"

external mul :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external mul : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%mulfloat32"

external div :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external div : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%divfloat32"

external pow : float32 -> float32 -> float32
= "caml_power_float32_bytecode" "powf"
external pow : t -> t -> t = "caml_power_float32_bytecode" "powf"
[@@unboxed] [@@noalloc]

module Operators = struct
external ( ~-. ) : (float32[@local_opt]) -> (float32[@local_opt])
= "%negfloat32"
external ( ~-. ) : (t[@local_opt]) -> (t[@local_opt]) = "%negfloat32"

external ( +. ) :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external ( +. ) : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%addfloat32"

external ( -. ) :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external ( -. ) : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%subfloat32"

external ( *. ) :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external ( *. ) : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%mulfloat32"

external ( /. ) :
(float32[@local_opt]) -> (float32[@local_opt]) -> (float32[@local_opt])
external ( /. ) : (t[@local_opt]) -> (t[@local_opt]) -> (t[@local_opt])
= "%divfloat32"

external ( ** ) : float32 -> float32 -> float32
= "caml_power_float32_bytecode" "powf"
external ( ** ) : t -> t -> t = "caml_power_float32_bytecode" "powf"
[@@unboxed] [@@noalloc]
end

external fma : float32 -> float32 -> float32 -> float32
= "caml_fma_float32_bytecode" "fmaf"
external fma : t -> t -> t -> t = "caml_fma_float32_bytecode" "fmaf"
[@@unboxed] [@@noalloc]

external rem : float32 -> float32 -> float32
= "caml_fmod_float32_bytecode" "fmodf"
external rem : t -> t -> t = "caml_fmod_float32_bytecode" "fmodf"
[@@unboxed] [@@noalloc]

external abs : (float32[@local_opt]) -> (float32[@local_opt]) = "%absfloat32"
external abs : (t[@local_opt]) -> (t[@local_opt]) = "%absfloat32"

let zero = 0.s
let one = 1.s
let minus_one = -1.s
let infinity = float32_of_bits 0x7f800000l
let neg_infinity = float32_of_bits 0xff800000l
let nan = float32_of_bits 0x7f800001l
let is_finite (x : float32) = sub x x = 0.s
let is_infinite (x : float32) = div 1.s x = 0.s
let is_nan (x : float32) = x <> x
let is_finite (x : t) = sub x x = 0.s
let is_infinite (x : t) = div 1.s x = 0.s
let is_nan (x : t) = x <> x
let pi = 0x1.921fb6p+1s
let max_float = float32_of_bits 0x7f7fffffl
let min_float = float32_of_bits 0x00800000l
let epsilon = float32_of_bits 0x34000000l

external of_int : int -> float32 = "%float32ofint"
external to_int : (float32[@local_opt]) -> int = "%intoffloat32"
external of_float : (float[@local_opt]) -> float32 = "%float32offloat"
external to_float : (float32[@local_opt]) -> float = "%floatoffloat32"
external of_int : int -> t = "%float32ofint"
external to_int : (t[@local_opt]) -> int = "%intoffloat32"
external of_float : (float[@local_opt]) -> t = "%float32offloat"
external to_float : (t[@local_opt]) -> float = "%floatoffloat32"

external of_bits : (int32[@local_opt]) -> float32
external of_bits : (int32[@local_opt]) -> t
= "caml_float32_of_bits_bytecode" "caml_float32_of_bits"
[@@unboxed] [@@noalloc] [@@builtin]

external to_bits : (float32[@local_opt]) -> int32
external to_bits : (t[@local_opt]) -> int32
= "caml_float32_to_bits_bytecode" "caml_float32_to_bits"
[@@unboxed] [@@noalloc] [@@builtin]

external of_string : string -> float32 = "caml_float32_of_string"
external of_string : string -> t = "caml_float32_of_string"

let of_string_opt s = try Some (of_string s) with Failure _ -> None

external format : string -> float32 -> string = "caml_format_float32"
external format : string -> t -> string = "caml_format_float32"

let to_string f = Stdlib.valid_float_lexem (format "%.9g" f)

Expand All @@ -121,153 +108,150 @@ type fpclass = Stdlib.fpclass =
| FP_infinite
| FP_nan

external classify_float : (float32[@unboxed]) -> fpclass
external classify_float : (t[@unboxed]) -> fpclass
= "caml_classify_float32_bytecode" "caml_classify_float32"
[@@noalloc]

external sqrt : float32 -> float32 = "caml_sqrt_float32_bytecode" "sqrtf"
external sqrt : t -> t = "caml_sqrt_float32_bytecode" "sqrtf"
[@@unboxed] [@@noalloc] [@@builtin]

external cbrt : float32 -> float32 = "caml_cbrt_float32_bytecode" "cbrtf"
external cbrt : t -> t = "caml_cbrt_float32_bytecode" "cbrtf"
[@@unboxed] [@@noalloc]

external exp : float32 -> float32 = "caml_exp_float32_bytecode" "expf"
external exp : t -> t = "caml_exp_float32_bytecode" "expf"
[@@unboxed] [@@noalloc]

external exp2 : float32 -> float32 = "caml_exp2_float32_bytecode" "exp2f"
external exp2 : t -> t = "caml_exp2_float32_bytecode" "exp2f"
[@@unboxed] [@@noalloc]

external log : float32 -> float32 = "caml_log_float32_bytecode" "logf"
external log : t -> t = "caml_log_float32_bytecode" "logf"
[@@unboxed] [@@noalloc]

external log10 : float32 -> float32 = "caml_log10_float32_bytecode" "log10f"
external log10 : t -> t = "caml_log10_float32_bytecode" "log10f"
[@@unboxed] [@@noalloc]

external log2 : float32 -> float32 = "caml_log2_float32_bytecode" "log2f"
external log2 : t -> t = "caml_log2_float32_bytecode" "log2f"
[@@unboxed] [@@noalloc]

external expm1 : float32 -> float32 = "caml_expm1_float32_bytecode" "expm1f"
external expm1 : t -> t = "caml_expm1_float32_bytecode" "expm1f"
[@@unboxed] [@@noalloc]

external log1p : float32 -> float32 = "caml_log1p_float32_bytecode" "log1pf"
external log1p : t -> t = "caml_log1p_float32_bytecode" "log1pf"
[@@unboxed] [@@noalloc]

external cos : float32 -> float32 = "caml_cos_float32_bytecode" "cosf"
external cos : t -> t = "caml_cos_float32_bytecode" "cosf"
[@@unboxed] [@@noalloc]

external sin : float32 -> float32 = "caml_sin_float32_bytecode" "sinf"
external sin : t -> t = "caml_sin_float32_bytecode" "sinf"
[@@unboxed] [@@noalloc]

external tan : float32 -> float32 = "caml_tan_float32_bytecode" "tanf"
external tan : t -> t = "caml_tan_float32_bytecode" "tanf"
[@@unboxed] [@@noalloc]

external acos : float32 -> float32 = "caml_acos_float32_bytecode" "acosf"
external acos : t -> t = "caml_acos_float32_bytecode" "acosf"
[@@unboxed] [@@noalloc]

external asin : float32 -> float32 = "caml_asin_float32_bytecode" "asinf"
external asin : t -> t = "caml_asin_float32_bytecode" "asinf"
[@@unboxed] [@@noalloc]

external atan : float32 -> float32 = "caml_atan_float32_bytecode" "atanf"
external atan : t -> t = "caml_atan_float32_bytecode" "atanf"
[@@unboxed] [@@noalloc]

external atan2 : float32 -> float32 -> float32
= "caml_atan2_float32_bytecode" "atan2f"
external atan2 : t -> t -> t = "caml_atan2_float32_bytecode" "atan2f"
[@@unboxed] [@@noalloc]

external hypot : float32 -> float32 -> float32
= "caml_hypot_float32_bytecode" "hypotf"
external hypot : t -> t -> t = "caml_hypot_float32_bytecode" "hypotf"
[@@unboxed] [@@noalloc]

external cosh : float32 -> float32 = "caml_cosh_float32_bytecode" "coshf"
external cosh : t -> t = "caml_cosh_float32_bytecode" "coshf"
[@@unboxed] [@@noalloc]

external sinh : float32 -> float32 = "caml_sinh_float32_bytecode" "sinhf"
external sinh : t -> t = "caml_sinh_float32_bytecode" "sinhf"
[@@unboxed] [@@noalloc]

external tanh : float32 -> float32 = "caml_tanh_float32_bytecode" "tanhf"
external tanh : t -> t = "caml_tanh_float32_bytecode" "tanhf"
[@@unboxed] [@@noalloc]

external acosh : float32 -> float32 = "caml_acosh_float32_bytecode" "acoshf"
external acosh : t -> t = "caml_acosh_float32_bytecode" "acoshf"
[@@unboxed] [@@noalloc]

external asinh : float32 -> float32 = "caml_asinh_float32_bytecode" "asinhf"
external asinh : t -> t = "caml_asinh_float32_bytecode" "asinhf"
[@@unboxed] [@@noalloc]

external atanh : float32 -> float32 = "caml_atanh_float32_bytecode" "atanhf"
external atanh : t -> t = "caml_atanh_float32_bytecode" "atanhf"
[@@unboxed] [@@noalloc]

external erf : float32 -> float32 = "caml_erf_float32_bytecode" "erff"
external erf : t -> t = "caml_erf_float32_bytecode" "erff"
[@@unboxed] [@@noalloc]

external erfc : float32 -> float32 = "caml_erfc_float32_bytecode" "erfcf"
external erfc : t -> t = "caml_erfc_float32_bytecode" "erfcf"
[@@unboxed] [@@noalloc]

external trunc : float32 -> float32 = "caml_trunc_float32_bytecode" "truncf"
external trunc : t -> t = "caml_trunc_float32_bytecode" "truncf"
[@@unboxed] [@@noalloc]

external round : float32 -> float32 = "caml_round_float32_bytecode" "roundf"
external round : t -> t = "caml_round_float32_bytecode" "roundf"
[@@unboxed] [@@noalloc]

external ceil : float32 -> float32 = "caml_ceil_float32_bytecode" "ceilf"
external ceil : t -> t = "caml_ceil_float32_bytecode" "ceilf"
[@@unboxed] [@@noalloc]

external floor : float32 -> float32 = "caml_floor_float32_bytecode" "floorf"
external floor : t -> t = "caml_floor_float32_bytecode" "floorf"
[@@unboxed] [@@noalloc]

let is_integer x = x = trunc x && is_finite x

external next_after : float32 -> float32 -> float32
external next_after : t -> t -> t
= "caml_nextafter_float32_bytecode" "nextafterf"
[@@unboxed] [@@noalloc]

let succ x = next_after x infinity
let pred x = next_after x neg_infinity

external copy_sign : float32 -> float32 -> float32
= "caml_copysign_float32_bytecode" "copysignf"
external copy_sign : t -> t -> t = "caml_copysign_float32_bytecode" "copysignf"
[@@unboxed] [@@noalloc]

external sign_bit : (float32[@unboxed]) -> bool
external sign_bit : (t[@unboxed]) -> bool
= "caml_signbit_float32_bytecode" "caml_signbit_float32"
[@@noalloc]

external frexp : float32 -> float32 * int = "caml_frexp_float32"
external frexp : t -> t * int = "caml_frexp_float32"

external ldexp : (float32[@unboxed]) -> (int[@untagged]) -> (float32[@unboxed])
external ldexp : (t[@unboxed]) -> (int[@untagged]) -> (t[@unboxed])
= "caml_ldexp_float32_bytecode" "caml_ldexp_float32"
[@@noalloc]

external modf : float32 -> float32 * float32 = "caml_modf_float32"
external compare : float32 -> float32 -> int = "%compare"
external modf : t -> t * t = "caml_modf_float32"
external compare : t -> t -> int = "%compare"

let equal x y = compare x y = 0

let[@inline] min (x : float32) (y : float32) =
let[@inline] min (x : t) (y : t) =
if y > x || ((not (sign_bit y)) && sign_bit x) then if is_nan y then y else x
else if is_nan x then x
else y

let[@inline] max (x : float32) (y : float32) =
let[@inline] max (x : t) (y : t) =
if y > x || ((not (sign_bit y)) && sign_bit x) then if is_nan x then x else y
else if is_nan y then y
else x

let[@inline] min_max (x : float32) (y : float32) =
let[@inline] min_max (x : t) (y : t) =
if is_nan x || is_nan y then (nan, nan)
else if y > x || ((not (sign_bit y)) && sign_bit x) then (x, y)
else (y, x)

let[@inline] min_num (x : float32) (y : float32) =
let[@inline] min_num (x : t) (y : t) =
if y > x || ((not (sign_bit y)) && sign_bit x) then if is_nan x then y else x
else if is_nan y then x
else y

let[@inline] max_num (x : float32) (y : float32) =
let[@inline] max_num (x : t) (y : t) =
if y > x || ((not (sign_bit y)) && sign_bit x) then if is_nan y then x else y
else if is_nan x then y
else x

let[@inline] min_max_num (x : float32) (y : float32) =
let[@inline] min_max_num (x : t) (y : t) =
if is_nan x then (y, y)
else if is_nan y then (x, x)
else if y > x || ((not (sign_bit y)) && sign_bit x) then (x, y)
Expand Down
Loading
Loading