summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/basic/wasm-apps/testapp.c
blob: ea575e20ce64e259f43704c89feada0aa101915e (plain)
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
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int
intToStr(int x, char *str, int str_len, int digit);
int
get_pow(int x, int y);
int32_t
calculate_native(int32_t n, int32_t func1, int32_t func2);

//
// Primitive parameters functions
//
float
generate_float(int iteration, double seed1, float seed2)
{
    float ret;

    printf("calling into WASM function: %s\n", __FUNCTION__);

    for (int i = 0; i < iteration; i++) {
        ret += 1.0f / seed1 + seed2;
    }

    return ret;
}

// Converts a floating-point/double number to a string.
// intToStr() is implemented outside wasm app
void
float_to_string(float n, char *res, int res_size, int afterpoint)
{

    printf("calling into WASM function: %s\n", __FUNCTION__);

    // Extract integer part
    int ipart = (int)n;

    // Extract floating part
    float fpart = n - (float)ipart;

    // convert integer part to string
    int i = intToStr(ipart, res, res_size, 0);

    // check for display option after point
    if (afterpoint != 0) {
        res[i] = '.'; // add dot

        // Get the value of fraction part upto given no.
        // of points after dot. The third parameter
        // is needed to handle cases like 233.007
        fpart = fpart * get_pow(10, afterpoint);

        intToStr((int)fpart, res + i + 1, sizeof(res + i + 1), afterpoint);
    }
}

int32_t
mul7(int32_t n)
{
    printf("calling into WASM function: %s,", __FUNCTION__);
    n = n * 7;
    printf("    %s return %d \n", __FUNCTION__, n);
    return n;
}

int32_t
mul5(int32_t n)
{
    printf("calling into WASM function: %s,", __FUNCTION__);
    n = n * 5;
    printf("    %s return %d \n", __FUNCTION__, n);
    return n;
}

int32_t
calculate(int32_t n)
{
    printf("calling into WASM function: %s\n", __FUNCTION__);
    int32_t (*f1)(int32_t) = &mul5;
    int32_t (*f2)(int32_t) = &mul7;
    return calculate_native(n, (uint32_t)f1, (uint32_t)f2);
}