{"id":28000678,"url":"https://github.com/sysprog21/kcalc","last_synced_at":"2025-06-12T04:35:48.658Z","repository":{"id":72389284,"uuid":"173585388","full_name":"sysprog21/kcalc","owner":"sysprog21","description":"Math expression evaluation as Linux kernel module","archived":false,"fork":false,"pushed_at":"2020-04-05T20:01:27.000Z","size":83,"stargazers_count":17,"open_issues_count":0,"forks_count":45,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-08T23:54:12.657Z","etag":null,"topics":["expression-evaluator","fixed-point","linux-kernel"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sysprog21.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-03-03T14:26:52.000Z","updated_at":"2024-04-03T21:48:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"62ac2bb3-6c81-4c69-8821-c2c5c0bb0ab4","html_url":"https://github.com/sysprog21/kcalc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sysprog21/kcalc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Fkcalc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Fkcalc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Fkcalc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Fkcalc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sysprog21","download_url":"https://codeload.github.com/sysprog21/kcalc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysprog21%2Fkcalc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259398667,"owners_count":22851479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expression-evaluator","fixed-point","linux-kernel"],"created_at":"2025-05-08T23:54:10.368Z","updated_at":"2025-06-12T04:35:48.627Z","avatar_url":"https://github.com/sysprog21.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kcalc: in-kernel math expression evaluation for Linux\n\nkcalc is a mathematical expression evaluator and takes string as\ninput, returning fixed-point numbers.\n\n## Features\n\n* Arithmetic, bitwise and logical operators;\n* Flexible variables;\n* Customized functions;\n\n## Fixed-point representation\n\nkcalc introduces a fixed-point number representation for fractional values:\none 32-bit size is divided into mantissa (28 bits), sign (1 bit) and\nexponent (3 bits).\n\n```\nMSB   31                               4    3   2    1   0  LSB\n     +----------------------------------+------+----------+\n     |     mantissa                     | sign | exponent |\n     +----------------------------------+------+----------+\n```\n\n## Usage:\n\nBuild and install the module\n\n```shell\n$ make\n$ sudo insmod calc.ko\n$ sudo chmod 0666 /dev/calc\n```\n\nThen simply send the expression to the module\n\n```shell\n$ echo -ne \"3*5\\0\" \u003e /dev/calc\n```\n\nThe expected output in `dmesg` should be:\n\n```shell\ncalc: Received 3 -\u003e 3*5\nResult: 240\n```\n\nThe result seems incorrect because we did not transform the value to normal representation.\nYou can use the utlity to convert values into human readable form:\n```shell\n$ source scripts/eval.sh\n$ fromfixed 240\n```\n\nThen, you can get `15`, which is the exact result for expression `3*5`.\n\nYou can check file `scripts/test.sh` for more examples about the expressions. Alteratively,\nexecue `make check` for the same script.\n\n```shell\n$ scripts/test.sh\n\n... Information generated by modinfo ...\n\nTesting  6*7 ...\n42\nTesting  1980+1 ...\n1981\nTesting  2019-1 ...\n2018\nTesting  42/6 ...\n7\nTesting  1/3 ...\n.33333330000000000000\nTesting  1/3*6+2/4 ...\n2.49999980000000000000\nTesting  (1/3)+(2/3) ...\n.99999990000000000000\nTesting  (2145%31)+23 ...\n29\n```\n\n## Internals\n\n`struct expr *expr_create(const char *s, size_t len, struct expr_var_list\n*vars, struct expr_func *funcs)` - returns compiled expression from the given\nstring. If expression uses variables - they are bound to `vars`, so you can\nmodify values before evaluation or check the results after the evaluation.\n\n`int expr_eval(struct expr *e)` - evaluates compiled expression.\n\n`void expr_destroy(struct expr *e, struct expr_var_list *vars)` - cleans up\nmemory. Parameters can be NULL (e.g. if you want to clean up expression, but\nreuse variables for another expression).\n\n`struct expr_var *expr_var(struct expr_var *vars, const char *s, size_t len)` -\nreturns/creates variable of the given name in the given list. This can be used\nto get variable references to get/set them manually.\n\n## Supported operators\n\n* Arithmetics: `+`, `-`, `*`, `/`, `%` (remainder), `**` (power)\n* Bitwise: `\u003c\u003c`, `\u003e\u003e`, `\u0026`, `|`, `^` (xor or unary bitwise negation)\n* Logical: `\u003c`, `\u003e`, `==`, `!=`, `\u003c=`, `\u003e=`, `\u0026\u0026`, `||`, `!` (unary not)\n* Other: `=` (assignment, e.g. `x=y=5`), `,` (separates expressions or function parameters)\n\n## License\n\n`kcalc`is released under the MIT License. Use of this source code is governed by\na MIT License that can be found in the LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysprog21%2Fkcalc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsysprog21%2Fkcalc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysprog21%2Fkcalc/lists"}