1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use crate::simd::*;
use std::{
    cmp::Ordering,
    convert::TryFrom,
    error::Error,
    fmt,
    hash::{Hash, Hasher},
    result,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IntervalErrorKind {
    PossiblyUndefinedOperation,
    UndefinedOperation,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IntervalError {
    pub(crate) kind: IntervalErrorKind,
}

impl IntervalError {
    /// Returns the type of the error.
    pub fn kind(&self) -> IntervalErrorKind {
        self.kind
    }
}

impl fmt::Display for IntervalError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.kind {
            IntervalErrorKind::PossiblyUndefinedOperation => {
                write!(f, "possibly undefined operation")
            }
            IntervalErrorKind::UndefinedOperation => write!(f, "undefined operation"),
        }
    }
}

impl Error for IntervalError {}

/// An alias for [`Result<T, E>`](`result::Result`) with [`E = IntervalError`](`IntervalError`).
pub type Result<T> = result::Result<T, IntervalError>;

/// An interval with [`f64`] bounds.
///
/// It is sometimes referred to as a *bare* interval in contrast to a decorated interval ([`DecInterval`]).
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Interval {
    // An interval is stored in a SIMD vector in the neginf-sup-nan form:
    //
    // - An nonempty interval [a, b] is stored as [-a; b].
    // - An empty interval is stored as [NaN; NaN].
    //
    // Elements of SIMD vectors are separated by a semicolon to distinguish from interval bounds.
    //
    // Representations of zeros and NaNs are arbitrary; a zero can be either +0.0 or -0.0,
    // and a NaN can be either a qNaN or a sNaN with an arbitrary payload.
    //
    // In Debug formatting, the value of `rep` is printed as either
    // `__m128d(-a, b)` (on x86-64) or `float64x2_t(-a, b)` (on AArch64).
    pub(crate) rep: F64X2,
}

impl Interval {
    pub(crate) fn inf_raw(self) -> f64 {
        -extract0(self.rep)
    }

    pub(crate) fn sup_raw(self) -> f64 {
        extract1(self.rep)
    }

    pub(crate) fn with_infsup_raw(a: f64, b: f64) -> Self {
        Self {
            rep: constant(-a, b),
        }
    }

    pub(crate) fn zero() -> Self {
        Self { rep: splat(0.0) }
    }
}

impl PartialEq for Interval {
    fn eq(&self, rhs: &Self) -> bool {
        self.both_empty(*rhs) | all(eq(self.rep, rhs.rep))
    }
}

impl Eq for Interval {}

impl Hash for Interval {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.inf().to_bits().hash(state);
        self.sup().to_bits().hash(state);
    }
}

impl TryFrom<(f64, f64)> for Interval {
    type Error = IntervalError;

    fn try_from((a, b): (f64, f64)) -> Result<Self> {
        if a <= b && a != f64::INFINITY && b != f64::NEG_INFINITY {
            Ok(Self::with_infsup_raw(a, b))
        } else {
            Err(Self::Error {
                kind: IntervalErrorKind::UndefinedOperation,
            })
        }
    }
}

/// The decoration of a [`DecInterval`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(u8)]
pub enum Decoration {
    /// The β€œill-formed” decoration.
    Ill = 0,
    /// The β€œtrivial” decoration.
    Trv = 4,
    /// The β€œdefined” decoration.
    Def = 8,
    /// The β€œdefined and continuous” decoration.
    Dac = 12,
    /// The β€œcommon” decoration.
    Com = 16,
}

impl Ord for Decoration {
    fn cmp(&self, rhs: &Self) -> Ordering {
        (*self as u8).cmp(&(*rhs as u8))
    }
}

impl PartialOrd for Decoration {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        Some(self.cmp(rhs))
    }
}

/// The decorated version of [`Interval`].
///
/// ## Notes on equality comparison
///
/// By definition, a NaI is *not* equal to itself:
///
/// ```
/// use inari::*;
/// assert_ne!(DecInterval::NAI, DecInterval::NAI);
/// ```
///
/// For this reason, the traits [`Eq`] and [`Hash`] are not implemented for the type.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct DecInterval {
    pub(crate) x: Interval,
    pub(crate) d: Decoration,
}

impl DecInterval {
    /// Creates a [`DecInterval`] from the given interval and the decoration below:
    ///
    /// | Interval             | Decoration          |
    /// | -------------------- | ------------------- |
    /// | Nonempty and bounded | [`Decoration::Com`] |
    /// | Unbounded            | [`Decoration::Dac`] |
    /// | Empty                | [`Decoration::Trv`] |
    pub fn new(x: Interval) -> Self {
        use Decoration::*;

        let d = if x.is_empty() {
            Trv
        } else if !x.is_common_interval() {
            Dac
        } else {
            Com
        };

        Self::new_unchecked(x, d)
    }

    /// Creates a [`DecInterval`] from the given interval and decoration.
    /// If the decoration is invalid for the interval, the first one in the list is used:
    ///
    /// | Interval             | Valid decorations                                                                                       |
    /// | -------------------- | ------------------------------------------------------------------------------------------------------- |
    /// | Nonempty and bounded | [`Decoration::Com`], [`Decoration::Dac`], [`Decoration::Def`], [`Decoration::Trv`], [`Decoration::Ill`] |
    /// | Unbounded            | [`Decoration::Dac`], [`Decoration::Def`], [`Decoration::Trv`], [`Decoration::Ill`]                      |
    /// | Empty                | [`Decoration::Trv`], [`Decoration::Ill`]                                                                |
    pub fn set_dec(x: Interval, d: Decoration) -> Self {
        use Decoration::*;

        if d == Ill {
            Self::NAI
        } else if x.is_empty() {
            Self::EMPTY
        } else if d == Com && !x.is_common_interval() {
            Self::new_unchecked(x, Dac)
        } else {
            Self::new_unchecked(x, d)
        }
    }

    /// Returns the interval part of `self` if it is not NaI; otherwise, [`None`].
    pub fn interval(self) -> Option<Interval> {
        if self.is_nai() {
            return None;
        }

        Some(self.x)
    }

    /// Returns the decoration part `self`.
    pub fn decoration(self) -> Decoration {
        self.d
    }

    pub(crate) const fn new_unchecked(x: Interval, d: Decoration) -> Self {
        Self { x, d }
    }
}

impl PartialEq for DecInterval {
    fn eq(&self, rhs: &Self) -> bool {
        if self.is_nai() || rhs.is_nai() {
            return false;
        }

        self.x == rhs.x
    }
}

impl TryFrom<(f64, f64)> for DecInterval {
    type Error = IntervalError;

    fn try_from(x: (f64, f64)) -> Result<Self> {
        match Interval::try_from(x) {
            Ok(x) => Ok(Self::new(x)),
            _ => Err(Self::Error {
                kind: IntervalErrorKind::UndefinedOperation,
            }),
        }
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! _interval {
    ($a:expr, $b:expr) => {{
        use ::std::{convert::TryFrom, primitive::*};
        fn is_f64(_: f64) {}
        is_f64($a);
        is_f64($b);
        $crate::Interval::try_from(($a, $b))
    }};
}

#[cfg(not(feature = "gmp"))]
#[macro_export]
macro_rules! interval {
    ($a:expr, $b:expr) => {
        $crate::_interval!($a, $b)
    };
}

/// Creates an [`Interval`] from [`f64`] bounds or from a bare interval literal.
///
/// In case of a failure, it returns a [`Result<Interval>`] instead of an [`Interval`].
///
/// If the construction is invalid,
/// an [`Err`] with [`IntervalErrorKind::UndefinedOperation`] is returned.
/// If it cannot determine whether the construction is valid or not,
/// [`IntervalErrorKind::PossiblyUndefinedOperation`] is returned.
///
/// - `interval!(a, b)`
///
///   Creates an interval $\[a, b\]$, where `a` and `b` are [`f64`] values.
///
///   The conditions $a ≀ b$, $a < +∞$ and $b > -∞$ must be held.
///
/// - `interval!(s)`
///
///   Creates an interval from a bare interval literal. `s` must be a string slice ([`&str`]).
///
/// - `interval!(s, exact)`
///
///   Creates an interval $𝒙$ from the bare interval literal obtained by `format!("{:x}", x)`.
///   `s` must be a string slice ([`&str`]).
///
/// For creating a constant, the macro [`const_interval!`] should be preferred over this one.
#[cfg(feature = "gmp")]
#[macro_export]
macro_rules! interval {
    ($s:expr) => {{
        use ::std::primitive::*;
        fn is_str(_: &str) {}
        is_str($s);
        $s.parse::<$crate::Interval>()
    }};

    ($s:expr, exact) => {{
        use ::std::primitive::*;
        fn is_str(_: &str) {}
        is_str($s);
        $crate::Interval::_try_from_str_exact($s)
    }};

    ($a:expr, $b:expr) => {
        $crate::_interval!($a, $b)
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _dec_interval {
    ($a:expr, $b:expr) => {{
        use ::std::{convert::TryFrom, primitive::*};
        fn is_f64(_: f64) {}
        is_f64($a);
        is_f64($b);
        $crate::DecInterval::try_from(($a, $b))
    }};
}

#[cfg(not(feature = "gmp"))]
#[macro_export]
macro_rules! dec_interval {
    ($a:expr, $b:expr) => {
        $crate::_dec_interval!($a, $b)
    };
}

/// Creates a [`DecInterval`] from [`f64`] bounds or from a decorated interval literal.
///
/// In case of a failure, it returns a [`Result<DecInterval>`] instead of a [`DecInterval`].
///
/// If the construction is invalid,
/// an [`Err`] with [`IntervalErrorKind::UndefinedOperation`] is returned.
/// If it cannot determine whether the construction is valid or not,
/// [`IntervalErrorKind::PossiblyUndefinedOperation`] is returned.
///
/// - `dec_interval!(a, b)`
///
///   Creates a decorated interval $\[a, b\]$ with the strongest decoration,
///   where `a` and `b` are [`f64`] values.
///
///   The conditions $a ≀ b$, $a < +∞$ and $b > -∞$ must be held.
///
/// - `dec_interval!(s)`
///
///   Creates a decorated interval from a decorated interval literal.
///   `s` must be a string slice ([`&str`]).
///
/// For creating a constant, the macro [`const_dec_interval!`] should be preferred over this one.
#[cfg(feature = "gmp")]
#[macro_export]
macro_rules! dec_interval {
    ($s:expr) => {{
        use ::std::primitive::*;
        fn is_str(_: &str) {}
        is_str($s);
        $s.parse::<$crate::DecInterval>()
    }};

    ($a:expr, $b:expr) => {
        $crate::_dec_interval!($a, $b)
    };
}

/// Creates an [`Interval`] from [`f64`] bounds.
///
/// The macro can be used in [constant expressions](https://doc.rust-lang.org/reference/const_eval.html#constant-expressions).
///
/// The usage is almost the same as the macro [`interval!(a, b)`](`interval!`)
/// except that this macro returns an [`Interval`] directly,
/// or results in a compilation error if the construction is invalid.
#[macro_export]
macro_rules! const_interval {
    ($a:expr, $b:expr) => {{
        use ::std::{mem::transmute, primitive::*};

        ::static_assertions::const_assert!(
            $a <= $b && $a != f64::INFINITY && $b != f64::NEG_INFINITY
        );

        #[allow(unused_unsafe)]
        unsafe {
            // Parentheses are used to avoid `clippy::double_neg`.
            transmute::<_, $crate::Interval>([-($a), $b])
        }
    }};
}

/// Creates a [`DecInterval`] from [`f64`] bounds.
///
/// The macro can be used in [constant expressions](https://doc.rust-lang.org/reference/const_eval.html#constant-expressions).
///
/// The usage is almost the same as the macro [`dec_interval!(a, b)`](`dec_interval!`)
/// except that this macro returns a [`DecInterval`] directly,
/// or results in a compilation error if the construction is invalid.
#[macro_export]
macro_rules! const_dec_interval {
    ($a:expr, $b:expr) => {{
        use ::std::{mem::transmute, primitive::*};

        #[repr(C)]
        struct _DecInterval {
            x: $crate::Interval,
            d: $crate::Decoration,
        }

        #[allow(unused_unsafe)]
        unsafe {
            transmute::<_, $crate::DecInterval>(_DecInterval {
                x: $crate::const_interval!($a, $b),
                d: if $a == f64::NEG_INFINITY || $b == f64::INFINITY {
                    $crate::Decoration::Dac
                } else {
                    $crate::Decoration::Com
                },
            })
        }
    }};
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn decoration_order() {
        use Decoration::*;
        assert!(Ill < Trv);
        assert!(Trv < Def);
        assert!(Def < Dac);
        assert!(Dac < Com);
    }

    #[test]
    fn macros() {
        // Check that these macros are usable for constants.
        const _I: Interval = const_interval!(1.0, 2.0);
        const _DI: DecInterval = const_dec_interval!(1.0, 2.0);

        // Check that type inference works.
        let _i = const_interval!(1.0, 2.0);
        let _di = const_dec_interval!(1.0, 2.0);

        assert_eq!(interval!(1.0, 1.0).unwrap(), const_interval!(1.0, 1.0));
        assert_eq!(interval!(1.0, 2.0).unwrap(), const_interval!(1.0, 2.0));
        assert_eq!(
            interval!(f64::NEG_INFINITY, 1.0).unwrap(),
            const_interval!(f64::NEG_INFINITY, 1.0)
        );
        assert_eq!(
            interval!(1.0, f64::INFINITY).unwrap(),
            const_interval!(1.0, f64::INFINITY)
        );

        assert_eq!(
            dec_interval!(1.0, 1.0).unwrap(),
            const_dec_interval!(1.0, 1.0)
        );
        assert_eq!(
            dec_interval!(1.0, 2.0).unwrap(),
            const_dec_interval!(1.0, 2.0)
        );
        assert_eq!(
            dec_interval!(f64::NEG_INFINITY, 1.0).unwrap(),
            const_dec_interval!(f64::NEG_INFINITY, 1.0)
        );
        assert_eq!(
            dec_interval!(1.0, f64::INFINITY).unwrap(),
            const_dec_interval!(1.0, f64::INFINITY)
        );
    }
}