File: examples.rst

package info (click to toggle)
gcem 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 752 kB
  • sloc: cpp: 5,162; makefile: 276; python: 24
file content (87 lines) | stat: -rw-r--r-- 1,884 bytes parent folder | download
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
.. Copyright (c) 2016-2024 Keith O'Hara

   Distributed under the terms of the Apache License, Version 2.0.

   The full license is in the file LICENSE, distributed with this software.

Examples
===========

To calculate 10!:

.. code:: cpp

    #include "gcem.hpp"

    int main()
    {
        constexpr int x = 10;
        constexpr int res = gcem::factorial(x);

        return 0;
    }

Inspecting the assembly code generated by Clang:

.. code:: asm

        push    rbp
        mov     rbp, rsp
        xor     eax, eax
        mov     dword ptr [rbp - 4], 0
        mov     dword ptr [rbp - 8], 10
        mov     dword ptr [rbp - 12], 3628800
        pop     rbp
        ret

We see that a function call has been replaced by a numeric value (10! = 3628800).

Similarly, to compute the log-Gamma function at a point:

.. code:: cpp

    #include "gcem.hpp"

    int main()
    {
        constexpr long double x = 1.5;
        constexpr long double res = gcem::lgamma(x);

        return 0;
    }

Assembly code:

.. code:: asm

    .LCPI0_0:
            .long   1069547520              # float 1.5
    .LCPI0_1:
            .quad   -622431863250842976     # x86_fp80 -0.120782237635245222719
            .short  49147
            .zero   6
    main:                                   # @main
            push    rbp
            mov     rbp, rsp
            xor     eax, eax
            mov     dword ptr [rbp - 4], 0
            fld     dword ptr [rip + .LCPI0_0]
            fstp    tbyte ptr [rbp - 32]
            fld     tbyte ptr [rip + .LCPI0_1]
            fstp    tbyte ptr [rbp - 48]
            pop     rbp
            ret

Test suite
----------

To build the full test suite:

.. code:: bash

    # clone gcem from GitHub
    git clone -b master --single-branch https://github.com/kthohr/gcem ./gcem
    # compile tests
    cd ./gcem/tests
    make
    ./run_tests