2013-08-14 06:02:15 +00:00
|
|
|
#include <string.h>
|
2015-01-16 01:27:17 +00:00
|
|
|
|
|
|
|
#include "ast.h"
|
|
|
|
#include "fold.h"
|
2013-08-14 06:02:15 +00:00
|
|
|
#include "parser.h"
|
2013-03-09 08:57:37 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
lex_ctx_t intrin::ctx() const {
|
|
|
|
return parser_ctx(m_parser);
|
|
|
|
}
|
2013-10-02 17:00:34 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_function *intrin::value(ast_value **out, const char *name, qcint_t vtype) {
|
|
|
|
ast_value *value = nullptr;
|
2015-01-15 20:18:33 +00:00
|
|
|
ast_function *func = nullptr;
|
2015-01-15 23:11:41 +00:00
|
|
|
char buffer[1024];
|
|
|
|
char stype [1024];
|
2013-10-02 17:00:34 +00:00
|
|
|
|
|
|
|
util_snprintf(buffer, sizeof(buffer), "__builtin_%s", name);
|
|
|
|
util_snprintf(stype, sizeof(stype), "<%s>", type_name[vtype]);
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
value = ast_value_new(ctx(), buffer, TYPE_FUNCTION);
|
|
|
|
value->intrinsic = true;
|
|
|
|
value->expression.next = (ast_expression*)ast_value_new(ctx(), stype, vtype);
|
|
|
|
func = ast_function_new(ctx(), buffer, value);
|
2013-10-16 04:04:39 +00:00
|
|
|
value->expression.flags |= AST_FLAG_ERASEABLE;
|
2013-10-02 17:00:34 +00:00
|
|
|
|
2013-10-16 04:04:39 +00:00
|
|
|
*out = value;
|
2013-10-02 17:00:34 +00:00
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
void intrin::reg(ast_value *const value, ast_function *const func) {
|
|
|
|
m_parser->functions.push_back(func);
|
|
|
|
m_parser->globals.push_back((ast_expression*)value);
|
2013-10-02 17:00:34 +00:00
|
|
|
}
|
2013-03-09 08:57:37 +00:00
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
#define QC_POW_EPSILON 0.00001f
|
2013-03-09 08:57:37 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::nullfunc() {
|
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_function *func = value(&val, nullptr, TYPE_VOID);
|
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-23 12:21:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::isfinite_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float isfinite(float x) {
|
|
|
|
* return !(isnan(x) || isinf(x));
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_function *func = value(&val, "isfinite", TYPE_FLOAT);
|
|
|
|
ast_call *callisnan = ast_call_new(ctx(), func_self("isnan", "isfinite"));
|
|
|
|
ast_call *callisinf = ast_call_new(ctx(), func_self("isinf", "isfinite"));
|
|
|
|
ast_block *block = ast_block_new(ctx());
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* float x; */
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* <callisnan> = isnan(x); */
|
2015-01-15 04:45:00 +00:00
|
|
|
callisnan->params.push_back((ast_expression*)x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* <callisinf> = isinf(x); */
|
2015-01-15 04:45:00 +00:00
|
|
|
callisinf->params.push_back((ast_expression*)x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* return (!<callisnan> || <callisinf>); */
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_unary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_NOT_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_OR,
|
|
|
|
(ast_expression*)callisnan,
|
|
|
|
(ast_expression*)callisinf
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(block);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
return (ast_expression*)val;;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::isinf_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float isinf(float x) {
|
|
|
|
* return (x != 0.0) && (x + x == x);
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "isinf", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_AND,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_NE_F,
|
|
|
|
(ast_expression*)x,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-24 02:14:13 +00:00
|
|
|
),
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_EQ_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)x
|
|
|
|
),
|
|
|
|
(ast_expression*)x
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::isnan_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float isnan(float x) {
|
|
|
|
* float local;
|
|
|
|
* local = x;
|
|
|
|
*
|
|
|
|
* return (x != local);
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *arg1 = ast_value_new(ctx(), "x",TYPE_FLOAT);
|
|
|
|
ast_value *local = ast_value_new(ctx(), "local", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "isnan", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->locals.push_back(local);
|
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)local,
|
|
|
|
(ast_expression*)arg1
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_NE_F,
|
|
|
|
(ast_expression*)arg1,
|
|
|
|
(ast_expression*)local
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(arg1);
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::isnormal_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float isnormal(float x) {
|
|
|
|
* return isfinite(x);
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_call *callisfinite = ast_call_new(ctx(), func_self("isfinite", "isnormal"));
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "isnormal", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2015-01-15 04:45:00 +00:00
|
|
|
callisfinite->params.push_back((ast_expression*)x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* return <callisfinite> */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)callisfinite
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::signbit_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float signbit(float x) {
|
|
|
|
* return (x < 0);
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "signbit", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* return (x < 0); */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_ternary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_LT,
|
|
|
|
(ast_expression*)x,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-24 02:14:13 +00:00
|
|
|
),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-24 02:14:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::acosh_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float acosh(float x) {
|
|
|
|
* return log(x + sqrt((x * x) - 1));
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_call *calllog = ast_call_new(ctx(), func_self("log", "acosh"));
|
|
|
|
ast_call *callsqrt = ast_call_new(ctx(), func_self("sqrt", "acosh"));
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "acosh", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* <callsqrt> = sqrt((x * x) - 1); */
|
2015-01-15 04:45:00 +00:00
|
|
|
callsqrt->params.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_SUB_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)x
|
|
|
|
),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-24 02:14:13 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* <calllog> = log(x + <callsqrt>); */
|
2015-01-15 04:45:00 +00:00
|
|
|
calllog->params.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)callsqrt
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* return <calllog>; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)calllog
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::asinh_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float asinh(float x) {
|
|
|
|
* return log(x + sqrt((x * x) + 1));
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_call *calllog = ast_call_new(ctx(), func_self("log", "asinh"));
|
|
|
|
ast_call *callsqrt = ast_call_new(ctx(), func_self("sqrt", "asinh"));
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "asinh", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* <callsqrt> = sqrt((x * x) + 1); */
|
2015-01-15 04:45:00 +00:00
|
|
|
callsqrt->params.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)x
|
|
|
|
),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-24 02:14:13 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* <calllog> = log(x + <callsqrt>); */
|
2015-01-15 04:45:00 +00:00
|
|
|
calllog->params.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)callsqrt
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* return <calllog>; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)calllog
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::atanh_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float atanh(float x) {
|
|
|
|
* return 0.5 * log((1 + x) / (1 - x))
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_call *calllog = ast_call_new(ctx(), func_self("log", "atanh"));
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "atanh", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* <callog> = log((1 + x) / (1 - x)); */
|
2015-01-15 04:45:00 +00:00
|
|
|
calllog->params.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_ADD_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)x
|
|
|
|
),
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_SUB_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)x
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* return 0.5 * <calllog>; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_MUL_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->constgen_float(0.5, false),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)calllog
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::exp_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float exp(float x) {
|
|
|
|
* float sum = 1.0;
|
|
|
|
* float acc = 1.0;
|
|
|
|
* float i;
|
|
|
|
* for (i = 1; i < 200; ++i)
|
|
|
|
* sum += (acc *= x / i);
|
|
|
|
*
|
|
|
|
* return sum;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_value *sum = ast_value_new(ctx(), "sum", TYPE_FLOAT);
|
|
|
|
ast_value *acc = ast_value_new(ctx(), "acc", TYPE_FLOAT);
|
|
|
|
ast_value *i = ast_value_new(ctx(), "i", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "exp", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2015-01-15 04:34:43 +00:00
|
|
|
|
|
|
|
body->locals.push_back(sum);
|
|
|
|
body->locals.push_back(acc);
|
|
|
|
body->locals.push_back(i);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* sum = 1.0; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)sum,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-24 02:14:13 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* acc = 1.0; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)acc,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-24 02:14:13 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* for (i = 1; i < 200; ++i)
|
|
|
|
* sum += (acc *= x / i);
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_loop_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
/* i = 1; */
|
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)i,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-24 02:14:13 +00:00
|
|
|
),
|
|
|
|
/* i < 200; */
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_LT,
|
|
|
|
(ast_expression*)i,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->constgen_float(200.0f, false)
|
2013-11-24 02:14:13 +00:00
|
|
|
),
|
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-24 02:14:13 +00:00
|
|
|
false,
|
|
|
|
/* ++i; */
|
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)i,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-24 02:14:13 +00:00
|
|
|
),
|
|
|
|
/* sum += (acc *= (x / i)) */
|
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)sum,
|
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)acc,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)i
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* return sum; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)sum
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::exp2_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float exp2(float x) {
|
|
|
|
* return pow(2, x);
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_call *callpow = ast_call_new(ctx(), func_self("pow", "exp2"));
|
|
|
|
ast_value *arg1 = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "exp2", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(arg1);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-16 01:27:17 +00:00
|
|
|
callpow->params.push_back((ast_expression*)m_fold->m_imm_float[3]);
|
2015-01-15 04:45:00 +00:00
|
|
|
callpow->params.push_back((ast_expression*)arg1);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* return <callpow> */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)callpow
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::expm1_() {
|
2013-11-24 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* float expm1(float x) {
|
|
|
|
* return exp(x) - 1;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_call *callexp = ast_call_new(ctx(), func_self("exp", "expm1"));
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "expm1", TYPE_FLOAT);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* <callexp> = exp(x); */
|
2015-01-15 04:45:00 +00:00
|
|
|
callexp->params.push_back((ast_expression*)x);
|
2013-11-24 02:14:13 +00:00
|
|
|
|
|
|
|
/* return <callexp> - 1; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-24 02:14:13 +00:00
|
|
|
INSTR_SUB_F,
|
|
|
|
(ast_expression*)callexp,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-24 02:14:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-24 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::pow_() {
|
2013-03-09 09:20:54 +00:00
|
|
|
/*
|
2013-11-23 11:40:27 +00:00
|
|
|
*
|
|
|
|
* float pow(float base, float exp) {
|
|
|
|
* float result;
|
|
|
|
* float low;
|
|
|
|
* float high;
|
|
|
|
* float mid;
|
|
|
|
* float square;
|
|
|
|
* float accumulate;
|
|
|
|
*
|
|
|
|
* if (exp == 0.0)
|
2013-11-23 16:14:42 +00:00
|
|
|
* return 1;
|
|
|
|
* if (exp == 1.0)
|
2013-11-23 11:40:27 +00:00
|
|
|
* return base;
|
|
|
|
* if (exp < 0)
|
|
|
|
* return 1.0 / pow(base, -exp);
|
|
|
|
* if (exp >= 1) {
|
|
|
|
* result = pow(base, exp / 2);
|
|
|
|
* return result * result;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* low = 0.0f;
|
|
|
|
* high = 1.0f;
|
|
|
|
* square = sqrt(base);
|
|
|
|
* accumulate = square;
|
|
|
|
* mid = high / 2.0f
|
|
|
|
*
|
|
|
|
* while (fabs(mid - exp) > QC_POW_EPSILON) {
|
|
|
|
* square = sqrt(square);
|
|
|
|
* if (mid < exp) {
|
|
|
|
* low = mid;
|
|
|
|
* accumulate *= square;
|
|
|
|
* } else {
|
|
|
|
* high = mid;
|
|
|
|
* accumulate *= (1.0f / square);
|
|
|
|
* }
|
|
|
|
* mid = (low + high) / 2;
|
2013-03-09 09:20:54 +00:00
|
|
|
* }
|
2013-11-23 11:40:27 +00:00
|
|
|
* return accumulate;
|
2013-04-21 10:24:55 +00:00
|
|
|
* }
|
2013-03-09 09:20:54 +00:00
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_function *func = value(&val, "pow", TYPE_FLOAT);
|
2013-11-23 11:40:27 +00:00
|
|
|
|
|
|
|
/* prepare some calls for later */
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_call *callpow1 = ast_call_new(ctx(), (ast_expression*)val); /* for pow(base, -exp) */
|
|
|
|
ast_call *callpow2 = ast_call_new(ctx(), (ast_expression*)val); /* for pow(vase, exp / 2) */
|
|
|
|
ast_call *callsqrt1 = ast_call_new(ctx(), func_self("sqrt", "pow")); /* for sqrt(base) */
|
|
|
|
ast_call *callsqrt2 = ast_call_new(ctx(), func_self("sqrt", "pow")); /* for sqrt(square) */
|
|
|
|
ast_call *callfabs = ast_call_new(ctx(), func_self("fabs", "pow")); /* for fabs(mid - exp) */
|
2013-11-23 11:40:27 +00:00
|
|
|
|
|
|
|
/* prepare some blocks for later */
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_block *expgt1 = ast_block_new(ctx());
|
|
|
|
ast_block *midltexp = ast_block_new(ctx());
|
|
|
|
ast_block *midltexpelse = ast_block_new(ctx());
|
|
|
|
ast_block *whileblock = ast_block_new(ctx());
|
2013-11-23 11:40:27 +00:00
|
|
|
|
|
|
|
/* float pow(float base, float exp) */
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *base = ast_value_new(ctx(), "base", TYPE_FLOAT);
|
|
|
|
ast_value *exp = ast_value_new(ctx(), "exp", TYPE_FLOAT);
|
2013-11-23 11:40:27 +00:00
|
|
|
/* { */
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_block *body = ast_block_new(ctx());
|
2013-10-16 04:04:39 +00:00
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* float result;
|
|
|
|
* float low;
|
|
|
|
* float high;
|
|
|
|
* float square;
|
|
|
|
* float accumulate;
|
|
|
|
* float mid;
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *result = ast_value_new(ctx(), "result", TYPE_FLOAT);
|
|
|
|
ast_value *low = ast_value_new(ctx(), "low", TYPE_FLOAT);
|
|
|
|
ast_value *high = ast_value_new(ctx(), "high", TYPE_FLOAT);
|
|
|
|
ast_value *square = ast_value_new(ctx(), "square", TYPE_FLOAT);
|
|
|
|
ast_value *accumulate = ast_value_new(ctx(), "accumulate", TYPE_FLOAT);
|
|
|
|
ast_value *mid = ast_value_new(ctx(), "mid", TYPE_FLOAT);
|
2015-01-15 04:34:43 +00:00
|
|
|
body->locals.push_back(result);
|
|
|
|
body->locals.push_back(low);
|
|
|
|
body->locals.push_back(high);
|
|
|
|
body->locals.push_back(square);
|
|
|
|
body->locals.push_back(accumulate);
|
|
|
|
body->locals.push_back(mid);
|
2013-11-23 11:40:27 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(base);
|
|
|
|
val->expression.params.push_back(exp);
|
2013-10-16 04:04:39 +00:00
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* if (exp == 0.0)
|
2013-11-23 16:14:42 +00:00
|
|
|
* return 1;
|
2013-11-23 11:40:27 +00:00
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_EQ_F,
|
|
|
|
(ast_expression*)exp,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-23 11:40:27 +00:00
|
|
|
),
|
2013-11-23 16:14:42 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-23 16:14:42 +00:00
|
|
|
),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr
|
2013-11-23 16:14:42 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if (exp == 1.0)
|
|
|
|
* return base;
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 16:14:42 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 16:14:42 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 16:14:42 +00:00
|
|
|
INSTR_EQ_F,
|
|
|
|
(ast_expression*)exp,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-23 16:14:42 +00:00
|
|
|
),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)base
|
|
|
|
),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr
|
2013-11-23 11:40:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* <callpow1> = pow(base, -exp) */
|
2015-01-15 04:45:00 +00:00
|
|
|
callpow1->params.push_back((ast_expression*)base);
|
|
|
|
callpow1->params.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_unary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
VINSTR_NEG_F,
|
|
|
|
(ast_expression*)exp
|
|
|
|
)
|
|
|
|
);
|
2013-10-16 04:04:39 +00:00
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* if (exp < 0)
|
|
|
|
* return 1.0 / <callpow1>;
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_LT,
|
|
|
|
(ast_expression*)exp,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-23 11:40:27 +00:00
|
|
|
),
|
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_DIV_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)callpow1
|
|
|
|
)
|
|
|
|
),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr
|
2013-11-23 11:40:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* <callpow2> = pow(base, exp / 2) */
|
2015-01-15 04:45:00 +00:00
|
|
|
callpow2->params.push_back((ast_expression*)base);
|
|
|
|
callpow2->params.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)exp,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[3] /* 2.0f */
|
2013-11-23 11:40:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* <expgt1> = {
|
|
|
|
* result = <callpow2>;
|
|
|
|
* return result * result;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
expgt1->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_STORE_F,
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)result,
|
|
|
|
(ast_expression*)callpow2
|
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
expgt1->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)result,
|
|
|
|
(ast_expression*)result
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if (exp >= 1) {
|
|
|
|
* <expgt1>
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_GE,
|
|
|
|
(ast_expression*)exp,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-23 11:40:27 +00:00
|
|
|
),
|
|
|
|
(ast_expression*)expgt1,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* <callsqrt1> = sqrt(base)
|
|
|
|
*/
|
2015-01-15 04:45:00 +00:00
|
|
|
callsqrt1->params.push_back((ast_expression*)base);
|
2013-11-23 11:40:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* low = 0.0f;
|
|
|
|
* high = 1.0f;
|
|
|
|
* square = sqrt(base);
|
|
|
|
* accumulate = square;
|
|
|
|
* mid = high / 2.0f;
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2015-01-15 23:11:41 +00:00
|
|
|
(ast_expression*)ast_store_new(ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)low,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-23 11:40:27 +00:00
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_STORE_F,
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)high,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-23 11:40:27 +00:00
|
|
|
)
|
|
|
|
);
|
2013-11-25 07:11:45 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)square,
|
|
|
|
(ast_expression*)callsqrt1
|
|
|
|
)
|
|
|
|
);
|
2013-11-25 07:11:45 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)accumulate,
|
|
|
|
(ast_expression*)square
|
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)mid,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)high,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[3] /* 2.0f */
|
2013-11-23 11:40:27 +00:00
|
|
|
)
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* <midltexp> = {
|
|
|
|
* low = mid;
|
|
|
|
* accumulate *= square;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
midltexp->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)low,
|
|
|
|
(ast_expression*)mid
|
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
midltexp->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_MUL_F,
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)accumulate,
|
|
|
|
(ast_expression*)square
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* <midltexpelse> = {
|
|
|
|
* high = mid;
|
|
|
|
* accumulate *= (1.0 / square);
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
midltexpelse->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)high,
|
|
|
|
(ast_expression*)mid
|
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
midltexpelse->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)accumulate,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_DIV_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)square
|
|
|
|
)
|
|
|
|
)
|
2013-10-16 04:04:39 +00:00
|
|
|
);
|
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* <callsqrt2> = sqrt(square)
|
|
|
|
*/
|
2015-01-15 04:45:00 +00:00
|
|
|
callsqrt2->params.push_back((ast_expression*)square);
|
2013-10-16 04:04:39 +00:00
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* <whileblock> = {
|
|
|
|
* square = <callsqrt2>;
|
|
|
|
* if (mid < exp)
|
|
|
|
* <midltexp>;
|
|
|
|
* else
|
|
|
|
* <midltexpelse>;
|
|
|
|
*
|
|
|
|
* mid = (low + high) / 2;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
whileblock->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_STORE_F,
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)square,
|
|
|
|
(ast_expression*)callsqrt2
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
whileblock->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_LT,
|
|
|
|
(ast_expression*)mid,
|
|
|
|
(ast_expression*)exp
|
|
|
|
),
|
|
|
|
(ast_expression*)midltexp,
|
|
|
|
(ast_expression*)midltexpelse
|
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
whileblock->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_STORE_F,
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)mid,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)low,
|
|
|
|
(ast_expression*)high
|
|
|
|
),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[3] /* 2.0f */
|
2013-11-23 11:40:27 +00:00
|
|
|
)
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* <callabs> = fabs(mid - exp)
|
|
|
|
*/
|
2015-01-15 04:45:00 +00:00
|
|
|
callfabs->params.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_SUB_F,
|
|
|
|
(ast_expression*)mid,
|
|
|
|
(ast_expression*)exp
|
|
|
|
)
|
2013-10-16 04:04:39 +00:00
|
|
|
);
|
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/*
|
|
|
|
* while (<callfabs> > epsilon)
|
|
|
|
* <whileblock>
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_loop_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
/* init */
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-23 11:40:27 +00:00
|
|
|
/* pre condition */
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
INSTR_GT,
|
|
|
|
(ast_expression*)callfabs,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->constgen_float(QC_POW_EPSILON, false)
|
2013-11-23 11:40:27 +00:00
|
|
|
),
|
|
|
|
/* pre not */
|
|
|
|
false,
|
|
|
|
/* post condition */
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-23 11:40:27 +00:00
|
|
|
/* post not */
|
|
|
|
false,
|
|
|
|
/* increment expression */
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-23 11:40:27 +00:00
|
|
|
/* code block */
|
|
|
|
(ast_expression*)whileblock
|
|
|
|
)
|
|
|
|
);
|
2013-10-16 04:04:39 +00:00
|
|
|
|
2013-11-23 16:15:28 +00:00
|
|
|
/* return accumulate */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)accumulate
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-23 11:40:27 +00:00
|
|
|
/* } */
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-03-09 08:57:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::mod_() {
|
2013-03-09 09:20:54 +00:00
|
|
|
/*
|
2013-11-23 08:53:07 +00:00
|
|
|
* float mod(float a, float b) {
|
|
|
|
* float div = a / b;
|
|
|
|
* float sign = (div < 0.0f) ? -1 : 1;
|
|
|
|
* return a - b * sign * floor(sign * div);
|
2013-03-09 09:20:54 +00:00
|
|
|
* }
|
2013-04-21 10:24:55 +00:00
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_call *call = ast_call_new(ctx(), func_self("floor", "mod"));
|
|
|
|
ast_value *a = ast_value_new(ctx(), "a", TYPE_FLOAT);
|
|
|
|
ast_value *b = ast_value_new(ctx(), "b", TYPE_FLOAT);
|
|
|
|
ast_value *div = ast_value_new(ctx(), "div", TYPE_FLOAT);
|
|
|
|
ast_value *sign = ast_value_new(ctx(), "sign", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "mod", TYPE_FLOAT);
|
|
|
|
|
|
|
|
val->expression.params.push_back(a);
|
|
|
|
val->expression.params.push_back(b);
|
2013-11-23 08:53:07 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->locals.push_back(div);
|
|
|
|
body->locals.push_back(sign);
|
2013-11-23 08:53:07 +00:00
|
|
|
|
|
|
|
/* div = a / b; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 08:53:07 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 08:53:07 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)div,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 08:53:07 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)a,
|
|
|
|
(ast_expression*)b
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* sign = (div < 0.0f) ? -1 : 1; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-23 08:53:07 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 08:53:07 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)sign,
|
|
|
|
(ast_expression*)ast_ternary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 08:53:07 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 08:53:07 +00:00
|
|
|
INSTR_LT,
|
|
|
|
(ast_expression*)div,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-23 08:53:07 +00:00
|
|
|
),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[2],
|
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-23 08:53:07 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* floor(sign * div) */
|
2015-01-15 04:45:00 +00:00
|
|
|
call->params.push_back(
|
2013-11-23 08:53:07 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 08:53:07 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)sign,
|
|
|
|
(ast_expression*)div
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-23 08:53:07 +00:00
|
|
|
/* return a - b * sign * <call> */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-10-16 04:04:39 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_SUB_F,
|
2013-11-23 08:53:07 +00:00
|
|
|
(ast_expression*)a,
|
2013-03-09 08:57:37 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_MUL_F,
|
2013-11-23 08:53:07 +00:00
|
|
|
(ast_expression*)b,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 08:53:07 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)sign,
|
|
|
|
(ast_expression*)call
|
|
|
|
)
|
2013-03-09 08:57:37 +00:00
|
|
|
)
|
|
|
|
)
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
2013-03-09 08:57:37 +00:00
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-03-09 09:20:54 +00:00
|
|
|
}
|
2013-08-28 15:28:27 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::fabs_() {
|
2013-08-28 15:28:27 +00:00
|
|
|
/*
|
|
|
|
* float fabs(float x) {
|
|
|
|
* return x < 0 ? -x : x;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *arg1 = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "fabs", TYPE_FLOAT);
|
2013-10-16 04:04:39 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-10-16 04:04:39 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
(ast_expression*)ast_ternary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-10-16 04:04:39 +00:00
|
|
|
INSTR_LE,
|
|
|
|
(ast_expression*)arg1,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-10-16 04:04:39 +00:00
|
|
|
),
|
2013-11-23 11:40:27 +00:00
|
|
|
(ast_expression*)ast_unary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-23 11:40:27 +00:00
|
|
|
VINSTR_NEG_F,
|
2013-08-28 15:28:27 +00:00
|
|
|
(ast_expression*)arg1
|
2013-10-16 04:04:39 +00:00
|
|
|
),
|
|
|
|
(ast_expression*)arg1
|
2013-08-28 15:28:27 +00:00
|
|
|
)
|
2013-10-16 04:04:39 +00:00
|
|
|
)
|
|
|
|
);
|
2013-08-28 15:28:27 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(arg1);
|
2015-01-15 04:34:43 +00:00
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-08-28 15:28:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::epsilon_() {
|
2013-11-25 16:26:16 +00:00
|
|
|
/*
|
|
|
|
* float epsilon(void) {
|
|
|
|
* float eps = 1.0f;
|
|
|
|
* do { eps /= 2.0f; } while ((1.0f + (eps / 2.0f)) != 1.0f);
|
|
|
|
* return eps;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *eps = ast_value_new(ctx(), "eps", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, "epsilon", TYPE_FLOAT);
|
2013-11-25 16:26:16 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->locals.push_back(eps);
|
2013-11-25 16:26:16 +00:00
|
|
|
|
|
|
|
/* eps = 1.0f; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)eps,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-25 16:26:16 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_loop_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2013-11-25 16:26:16 +00:00
|
|
|
false,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_NE_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_ADD_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)eps,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[3] /* 2.0f */
|
2013-11-25 16:26:16 +00:00
|
|
|
)
|
|
|
|
),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-25 16:26:16 +00:00
|
|
|
),
|
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)eps,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[3] /* 2.0f */
|
2013-11-25 16:26:16 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* return eps; */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)eps
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-25 16:26:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::nan_() {
|
2013-11-25 16:26:16 +00:00
|
|
|
/*
|
|
|
|
* float nan(void) {
|
|
|
|
* float x = 0.0f;
|
|
|
|
* return x / x;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_function *func = value(&val, "nan", TYPE_FLOAT);
|
|
|
|
ast_block *block = ast_block_new(ctx());
|
2013-11-25 16:26:16 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
block->locals.push_back(x);
|
2013-11-25 16:26:16 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)x,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-25 16:26:16 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)x
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(block);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-25 16:26:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::inf_() {
|
2013-11-25 16:26:16 +00:00
|
|
|
/*
|
2013-11-25 19:08:05 +00:00
|
|
|
* float inf(void) {
|
2013-11-25 16:26:16 +00:00
|
|
|
* float x = 1.0f;
|
|
|
|
* float y = 0.0f;
|
|
|
|
* return x / y;
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *x = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_value *y = ast_value_new(ctx(), "y", TYPE_FLOAT);
|
|
|
|
ast_function *func = value(&val, "inf", TYPE_FLOAT);
|
|
|
|
ast_block *block = ast_block_new(ctx());
|
2013-11-25 16:26:16 +00:00
|
|
|
size_t i;
|
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
block->locals.push_back(x);
|
|
|
|
block->locals.push_back(y);
|
2013-11-25 16:26:16 +00:00
|
|
|
|
|
|
|
/* to keep code size down */
|
|
|
|
for (i = 0; i <= 1; i++) {
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)((i == 0) ? x : y),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[i]
|
2013-11-25 16:26:16 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 16:26:16 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)x,
|
|
|
|
(ast_expression*)y
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(block);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-25 16:26:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::ln_() {
|
2013-11-25 18:21:27 +00:00
|
|
|
/*
|
|
|
|
* float log(float power, float base) {
|
|
|
|
* float whole;
|
|
|
|
* float nth
|
|
|
|
* float sign = 1.0f;
|
|
|
|
* float eps = epsilon();
|
|
|
|
*
|
|
|
|
* if (power <= 1.0f || bbase <= 1.0) {
|
|
|
|
* if (power <= 0.0f || base <= 0.0f)
|
|
|
|
* return nan();
|
|
|
|
*
|
|
|
|
* if (power < 1.0f) {
|
|
|
|
* power = 1.0f / power;
|
|
|
|
* sign *= -1.0f;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* if (base < 1.0f) {
|
|
|
|
* sign *= -1.0f;
|
|
|
|
* base = 1.0f / base;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* float A_i = 1;
|
|
|
|
* float B_i = 0;
|
|
|
|
* float A_iminus1 = 0;
|
|
|
|
* float B_iminus1 = 1;
|
|
|
|
*
|
|
|
|
* for (;;) {
|
|
|
|
* whole = power;
|
|
|
|
* nth = 0.0f;
|
|
|
|
*
|
|
|
|
* while (whole >= base) {
|
2013-11-25 19:08:05 +00:00
|
|
|
* float base2 = base;
|
|
|
|
* float n2 = 1.0f;
|
|
|
|
* float newbase2 = base2 * base2;
|
|
|
|
*
|
|
|
|
* while (whole >= newbase2) {
|
|
|
|
* base2 = newbase2;
|
|
|
|
* n2 *= 2;
|
|
|
|
* newbase2 *= newbase2;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* whole /= base2;
|
|
|
|
* nth += n2;
|
2013-11-25 18:21:27 +00:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* float b_iplus1 = n;
|
|
|
|
* float A_iplus1 = b_iplus1 * A_i + A_iminus1;
|
|
|
|
* float B_iplus1 = b_iplus1 * B_i + B_iminus1;
|
|
|
|
*
|
|
|
|
* A_iminus1 = A_i;
|
|
|
|
* B_iminus1 = B_i;
|
|
|
|
* A_i = A_iplus1;
|
|
|
|
* B_i = B_iplus1;
|
|
|
|
*
|
|
|
|
* if (whole <= 1.0f + eps)
|
|
|
|
* break;
|
|
|
|
*
|
|
|
|
* power = base;
|
|
|
|
* bower = whole;
|
|
|
|
* }
|
|
|
|
* return sign * A_i / B_i;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_value *power = ast_value_new(ctx(), "power", TYPE_FLOAT);
|
|
|
|
ast_value *base = ast_value_new(ctx(), "base",TYPE_FLOAT);
|
|
|
|
ast_value *whole= ast_value_new(ctx(), "whole", TYPE_FLOAT);
|
|
|
|
ast_value *nth = ast_value_new(ctx(), "nth", TYPE_FLOAT);
|
|
|
|
ast_value *sign = ast_value_new(ctx(), "sign", TYPE_FLOAT);
|
|
|
|
ast_value *A_i = ast_value_new(ctx(), "A_i", TYPE_FLOAT);
|
|
|
|
ast_value *B_i = ast_value_new(ctx(), "B_i", TYPE_FLOAT);
|
|
|
|
ast_value *A_iminus1 = ast_value_new(ctx(), "A_iminus1", TYPE_FLOAT);
|
|
|
|
ast_value *B_iminus1 = ast_value_new(ctx(), "B_iminus1", TYPE_FLOAT);
|
|
|
|
ast_value *b_iplus1 = ast_value_new(ctx(), "b_iplus1", TYPE_FLOAT);
|
|
|
|
ast_value *A_iplus1 = ast_value_new(ctx(), "A_iplus1", TYPE_FLOAT);
|
|
|
|
ast_value *B_iplus1 = ast_value_new(ctx(), "B_iplus1", TYPE_FLOAT);
|
|
|
|
ast_value *eps = ast_value_new(ctx(), "eps", TYPE_FLOAT);
|
|
|
|
ast_value *base2 = ast_value_new(ctx(), "base2", TYPE_FLOAT);
|
|
|
|
ast_value *n2 = ast_value_new(ctx(), "n2",TYPE_FLOAT);
|
|
|
|
ast_value *newbase2 = ast_value_new(ctx(), "newbase2", TYPE_FLOAT);
|
|
|
|
ast_block *block = ast_block_new(ctx());
|
|
|
|
ast_block *plt1orblt1 = ast_block_new(ctx()); // (power <= 1.0f || base <= 1.0f)
|
|
|
|
ast_block *plt1 = ast_block_new(ctx()); // (power < 1.0f)
|
|
|
|
ast_block *blt1 = ast_block_new(ctx()); // (base< 1.0f)
|
|
|
|
ast_block *forloop = ast_block_new(ctx()); // for(;;)
|
|
|
|
ast_block *whileloop = ast_block_new(ctx()); // while (whole >= base)
|
|
|
|
ast_block *nestwhile= ast_block_new(ctx()); // while (whole >= newbase2)
|
|
|
|
ast_function *func = value(&val, "ln", TYPE_FLOAT);
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
val->expression.params.push_back(power);
|
|
|
|
val->expression.params.push_back(base);
|
2013-11-25 18:21:27 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
block->locals.push_back(whole);
|
|
|
|
block->locals.push_back(nth);
|
|
|
|
block->locals.push_back(sign);
|
|
|
|
block->locals.push_back(eps);
|
|
|
|
block->locals.push_back(A_i);
|
|
|
|
block->locals.push_back(B_i);
|
|
|
|
block->locals.push_back(A_iminus1);
|
|
|
|
block->locals.push_back(B_iminus1);
|
2013-11-25 18:21:27 +00:00
|
|
|
|
|
|
|
/* sign = 1.0f; */
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)sign,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* eps = __builtin_epsilon(); */
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)eps,
|
|
|
|
(ast_expression*)ast_call_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
|
|
|
func_self("__builtin_epsilon", "ln")
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A_i = 1;
|
|
|
|
* B_i = 0;
|
|
|
|
* A_iminus1 = 0;
|
|
|
|
* B_iminus1 = 1;
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= 1; i++) {
|
|
|
|
int j;
|
|
|
|
for (j = 1; j >= 0; j--) {
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)((j) ? ((i) ? B_iminus1 : A_i)
|
|
|
|
: ((i) ? A_iminus1 : B_i)),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[j]
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* <plt1> = {
|
|
|
|
* power = 1.0f / power;
|
|
|
|
* sign *= -1.0f;
|
|
|
|
* }
|
|
|
|
* <blt1> = {
|
|
|
|
* base = 1.0f / base;
|
|
|
|
* sign *= -1.0f;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= 1; i++) {
|
2015-01-15 04:34:43 +00:00
|
|
|
((i) ? blt1 : plt1)->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)((i) ? base : power),
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_DIV_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)((i) ? base : power)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2015-01-15 04:34:43 +00:00
|
|
|
plt1->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)sign,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[2]
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* <plt1orblt1> = {
|
|
|
|
* if (power <= 0.0 || base <= 0.0f)
|
|
|
|
* return __builtin_nan();
|
|
|
|
* if (power < 1.0f)
|
|
|
|
* <plt1>
|
|
|
|
* if (base < 1.0f)
|
|
|
|
* <blt1>
|
|
|
|
* }
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
plt1orblt1->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_OR,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_LE,
|
|
|
|
(ast_expression*)power,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-25 18:21:27 +00:00
|
|
|
),
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_LE,
|
|
|
|
(ast_expression*)base,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_call_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
|
|
|
func_self("__builtin_nan", "ln")
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
for (i = 0; i <= 1; i++) {
|
2015-01-15 04:34:43 +00:00
|
|
|
plt1orblt1->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_LT,
|
|
|
|
(ast_expression*)((i) ? base : power),
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-25 18:21:27 +00:00
|
|
|
),
|
|
|
|
(ast_expression*)((i) ? blt1 : plt1),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back((ast_expression*)plt1orblt1);
|
2013-11-25 18:21:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* whole = power; */
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)whole,
|
|
|
|
(ast_expression*)power
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* nth = 0.0f; */
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)nth,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[0]
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-25 19:08:05 +00:00
|
|
|
/* base2 = base; */
|
2015-01-15 04:34:43 +00:00
|
|
|
whileloop->exprs.push_back(
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)base2,
|
|
|
|
(ast_expression*)base
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* n2 = 1.0f; */
|
2015-01-15 04:34:43 +00:00
|
|
|
whileloop->exprs.push_back(
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)n2,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1]
|
2013-11-25 19:08:05 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* newbase2 = base2 * base2; */
|
2015-01-15 04:34:43 +00:00
|
|
|
whileloop->exprs.push_back(
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)newbase2,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)base2,
|
|
|
|
(ast_expression*)base2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* while loop locals */
|
2015-01-15 04:34:43 +00:00
|
|
|
whileloop->locals.push_back(base2);
|
|
|
|
whileloop->locals.push_back(n2);
|
|
|
|
whileloop->locals.push_back(newbase2);
|
2013-11-25 19:08:05 +00:00
|
|
|
|
|
|
|
/* base2 = newbase2; */
|
2015-01-15 04:34:43 +00:00
|
|
|
nestwhile->exprs.push_back(
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)base2,
|
|
|
|
(ast_expression*)newbase2
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* n2 *= 2; */
|
2015-01-15 04:34:43 +00:00
|
|
|
nestwhile->exprs.push_back(
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)n2,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[3] /* 2.0f */
|
2013-11-25 19:08:05 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* newbase2 *= newbase2; */
|
2015-01-15 04:34:43 +00:00
|
|
|
nestwhile->exprs.push_back(
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)newbase2,
|
|
|
|
(ast_expression*)newbase2
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* while (whole >= newbase2) */
|
2015-01-15 04:34:43 +00:00
|
|
|
whileloop->exprs.push_back(
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_loop_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 19:08:05 +00:00
|
|
|
INSTR_GE,
|
|
|
|
(ast_expression*)whole,
|
|
|
|
(ast_expression*)newbase2
|
|
|
|
),
|
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 19:08:05 +00:00
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)nestwhile
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* whole /= base2; */
|
2015-01-15 04:34:43 +00:00
|
|
|
whileloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)whole,
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)base2
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-11-25 19:08:05 +00:00
|
|
|
/* nth += n2; */
|
2015-01-15 04:34:43 +00:00
|
|
|
whileloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binstore_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)nth,
|
2013-11-25 19:08:05 +00:00
|
|
|
(ast_expression*)n2
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* while (whole >= base) */
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_loop_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_GE,
|
|
|
|
(ast_expression*)whole,
|
|
|
|
(ast_expression*)base
|
|
|
|
),
|
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 18:21:27 +00:00
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)whileloop
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->locals.push_back(b_iplus1);
|
|
|
|
forloop->locals.push_back(A_iplus1);
|
|
|
|
forloop->locals.push_back(B_iplus1);
|
2013-11-25 18:21:27 +00:00
|
|
|
|
|
|
|
/* b_iplus1 = nth; */
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)b_iplus1,
|
|
|
|
(ast_expression*)nth
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A_iplus1 = b_iplus1 * A_i + A_iminus1;
|
|
|
|
* B_iplus1 = b_iplus1 * B_i + B_iminus1;
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= 1; i++) {
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)((i) ? B_iplus1 : A_iplus1),
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_ADD_F,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)b_iplus1,
|
|
|
|
(ast_expression*) ((i) ? B_i : A_i)
|
|
|
|
),
|
|
|
|
(ast_expression*)((i) ? B_iminus1 : A_iminus1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A_iminus1 = A_i;
|
|
|
|
* B_iminus1 = B_i;
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= 1; i++) {
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)((i) ? B_iminus1 : A_iminus1),
|
|
|
|
(ast_expression*)((i) ? B_i : A_i)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A_i = A_iplus1;
|
|
|
|
* B_i = B_iplus1;
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= 1; i++) {
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)((i) ? B_i : A_i),
|
|
|
|
(ast_expression*)((i) ? B_iplus1 : A_iplus1)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if (whole <= 1.0f + eps)
|
|
|
|
* break;
|
|
|
|
*/
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_ifthen_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_LE,
|
|
|
|
(ast_expression*)whole,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_ADD_F,
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)eps
|
|
|
|
)
|
|
|
|
),
|
|
|
|
(ast_expression*)ast_breakcont_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
false,
|
|
|
|
0
|
|
|
|
),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr
|
2013-11-25 18:21:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* power = base;
|
|
|
|
* base = whole;
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= 1; i++) {
|
2015-01-15 04:34:43 +00:00
|
|
|
forloop->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_store_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_STORE_F,
|
|
|
|
(ast_expression*)((i) ? base : power),
|
|
|
|
(ast_expression*)((i) ? whole : base)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the for loop block */
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_loop_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
|
|
|
/* for(; 1; ) ?? (can this be nullptr too?) */
|
2015-01-16 01:27:17 +00:00
|
|
|
(ast_expression*)m_fold->m_imm_float[1],
|
2013-11-25 18:21:27 +00:00
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 18:21:27 +00:00
|
|
|
false,
|
2015-01-15 20:18:33 +00:00
|
|
|
nullptr,
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)forloop
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* return sign * A_i / B_il */
|
2015-01-15 04:34:43 +00:00
|
|
|
block->exprs.push_back(
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_MUL_F,
|
|
|
|
(ast_expression*)sign,
|
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-11-25 18:21:27 +00:00
|
|
|
INSTR_DIV_F,
|
|
|
|
(ast_expression*)A_i,
|
|
|
|
(ast_expression*)B_i
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(block);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-11-25 18:21:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::log_variant(const char *name, float base) {
|
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_call *callln = ast_call_new (ctx(), func_self("__builtin_ln", name));
|
|
|
|
ast_value *arg1 = ast_value_new(ctx(), "x", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, name, TYPE_FLOAT);
|
2013-12-03 22:40:15 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(arg1);
|
2013-12-03 22:40:15 +00:00
|
|
|
|
2015-01-15 04:45:00 +00:00
|
|
|
callln->params.push_back((ast_expression*)arg1);
|
2015-01-16 01:27:17 +00:00
|
|
|
callln->params.push_back((ast_expression*)m_fold->constgen_float(base, false));
|
2013-12-03 22:40:15 +00:00
|
|
|
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-12-03 22:40:15 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2013-12-03 22:40:15 +00:00
|
|
|
(ast_expression*)callln
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-12-03 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::log_() {
|
|
|
|
return log_variant("log", 2.7182818284590452354);
|
2013-12-03 22:40:15 +00:00
|
|
|
}
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::log10_() {
|
|
|
|
return log_variant("log10", 10);
|
2013-12-03 22:40:15 +00:00
|
|
|
}
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::log2_() {
|
|
|
|
return log_variant("log2", 2);
|
2013-12-03 22:40:15 +00:00
|
|
|
}
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::logb_() {
|
2013-12-03 22:40:15 +00:00
|
|
|
/* FLT_RADIX == 2 for now */
|
2015-01-15 23:11:41 +00:00
|
|
|
return log_variant("log2", 2);
|
2013-12-03 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::shift_variant(const char *name, size_t instr) {
|
2014-01-07 17:58:53 +00:00
|
|
|
/*
|
|
|
|
* float [shift] (float a, float b) {
|
|
|
|
* return floor(a [instr] pow(2, b));
|
|
|
|
*/
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_value *val = nullptr;
|
|
|
|
ast_call *callpow = ast_call_new(ctx(), func_self("pow", name));
|
|
|
|
ast_call *callfloor = ast_call_new(ctx(), func_self("floor", name));
|
|
|
|
ast_value *a = ast_value_new(ctx(), "a", TYPE_FLOAT);
|
|
|
|
ast_value *b = ast_value_new(ctx(), "b", TYPE_FLOAT);
|
|
|
|
ast_block *body = ast_block_new(ctx());
|
|
|
|
ast_function *func = value(&val, name, TYPE_FLOAT);
|
2013-12-03 22:40:15 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
val->expression.params.push_back(a);
|
|
|
|
val->expression.params.push_back(b);
|
2013-12-03 22:40:15 +00:00
|
|
|
|
2014-01-07 17:58:53 +00:00
|
|
|
/* <callpow> = pow(2, b) */
|
2015-01-16 01:27:17 +00:00
|
|
|
callpow->params.push_back((ast_expression*)m_fold->m_imm_float[3]);
|
2015-01-15 04:45:00 +00:00
|
|
|
callpow->params.push_back((ast_expression*)b);
|
2013-12-03 22:40:15 +00:00
|
|
|
|
2014-01-07 17:58:53 +00:00
|
|
|
/* <callfloor> = floor(a [instr] <callpow>) */
|
2015-01-15 04:45:00 +00:00
|
|
|
callfloor->params.push_back(
|
2014-01-07 17:58:53 +00:00
|
|
|
(ast_expression*)ast_binary_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2014-01-07 17:58:53 +00:00
|
|
|
instr,
|
|
|
|
(ast_expression*)a,
|
|
|
|
(ast_expression*)callpow
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* return <callfloor> */
|
2015-01-15 04:34:43 +00:00
|
|
|
body->exprs.push_back(
|
2013-12-03 22:40:15 +00:00
|
|
|
(ast_expression*)ast_return_new(
|
2015-01-15 23:11:41 +00:00
|
|
|
ctx(),
|
2014-01-07 17:58:53 +00:00
|
|
|
(ast_expression*)callfloor
|
2013-12-03 22:40:15 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-01-15 19:15:44 +00:00
|
|
|
func->blocks.push_back(body);
|
2015-01-15 23:11:41 +00:00
|
|
|
reg(val, func);
|
|
|
|
return (ast_expression*)val;
|
2013-12-03 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::lshift() {
|
|
|
|
return shift_variant("lshift", INSTR_MUL_F);
|
2013-12-03 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::rshift() {
|
|
|
|
return shift_variant("rshift", INSTR_DIV_F);
|
2013-08-14 03:03:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
void intrin::error(const char *fmt, ...) {
|
2013-08-14 06:02:15 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2015-01-15 23:11:41 +00:00
|
|
|
vcompile_error(ctx(), fmt, ap);
|
2013-08-14 06:02:15 +00:00
|
|
|
va_end(ap);
|
2013-04-14 23:51:16 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 06:02:15 +00:00
|
|
|
/* exposed */
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::debug_typestring() {
|
|
|
|
return (ast_expression*)0x1;
|
|
|
|
}
|
2013-10-16 04:04:39 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
intrin::intrin(parser_t *parser)
|
|
|
|
: m_parser(parser)
|
2015-01-16 01:27:17 +00:00
|
|
|
, m_fold(&parser->m_fold)
|
2015-01-15 23:11:41 +00:00
|
|
|
{
|
|
|
|
static const intrin_func_t intrinsics[] = {
|
|
|
|
{&intrin::isfinite_, "__builtin_isfinite", "isfinite", 1},
|
|
|
|
{&intrin::isinf_, "__builtin_isinf", "isinf", 1},
|
|
|
|
{&intrin::isnan_, "__builtin_isnan", "isnan", 1},
|
|
|
|
{&intrin::isnormal_, "__builtin_isnormal", "isnormal", 1},
|
|
|
|
{&intrin::signbit_, "__builtin_signbit", "signbit", 1},
|
|
|
|
{&intrin::acosh_, "__builtin_acosh", "acosh", 1},
|
|
|
|
{&intrin::asinh_, "__builtin_asinh", "asinh", 1},
|
|
|
|
{&intrin::atanh_, "__builtin_atanh", "atanh", 1},
|
|
|
|
{&intrin::exp_, "__builtin_exp", "exp", 1},
|
|
|
|
{&intrin::exp2_, "__builtin_exp2", "exp2", 1},
|
|
|
|
{&intrin::expm1_, "__builtin_expm1", "expm1", 1},
|
|
|
|
{&intrin::mod_, "__builtin_mod", "mod", 2},
|
|
|
|
{&intrin::pow_, "__builtin_pow", "pow", 2},
|
|
|
|
{&intrin::fabs_, "__builtin_fabs", "fabs", 1},
|
|
|
|
{&intrin::log_, "__builtin_log", "log", 1},
|
|
|
|
{&intrin::log10_, "__builtin_log10", "log10", 1},
|
|
|
|
{&intrin::log2_, "__builtin_log2", "log2", 1},
|
|
|
|
{&intrin::logb_, "__builtin_logb", "logb", 1},
|
|
|
|
{&intrin::lshift, "__builtin_lshift", "", 2},
|
|
|
|
{&intrin::rshift, "__builtin_rshift", "", 2},
|
|
|
|
{&intrin::epsilon_, "__builtin_epsilon", "", 0},
|
|
|
|
{&intrin::nan_, "__builtin_nan", "", 0},
|
|
|
|
{&intrin::inf_, "__builtin_inf", "", 0},
|
|
|
|
{&intrin::ln_, "__builtin_ln", "", 2},
|
|
|
|
{&intrin::debug_typestring, "__builtin_debug_typestring", "", 0},
|
|
|
|
{&intrin::nullfunc, "#nullfunc", "", 0}
|
|
|
|
};
|
2013-04-14 23:51:16 +00:00
|
|
|
|
2015-01-15 08:01:26 +00:00
|
|
|
for (auto &it : intrinsics) {
|
2015-01-15 23:11:41 +00:00
|
|
|
m_intrinsics.push_back(it);
|
|
|
|
m_generated.push_back(nullptr);
|
2015-01-15 08:01:26 +00:00
|
|
|
}
|
2013-08-14 06:02:15 +00:00
|
|
|
}
|
2013-03-09 08:57:37 +00:00
|
|
|
|
2015-01-16 01:27:17 +00:00
|
|
|
ast_expression *intrin::do_fold(ast_value *val, ast_expression **exprs) {
|
2015-01-15 23:11:41 +00:00
|
|
|
if (!val || !val->name)
|
2015-01-15 20:18:33 +00:00
|
|
|
return nullptr;
|
2015-01-15 23:11:41 +00:00
|
|
|
static constexpr size_t kPrefixLength = 10; // "__builtin_"
|
|
|
|
for (auto &it : m_intrinsics) {
|
|
|
|
if (!strcmp(val->name, it.name))
|
2015-01-15 08:01:26 +00:00
|
|
|
return (vec_size(exprs) != it.args)
|
2015-01-15 20:18:33 +00:00
|
|
|
? nullptr
|
2015-01-16 01:27:17 +00:00
|
|
|
: m_fold->intrinsic(val->name + kPrefixLength, exprs);
|
2015-01-15 08:01:26 +00:00
|
|
|
}
|
2015-01-15 20:18:33 +00:00
|
|
|
return nullptr;
|
2013-10-16 04:04:39 +00:00
|
|
|
}
|
2013-08-28 16:46:22 +00:00
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::func_try(size_t offset, const char *compare) {
|
|
|
|
for (auto &it : m_intrinsics) {
|
|
|
|
const size_t index = &it - &m_intrinsics[0];
|
2015-01-15 08:01:26 +00:00
|
|
|
if (strcmp(*(char **)((char *)&it + offset), compare))
|
2013-10-16 04:04:39 +00:00
|
|
|
continue;
|
2015-01-15 23:11:41 +00:00
|
|
|
if (m_generated[index])
|
|
|
|
return m_generated[index];
|
|
|
|
return m_generated[index] = (this->*it.intrin_func_t::function)();
|
2013-08-30 11:12:16 +00:00
|
|
|
}
|
2015-01-15 08:01:26 +00:00
|
|
|
return nullptr;
|
2013-08-28 16:46:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::func_self(const char *name, const char *from) {
|
2015-01-15 06:35:56 +00:00
|
|
|
ast_expression *find;
|
2013-08-14 06:02:15 +00:00
|
|
|
/* try current first */
|
2015-01-15 23:11:41 +00:00
|
|
|
if ((find = parser_find_global(m_parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
|
|
|
|
for (auto &it : m_parser->functions)
|
2015-01-15 06:35:56 +00:00
|
|
|
if (((ast_value*)find)->name && !strcmp(it->name, ((ast_value*)find)->name) && it->builtin < 0)
|
2013-10-16 04:04:39 +00:00
|
|
|
return find;
|
2013-08-14 06:02:15 +00:00
|
|
|
/* try name second */
|
2015-01-15 23:11:41 +00:00
|
|
|
if ((find = func_try(offsetof(intrin_func_t, name), name)))
|
2013-10-16 04:04:39 +00:00
|
|
|
return find;
|
2013-08-14 06:02:15 +00:00
|
|
|
/* try alias third */
|
2015-01-15 23:11:41 +00:00
|
|
|
if ((find = func_try(offsetof(intrin_func_t, alias), name)))
|
2013-10-16 04:04:39 +00:00
|
|
|
return find;
|
2013-08-14 06:02:15 +00:00
|
|
|
|
2013-11-23 15:58:03 +00:00
|
|
|
if (from) {
|
2015-01-15 23:11:41 +00:00
|
|
|
error("need function `%s', compiler depends on it for `__builtin_%s'", name, from);
|
|
|
|
return func_self("#nullfunc", nullptr);
|
2013-11-23 15:58:03 +00:00
|
|
|
}
|
2015-01-15 20:18:33 +00:00
|
|
|
return nullptr;
|
2013-11-23 12:21:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:41 +00:00
|
|
|
ast_expression *intrin::func(const char *name) {
|
|
|
|
return func_self(name, nullptr);
|
2013-03-09 08:57:37 +00:00
|
|
|
}
|