|
| 1 | +(**************************************************************************) |
| 2 | +(* *) |
| 3 | +(* OCaml *) |
| 4 | +(* *) |
| 5 | +(* Chris Casinghino, Jane Street, New York *) |
| 6 | +(* *) |
| 7 | +(* Copyright 2023 Jane Street Group LLC *) |
| 8 | +(* *) |
| 9 | +(* All rights reserved. This file is distributed under the terms of *) |
| 10 | +(* the GNU Lesser General Public License version 2.1, with the *) |
| 11 | +(* special exception on linking described in the file LICENSE. *) |
| 12 | +(* *) |
| 13 | +(**************************************************************************) |
| 14 | + |
| 15 | +open! Stdlib |
| 16 | + |
| 17 | +[@@@ocaml.flambda_o3] |
| 18 | + |
| 19 | +external box_float : float# -> (float[@local_opt]) = "%box_float" |
| 20 | + |
| 21 | +external unbox_float : (float[@local_opt]) -> float# = "%unbox_float" |
| 22 | + |
| 23 | +external box_int32 : int32# -> (int32[@local_opt]) = "%box_int32" |
| 24 | + |
| 25 | +external unbox_int32 : (int32[@local_opt]) -> int32# = "%unbox_int32" |
| 26 | + |
| 27 | +external to_float32 : float32# -> (float32[@local_opt]) = "%box_float32" |
| 28 | + |
| 29 | +external of_float32 : (float32[@local_opt]) -> float32# = "%unbox_float32" |
| 30 | + |
| 31 | +(* CR layouts: Investigate whether it's worth making these things externals. |
| 32 | + Are there situations where the middle-end won't inline them and remove the |
| 33 | + boxing/unboxing? *) |
| 34 | + |
| 35 | +let[@inline always] neg x = of_float32 (Float32.neg (to_float32 x)) |
| 36 | + |
| 37 | +let[@inline always] add x y = of_float32 (Float32.add (to_float32 x) (to_float32 y)) |
| 38 | + |
| 39 | +let[@inline always] sub x y = of_float32 (Float32.sub (to_float32 x) (to_float32 y)) |
| 40 | + |
| 41 | +let[@inline always] mul x y = of_float32 (Float32.mul (to_float32 x) (to_float32 y)) |
| 42 | + |
| 43 | +let[@inline always] div x y = of_float32 (Float32.div (to_float32 x) (to_float32 y)) |
| 44 | + |
| 45 | +let[@inline always] pow x y = of_float32 (Float32.pow (to_float32 x) (to_float32 y)) |
| 46 | + |
| 47 | +module Operators = struct |
| 48 | + let[@inline always] ( ~-. ) x = of_float32 (Float32.neg (to_float32 x)) |
| 49 | + |
| 50 | + let[@inline always] ( +. ) x y = of_float32 (Float32.add (to_float32 x) (to_float32 y)) |
| 51 | + |
| 52 | + let[@inline always] ( -. ) x y = of_float32 (Float32.sub (to_float32 x) (to_float32 y)) |
| 53 | + |
| 54 | + let[@inline always] ( *. ) x y = of_float32 (Float32.mul (to_float32 x) (to_float32 y)) |
| 55 | + |
| 56 | + let[@inline always] ( /. ) x y = of_float32 (Float32.div (to_float32 x) (to_float32 y)) |
| 57 | + |
| 58 | + let[@inline always] ( ** ) x y = of_float32 (Float32.pow (to_float32 x) (to_float32 y)) |
| 59 | +end |
| 60 | + |
| 61 | +let[@inline always] fma x y z = of_float32 (Float32.fma (to_float32 x) (to_float32 y) (to_float32 z)) |
| 62 | + |
| 63 | +let[@inline always] rem x y = of_float32 (Float32.rem (to_float32 x) (to_float32 y)) |
| 64 | + |
| 65 | +let[@inline always] succ x = of_float32 (Float32.succ (to_float32 x)) |
| 66 | + |
| 67 | +let[@inline always] pred x = of_float32 (Float32.pred (to_float32 x)) |
| 68 | + |
| 69 | +let[@inline always] abs x = of_float32 (Float32.abs (to_float32 x)) |
| 70 | + |
| 71 | +let[@inline always] is_finite x = Float32.is_finite (to_float32 x) |
| 72 | + |
| 73 | +let[@inline always] is_infinite x = Float32.is_infinite (to_float32 x) |
| 74 | + |
| 75 | +let[@inline always] is_nan x = Float32.is_nan (to_float32 x) |
| 76 | + |
| 77 | +let[@inline always] is_integer x = Float32.is_integer (to_float32 x) |
| 78 | + |
| 79 | +let[@inline always] of_int x = of_float32 (Float32.of_int x) |
| 80 | + |
| 81 | +let[@inline always] to_int x = Float32.to_int (to_float32 x) |
| 82 | + |
| 83 | +let[@inline always] of_float x = of_float32 (Float32.of_float (box_float x)) |
| 84 | + |
| 85 | +let[@inline always] to_float x = unbox_float (Float32.to_float (to_float32 x)) |
| 86 | + |
| 87 | +let[@inline always] of_bits x = of_float32 (Float32.of_bits (box_int32 x)) |
| 88 | + |
| 89 | +let[@inline always] to_bits x = unbox_int32 (Float32.to_bits (to_float32 x)) |
| 90 | + |
| 91 | +let[@inline always] of_string x = of_float32 (Float32.of_string x) |
| 92 | + |
| 93 | +let[@inline always] to_string x = Float32.to_string (to_float32 x) |
| 94 | + |
| 95 | +type fpclass = Stdlib.fpclass = |
| 96 | + FP_normal |
| 97 | + | FP_subnormal |
| 98 | + | FP_zero |
| 99 | + | FP_infinite |
| 100 | + | FP_nan |
| 101 | + |
| 102 | +let[@inline always] classify_float x = Float32.classify_float (to_float32 x) |
| 103 | + |
| 104 | +let[@inline always] sqrt x = of_float32 (Float32.sqrt (to_float32 x)) |
| 105 | + |
| 106 | +let[@inline always] cbrt x = of_float32 (Float32.cbrt (to_float32 x)) |
| 107 | + |
| 108 | +let[@inline always] exp x = of_float32 (Float32.exp (to_float32 x)) |
| 109 | + |
| 110 | +let[@inline always] exp2 x = of_float32 (Float32.exp2 (to_float32 x)) |
| 111 | + |
| 112 | +let[@inline always] log x = of_float32 (Float32.log (to_float32 x)) |
| 113 | + |
| 114 | +let[@inline always] log10 x = of_float32 (Float32.log10 (to_float32 x)) |
| 115 | + |
| 116 | +let[@inline always] log2 x = of_float32 (Float32.log2 (to_float32 x)) |
| 117 | + |
| 118 | +let[@inline always] expm1 x = of_float32 (Float32.expm1 (to_float32 x)) |
| 119 | + |
| 120 | +let[@inline always] log1p x = of_float32 (Float32.log1p (to_float32 x)) |
| 121 | + |
| 122 | +let[@inline always] cos x = of_float32 (Float32.cos (to_float32 x)) |
| 123 | + |
| 124 | +let[@inline always] sin x = of_float32 (Float32.sin (to_float32 x)) |
| 125 | + |
| 126 | +let[@inline always] tan x = of_float32 (Float32.tan (to_float32 x)) |
| 127 | + |
| 128 | +let[@inline always] acos x = of_float32 (Float32.acos (to_float32 x)) |
| 129 | + |
| 130 | +let[@inline always] asin x = of_float32 (Float32.asin (to_float32 x)) |
| 131 | + |
| 132 | +let[@inline always] atan x = of_float32 (Float32.atan (to_float32 x)) |
| 133 | + |
| 134 | +let[@inline always] atan2 x y = of_float32 (Float32.atan2 (to_float32 x) (to_float32 y)) |
| 135 | + |
| 136 | +let[@inline always] hypot x y = of_float32 (Float32.hypot (to_float32 x) (to_float32 y)) |
| 137 | + |
| 138 | +let[@inline always] cosh x = of_float32 (Float32.cosh (to_float32 x)) |
| 139 | + |
| 140 | +let[@inline always] sinh x = of_float32 (Float32.sinh (to_float32 x)) |
| 141 | + |
| 142 | +let[@inline always] tanh x = of_float32 (Float32.tanh (to_float32 x)) |
| 143 | + |
| 144 | +let[@inline always] acosh x = of_float32 (Float32.acosh (to_float32 x)) |
| 145 | + |
| 146 | +let[@inline always] asinh x = of_float32 (Float32.asinh (to_float32 x)) |
| 147 | + |
| 148 | +let[@inline always] atanh x = of_float32 (Float32.atanh (to_float32 x)) |
| 149 | + |
| 150 | +let[@inline always] erf x = of_float32 (Float32.erf (to_float32 x)) |
| 151 | + |
| 152 | +let[@inline always] erfc x = of_float32 (Float32.erfc (to_float32 x)) |
| 153 | + |
| 154 | +let[@inline always] trunc x = of_float32 (Float32.trunc (to_float32 x)) |
| 155 | + |
| 156 | +let[@inline always] round x = of_float32 (Float32.round (to_float32 x)) |
| 157 | + |
| 158 | +let[@inline always] ceil x = of_float32 (Float32.ceil (to_float32 x)) |
| 159 | + |
| 160 | +let[@inline always] floor x = of_float32 (Float32.floor (to_float32 x)) |
| 161 | + |
| 162 | +let[@inline always] next_after x y = of_float32 (Float32.next_after (to_float32 x) (to_float32 y)) |
| 163 | + |
| 164 | +let[@inline always] copy_sign x y = of_float32 (Float32.copy_sign (to_float32 x) (to_float32 y)) |
| 165 | + |
| 166 | +let[@inline always] sign_bit x = Float32.sign_bit (to_float32 x) |
| 167 | + |
| 168 | +let[@inline always] ldexp x i = of_float32 (Float32.ldexp (to_float32 x) i) |
| 169 | + |
| 170 | +type t = float32# |
| 171 | + |
| 172 | +let[@inline always] compare x y = Float32.compare (to_float32 x) (to_float32 y) |
| 173 | + |
| 174 | +let[@inline always] equal x y = Float32.equal (to_float32 x) (to_float32 y) |
| 175 | + |
| 176 | +let[@inline always] min x y = of_float32 (Float32.min (to_float32 x) (to_float32 y)) |
| 177 | + |
| 178 | +let[@inline always] max x y = of_float32 (Float32.max (to_float32 x) (to_float32 y)) |
| 179 | + |
| 180 | +let[@inline always] min_num x y = of_float32 (Float32.min_num (to_float32 x) (to_float32 y)) |
| 181 | + |
| 182 | +let[@inline always] max_num x y = of_float32 (Float32.max_num (to_float32 x) (to_float32 y)) |
0 commit comments