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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// TODO: transition math functions to `__quantum__rt` once compiler support is ready (https://github.com/microsoft/qsharp-compiler/issues/1557).

use rand::Rng;
use std::ffi::c_double;

use crate::strings::convert;

#[cfg(not(feature = "fail-support"))]
#[allow(improper_ctypes)]
extern "C" {
    fn __quantum__rt__fail(str: *const std::ffi::CString);
}

#[cfg(feature = "fail-support")]
use crate::__quantum__rt__fail;

#[no_mangle]
pub extern "C" fn __quantum__qis__nan__body() -> c_double {
    c_double::NAN
}

#[no_mangle]
pub extern "C" fn __quantum__qis__isnan__body(val: c_double) -> bool {
    val.is_nan()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__infinity__body() -> c_double {
    c_double::INFINITY
}

#[no_mangle]
pub extern "C" fn __quantum__qis__isinf__body(val: c_double) -> bool {
    val.is_infinite() && val.is_sign_positive()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__isnegativeinfinity__body(val: c_double) -> bool {
    val.is_infinite() && val.is_sign_negative()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__sin__body(val: c_double) -> c_double {
    val.sin()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__cos__body(val: c_double) -> c_double {
    val.cos()
}
#[no_mangle]
pub extern "C" fn __quantum__qis__tan__body(val: c_double) -> c_double {
    val.tan()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__arctan2__body(y: c_double, x: c_double) -> c_double {
    y.atan2(x)
}

#[no_mangle]
pub extern "C" fn __quantum__qis__sinh__body(val: c_double) -> c_double {
    val.sinh()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__cosh__body(val: c_double) -> c_double {
    val.cosh()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__tanh__body(val: c_double) -> c_double {
    val.tanh()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__arcsin__body(val: c_double) -> c_double {
    val.asin()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__arccos__body(val: c_double) -> c_double {
    val.acos()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__arctan__body(val: c_double) -> c_double {
    val.atan()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__sqrt__body(val: c_double) -> c_double {
    val.sqrt()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__log__body(val: c_double) -> c_double {
    val.ln()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__ieeeremainder__body(x: c_double, y: c_double) -> c_double {
    x - y * (x / y).round()
}

#[no_mangle]
pub extern "C" fn __quantum__qis__drawrandomint__body(min: i64, max: i64) -> i64 {
    if min > max {
        unsafe {
            __quantum__rt__fail(convert(&"Invalid Argument: minimum > maximum".to_string()));
        }
    }
    rand::thread_rng().gen_range(min..=max)
}

#[no_mangle]
pub extern "C" fn __quantum__qis__drawrandomdouble__body(min: c_double, max: c_double) -> f64 {
    if min > max {
        unsafe {
            __quantum__rt__fail(convert(&"Invalid Argument: minimum > maximum".to_string()));
        }
    }
    rand::thread_rng().gen_range(min..=max)
}