Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@
- Implemented freelist-based `malloc`/`free`
- Modified linker script to carve out 8KB heap region
- Wrote original packet parser software app

## Erez Strauss ([@erez-strauss](https://github.com/erez-strauss))

- Implemented portable `sprintf`/`snprintf` library with full format support (`%d`, `%f`, `%e`, `%g`, `%x`, flags, width, precision, length modifiers)
- Created comprehensive test suite (~260 test cases) covering integer, floating-point, string, and truncation formatting
28 changes: 28 additions & 0 deletions sw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ free(ptr); // Return to freelist
- Arena: Fast allocation, bulk deallocation, no fragmentation, fixed lifetime
- malloc/free: Flexible lifetime, individual deallocation, potential fragmentation

### Sprintf (`lib/include/sprintf.h`, `lib/src/sprintf.c`)

Portable `sprintf`/`snprintf` with no `<stdio.h>` dependency. Uses integer-scaling for floating-point formatting to avoid cascading FP-rounding errors.

```c
#include "sprintf.h"

char buf[128];
sprintf(buf, "x=%d y=%s", 42, "hello"); // Unbounded format
snprintf(buf, sizeof(buf), "%.2f", 3.14159); // Bounded (C99 semantics)
```

**Supported format specifiers:**
- `%d`/`%i`, `%u`, `%o`, `%x`/`%X` — integer (signed/unsigned, octal, hex)
- `%f`/`%F`, `%e`/`%E`, `%g`/`%G` — floating-point (fixed, scientific, shortest)
- `%c` — character, `%s` — string, `%p` — pointer, `%%` — literal percent
- Flags: `-` `+` `space` `0` `#`
- Width/precision: literal or `*`
- Length modifiers: `hh` `h` `l` `ll` `z` `t`

**Makefile setup:** The 64-bit integer arithmetic used internally requires `-lgcc`. Add this to your app's Makefile before the `include`:
```makefile
EXTRA_LDFLAGS := -lgcc
SRC_C := ../../lib/src/uart.c ../../lib/src/sprintf.c your_app.c
include ../../common/common.mk
```

### Limits (`lib/include/limits.h`)

Integer limit constants for 32-bit systems.
Expand Down Expand Up @@ -308,6 +335,7 @@ Apps are also discoverable via `./tests/test_run_cocotb.py --list-tests`.
| `ras_stress_test/` | BTB+RAS stress test mixing loops, branches, and function pointers |
| `ras_test/` | Return Address Stack verification (deep nesting, coroutines, alignment) |
| `spanning_test/` | 32-bit instruction fetch across word boundary verification |
| `sprintf_test/` | sprintf/snprintf formatting test suite (~200 cases) |
| `strings_test/` | String/ctype/stdlib library test suite |
| `uart_echo/` | Interactive UART RX demo with echo, hex, and count commands |

Expand Down
19 changes: 19 additions & 0 deletions sw/apps/sprintf_test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2026 Two Sigma Open Source, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Makefile for sprintf/snprintf test application
# Tests custom sprintf/snprintf formatting implementation
EXTRA_LDFLAGS := -lgcc
SRC_C := ../../lib/src/uart.c ../../lib/src/string.c ../../lib/src/sprintf.c sprintf_test.c
include ../../common/common.mk
Loading
Loading