Skip to content

Commit 1d62e22

Browse files
authored
Merge pull request #597 from scratchcpp/llvm_math_instructions
LLVM: Implement math instructions
2 parents 3f651dc + edb28bf commit 1d62e22

File tree

11 files changed

+1529
-88
lines changed

11 files changed

+1529
-88
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ class LIBSCRATCHCPP_EXPORT Compiler
6161
void createOr();
6262
void createNot();
6363

64+
void createMod();
65+
void createRound();
66+
void createAbs();
67+
void createFloor();
68+
void createCeil();
69+
void createSqrt();
70+
void createSin();
71+
void createCos();
72+
void createTan();
73+
void createAsin();
74+
void createAcos();
75+
void createAtan();
76+
void createLn();
77+
void createLog10();
78+
void createExp();
79+
void createExp10();
80+
6481
void moveToIf(std::shared_ptr<Block> substack);
6582
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
6683
void moveToRepeatLoop(std::shared_ptr<Block> substack);

src/dev/engine/compiler.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,102 @@ void Compiler::createNot()
160160
impl->builder->createNot();
161161
}
162162

163+
/*! Creates a remainder operation using the last 2 values. */
164+
void Compiler::createMod()
165+
{
166+
impl->builder->createMod();
167+
}
168+
169+
/*! Creates a round operation using the last value. */
170+
void Compiler::createRound()
171+
{
172+
impl->builder->createRound();
173+
}
174+
175+
/*! Creates an abs operation using the last value. */
176+
void Compiler::createAbs()
177+
{
178+
impl->builder->createAbs();
179+
}
180+
181+
/*! Creates a floor operation using the last value. */
182+
void Compiler::createFloor()
183+
{
184+
impl->builder->createFloor();
185+
}
186+
187+
/*! Creates a ceiling operation using the last value. */
188+
void Compiler::createCeil()
189+
{
190+
impl->builder->createCeil();
191+
}
192+
193+
/*! Creates a square root operation using the last value. */
194+
void Compiler::createSqrt()
195+
{
196+
impl->builder->createSqrt();
197+
}
198+
199+
/*! Creates a sin operation using the last value. */
200+
void Compiler::createSin()
201+
{
202+
impl->builder->createSin();
203+
}
204+
205+
/*! Creates a cos operation using the last value. */
206+
void Compiler::createCos()
207+
{
208+
impl->builder->createCos();
209+
}
210+
211+
/*! Creates a tan operation using the last value. */
212+
void Compiler::createTan()
213+
{
214+
impl->builder->createTan();
215+
}
216+
217+
/*! Creates an asin operation using the last value. */
218+
void Compiler::createAsin()
219+
{
220+
impl->builder->createAsin();
221+
}
222+
223+
/*! Creates an acos operation using the last value. */
224+
void Compiler::createAcos()
225+
{
226+
impl->builder->createAcos();
227+
}
228+
229+
/*! Creates an atan operation using the last value. */
230+
void Compiler::createAtan()
231+
{
232+
impl->builder->createAtan();
233+
}
234+
235+
/*! Creates an ln operation using the last value. */
236+
void Compiler::createLn()
237+
{
238+
impl->builder->createLn();
239+
}
240+
241+
/*! Creates a log10 operation using the last value. */
242+
void Compiler::createLog10()
243+
{
244+
impl->builder->createLog10();
245+
}
246+
247+
/*! Creates an e^x operation using the last value. */
248+
void Compiler::createExp()
249+
{
250+
impl->builder->createExp();
251+
}
252+
253+
/*! Creates a 10^x operation using the last value. */
254+
void Compiler::createExp10()
255+
{
256+
impl->builder->createExp10();
257+
}
258+
163259
/*! Jumps to the given if substack. */
164260
void Compiler::moveToIf(std::shared_ptr<Block> substack)
165261
{

src/dev/engine/internal/icodebuilder.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ class ICodeBuilder
3737
virtual void createOr() = 0;
3838
virtual void createNot() = 0;
3939

40+
virtual void createMod() = 0;
41+
virtual void createRound() = 0;
42+
virtual void createAbs() = 0;
43+
virtual void createFloor() = 0;
44+
virtual void createCeil() = 0;
45+
virtual void createSqrt() = 0;
46+
virtual void createSin() = 0;
47+
virtual void createCos() = 0;
48+
virtual void createTan() = 0;
49+
virtual void createAsin() = 0;
50+
virtual void createAcos() = 0;
51+
virtual void createAtan() = 0;
52+
virtual void createLn() = 0;
53+
virtual void createLog10() = 0;
54+
virtual void createExp() = 0;
55+
virtual void createExp10() = 0;
56+
4057
virtual void beginIfStatement() = 0;
4158
virtual void beginElseBranch() = 0;
4259
virtual void endIf() = 0;

src/dev/engine/internal/llvmcodebuilder.cpp

Lines changed: 294 additions & 0 deletions
Large diffs are not rendered by default.

src/dev/engine/internal/llvmcodebuilder.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ class LLVMCodeBuilder : public ICodeBuilder
3939
void createOr() override;
4040
void createNot() override;
4141

42+
void createMod() override;
43+
void createRound() override;
44+
void createAbs() override;
45+
void createFloor() override;
46+
void createCeil() override;
47+
void createSqrt() override;
48+
void createSin() override;
49+
void createCos() override;
50+
void createTan() override;
51+
void createAsin() override;
52+
void createAcos() override;
53+
void createAtan() override;
54+
void createLn() override;
55+
void createLog10() override;
56+
void createExp() override;
57+
void createExp10() override;
58+
4259
void beginIfStatement() override;
4360
void beginElseBranch() override;
4461
void endIf() override;
@@ -81,6 +98,22 @@ class LLVMCodeBuilder : public ICodeBuilder
8198
And,
8299
Or,
83100
Not,
101+
Mod,
102+
Round,
103+
Abs,
104+
Floor,
105+
Ceil,
106+
Sqrt,
107+
Sin,
108+
Cos,
109+
Tan,
110+
Asin,
111+
Acos,
112+
Atan,
113+
Ln,
114+
Log10,
115+
Exp,
116+
Exp10,
84117
Yield,
85118
BeginIf,
86119
BeginElse,

src/scratch/value_functions.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,9 @@ extern "C"
354354
/*! Calculates the modulo the given values and writes the result to dst. */
355355
void value_mod(const libscratchcpp::ValueData *v1, const libscratchcpp::ValueData *v2, ValueData *dst)
356356
{
357-
double a = value_toDouble(v1);
358-
double b = value_toDouble(v2);
359-
360-
if ((b == 0) || std::isinf(a))
361-
value_assign_double(dst, std::numeric_limits<double>::quiet_NaN());
362-
else if (std::isinf(b))
363-
value_assign_double(dst, value_toDouble(v1));
364-
else if (value_isNegative(v1) || value_isNegative(v2))
365-
value_assign_double(dst, fmod(value_toDouble(v2) + fmod(value_toDouble(v1), -value_toDouble(v2)), value_toDouble(v2)));
366-
else
367-
value_assign_double(dst, fmod(value_toDouble(v1), value_toDouble(v2)));
357+
const double a = value_toDouble(v1);
358+
const double b = value_toDouble(v2);
359+
value_assign_double(dst, fmod(a, b) / b < 0.0 ? fmod(a, b) + b : fmod(a, b));
368360
}
369361

370362
/* comparison */

src/scratch/value_functions_p.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern "C"
5050
return v < 0 && std::isinf(v);
5151
}
5252

53-
inline long value_convert_int_str(const char *str, int n, bool *ok)
53+
inline double value_convert_int_str(const char *str, int n, bool *ok)
5454
{
5555
if (ok)
5656
*ok = false;
@@ -80,7 +80,7 @@ extern "C"
8080
*ok = true;
8181

8282
if (isNegative)
83-
return -ret;
83+
return -static_cast<double>(ret); // for negative zero
8484
else
8585
return ret;
8686
}

0 commit comments

Comments
 (0)