Carp/core/Float.carp
Scott Olsen 5d530b7491
feat: register MAX and MIN macros for stdint types (#1412)
* feat: register MAX and MIN macros for stdint types

Adds MAX and MIN for each INT<N> type and MAX for each UINT<N> type.
These are macros defined by stdint.h and are sometimes useful for bounds
determinations and conversions and such.

* feat: make MAX and MIN interfaces

Since several numeric types define maximum and minimum values, it makes
sense for these to be defined as interfaces. This commit also makes
existing definitions of MAX and MIN for Carp's numeric types implement
the interfaces.

* fix: respect let binding shadowing in memory management (#1413)

* fix: respect let binding shadowing in memory management

Previously, we didn't account for shadowing in let bindings in our
memory management routines. This led to rare situations in which
multiple deleters might be added for a single variable name, for
example:

```clojure
(defn n [xs]
  (let [xs [1 2 3]
        n &xs]
    n))
```

The borrow checker would fail on this code since it would assign `xs`
two deleters, one for the untyped argument and another for the let
binding.

Instead, we now perform *exclusive* ownership transfer for the duration
of the let scope--when a shadow is introduced, the previous deleters for
that variable name are dropped until the end of the let scope, since we
evaluate all instances of the shadowed name to the more local binding.
At the end of the let scope, the original deleter is restored.

Fixes issue #597

* refactor: improved dead reference error for let

Since let scopes resolve to their bodies, we can report the body of the
let as the xobj producing an error when a dead reference is returned.

* test: update error message for dead refs in let

* test: add regression test for issue #597

Ensure we don't regress and fail to manage memory when let bindings
shadow function argument names.

* fix: respect symbol modes on interface concretization (#1415)

* fix: respect symbol modes on interface concretization

When concretizing interfaces (finding the appropriate implementation at
a call site) we previously set the lookup mode of all such resolved
symbols to CarpLand AFunction. This incorrectly overwrites the lookup
mode of Externally registered types, causing them to emit incorrect C
when the user specifies an override.

We now preserve whatever lookup mode is assigned to the implementation
the concretization resolves the interface to. This not only fixes the
external override emission issue, but should be more correct in general.

fixes #1414

* test: add regression test for issue #1414
2022-04-13 09:32:46 +02:00

129 lines
3.3 KiB
Plaintext

(system-include "carp_float.h")
(doc Float "is a smaller numerical floating point type. Its literals are
suffixed with `f`.")
(defmodule Float
(def pi 3.1415926536f)
(register MAX Float "CARP_FLT_MAX")
(implements MAX MAX)
(register neg (Fn [Float] Float))
(register + (Fn [Float Float] Float))
(register - (Fn [Float Float] Float))
(register * (Fn [Float Float] Float))
(register / (Fn [Float Float] Float))
(register to-int (Fn [Float] Int))
(register to-bytes (Fn [Float] Int))
(register from-int (Fn [Int] Float))
(register copy (Fn [(Ref Float)] Float))
(register round (Fn [Float] Int))
(register = (Fn [Float Float] Bool))
(register < (Fn [Float Float] Bool))
(register > (Fn [Float Float] Bool))
(implements pi Float.pi)
(implements + Float.+)
(implements - Float.-)
(implements * Float.*)
(implements / Float./)
(implements < Float.<)
(implements > Float.>)
(implements = Float.=)
(implements copy Float.copy)
(implements neg Float.neg)
(implements to-int Float.to-int)
(implements from-int Float.from-int)
(defn clamp [min, max, val]
(if (> val max)
max
(if (< val min)
min
val)))
(doc approx "checks whether `x` and `y` are approximately equal.
The margin of error is 0.00001.")
(defn approx [x y]
(Generics.approx x y))
(register copy (Fn [(Ref Float)] Float))
(register abs (Fn [Float] Float))
(register acos (Fn [Float] Float))
(register asin (Fn [Float] Float))
(register atan (Fn [Float] Float))
(register atan2 (Fn [Float Float] Float))
(register ceil (Fn [Float] Float))
(register cos (Fn [Float] Float))
(register cosh (Fn [Float] Float))
(register exp (Fn [Float] Float))
(register floor (Fn [Float] Float))
(register frexp (Fn [Float (Ref Int)] Float))
(register ldexp (Fn [Float Int] Float))
(register log (Fn [Float] Float))
(register log10 (Fn [Float] Float))
(register mod (Fn [Float Float] Float))
(register modf (Fn [Float (Ref Float)] Float))
(register pow (Fn [Float Float] Float))
(register sin (Fn [Float] Float))
(register sinh (Fn [Float] Float))
(register sqrt (Fn [Float] Float))
(register tan (Fn [Float] Float))
(register tanh (Fn [Float] Float))
(implements abs Float.abs)
(implements acos Float.acos)
(implements asin Float.asin)
(implements atan Float.atan)
(implements atan2 Float.atan2)
(implements ceil Float.ceil)
(implements cos Float.cos)
(implements cosh Float.cosh)
(implements exp Float.exp)
(implements floor Float.floor)
(implements frexp Float.frexp)
(implements ldexp Float.ldexp)
(implements log Float.log)
(implements log10 Float.log10)
(implements mod Float.mod)
(implements modf Float.modf)
(implements pow Float.pow)
(implements sin Float.sin)
(implements sinh Float.sinh)
(implements sqrt Float.sqrt)
(implements tan Float.tan)
(implements tanh Float.tanh)
(doc zero "returns `0.0f`.")
(defn zero []
0.0f)
(implements zero Float.zero)
(defn inc [x]
(+ 1.0f x))
(implements inc Float.inc)
(defn dec [x]
(- x 1.0f))
(implements dec Float.dec)
(defn add-ref [x y]
(Float.+ @x @y))
(implements add-ref Float.add-ref)
)
(defmodule FloatRef
(defn = [a b]
(Float.= @a @b))
(implements = FloatRef.=)
(defn < [a b]
(Float.< @a @b))
(implements < FloatRef.<)
(defn > [a b]
(Float.> @a @b))
(implements > FloatRef.>)
)