|
| 1 | +# Contributing to float8 |
| 2 | + |
| 3 | +Thank you for your interest in contributing to the float8 library! This document provides guidelines and information for contributors. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Code of Conduct](#code-of-conduct) |
| 8 | +- [Getting Started](#getting-started) |
| 9 | +- [Development Setup](#development-setup) |
| 10 | +- [Contributing Guidelines](#contributing-guidelines) |
| 11 | +- [Testing](#testing) |
| 12 | +- [Code Style](#code-style) |
| 13 | +- [Submitting Changes](#submitting-changes) |
| 14 | +- [Issue Reporting](#issue-reporting) |
| 15 | +- [Performance Considerations](#performance-considerations) |
| 16 | + |
| 17 | +## Code of Conduct |
| 18 | + |
| 19 | +This project adheres to a code of conduct that promotes a welcoming and inclusive environment. Please be respectful and professional in all interactions. |
| 20 | + |
| 21 | +## Getting Started |
| 22 | + |
| 23 | +1. **Fork the repository** on GitHub |
| 24 | +2. **Clone your fork** locally: |
| 25 | + ```bash |
| 26 | + git clone https://github.com/yourusername/float8.git |
| 27 | + cd float8 |
| 28 | + ``` |
| 29 | +3. **Add the upstream remote**: |
| 30 | + ```bash |
| 31 | + git remote add upstream https://github.com/zerfoo/float8.git |
| 32 | + ``` |
| 33 | + |
| 34 | +## Development Setup |
| 35 | + |
| 36 | +### Prerequisites |
| 37 | + |
| 38 | +- Go 1.21 or later |
| 39 | +- Git |
| 40 | + |
| 41 | +### Local Development |
| 42 | + |
| 43 | +1. **Install dependencies** (if any): |
| 44 | + ```bash |
| 45 | + go mod download |
| 46 | + ``` |
| 47 | + |
| 48 | +2. **Run tests** to ensure everything works: |
| 49 | + ```bash |
| 50 | + go test ./... |
| 51 | + ``` |
| 52 | + |
| 53 | +3. **Run benchmarks**: |
| 54 | + ```bash |
| 55 | + go test -bench=. -benchmem ./... |
| 56 | + ``` |
| 57 | + |
| 58 | +## Contributing Guidelines |
| 59 | + |
| 60 | +### Types of Contributions |
| 61 | + |
| 62 | +We welcome several types of contributions: |
| 63 | + |
| 64 | +- **Bug fixes**: Fix issues in existing functionality |
| 65 | +- **Performance improvements**: Optimize existing algorithms |
| 66 | +- **New features**: Add new mathematical functions or operations |
| 67 | +- **Documentation**: Improve README, comments, or examples |
| 68 | +- **Tests**: Add test cases for better coverage |
| 69 | +- **Benchmarks**: Add performance benchmarks |
| 70 | + |
| 71 | +### Before You Start |
| 72 | + |
| 73 | +1. **Check existing issues** to see if your contribution is already being worked on |
| 74 | +2. **Open an issue** to discuss major changes before implementing them |
| 75 | +3. **Keep changes focused** - one feature or fix per pull request |
| 76 | + |
| 77 | +### Implementation Guidelines |
| 78 | + |
| 79 | +#### Core Principles |
| 80 | + |
| 81 | +1. **Correctness**: All operations must be mathematically correct according to IEEE 754 FP8 E4M3FN specification |
| 82 | +2. **Performance**: Consider both speed and memory usage |
| 83 | +3. **Compatibility**: Maintain backward compatibility unless absolutely necessary |
| 84 | +4. **Simplicity**: Prefer clear, readable code over clever optimizations |
| 85 | + |
| 86 | +#### Adding New Functions |
| 87 | + |
| 88 | +When adding new mathematical functions: |
| 89 | + |
| 90 | +1. **Follow the pattern** established by existing functions |
| 91 | +2. **Handle edge cases** (NaN, zero, overflow/underflow) |
| 92 | +3. **Add comprehensive tests** including edge cases |
| 93 | +4. **Include benchmarks** for performance-critical functions |
| 94 | +5. **Document the function** with clear comments |
| 95 | + |
| 96 | +Example function structure: |
| 97 | +```go |
| 98 | +// NewFunction performs a specific mathematical operation on Float8 values. |
| 99 | +// It handles special cases like NaN and zero appropriately. |
| 100 | +func (f Float8) NewFunction() Float8 { |
| 101 | + // Handle special cases first |
| 102 | + if f.IsNaN() { |
| 103 | + return NaN() |
| 104 | + } |
| 105 | + |
| 106 | + // Main implementation |
| 107 | + // ... |
| 108 | + |
| 109 | + return result |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Testing |
| 114 | + |
| 115 | +### Test Requirements |
| 116 | + |
| 117 | +- **All new code must have tests** |
| 118 | +- **Aim for high test coverage** (>90%) |
| 119 | +- **Include edge cases** and boundary conditions |
| 120 | +- **Test both normal and error paths** |
| 121 | + |
| 122 | +### Running Tests |
| 123 | + |
| 124 | +```bash |
| 125 | +# Run all tests |
| 126 | +go test ./... |
| 127 | + |
| 128 | +# Run tests with coverage |
| 129 | +go test -cover ./... |
| 130 | + |
| 131 | +# Run specific test files |
| 132 | +go test -v ./arithmetic_test.go |
| 133 | + |
| 134 | +# Generate coverage report |
| 135 | +go test -coverprofile=coverage.out ./... |
| 136 | +go tool cover -html=coverage.out |
| 137 | +``` |
| 138 | + |
| 139 | +### Test Categories |
| 140 | + |
| 141 | +1. **Unit tests**: Test individual functions |
| 142 | +2. **Property tests**: Test mathematical properties (commutativity, associativity, etc.) |
| 143 | +3. **Edge case tests**: Test boundary conditions and special values |
| 144 | +4. **Performance tests**: Benchmark critical operations |
| 145 | + |
| 146 | +### Writing Tests |
| 147 | + |
| 148 | +Follow this pattern for test functions: |
| 149 | + |
| 150 | +```go |
| 151 | +func TestNewFunction(t *testing.T) { |
| 152 | + tests := []struct { |
| 153 | + name string |
| 154 | + input Float8 |
| 155 | + expected Float8 |
| 156 | + }{ |
| 157 | + {"normal case", FromFloat32(2.0), FromFloat32(4.0)}, |
| 158 | + {"zero", Zero(), Zero()}, |
| 159 | + {"NaN", NaN(), NaN()}, |
| 160 | + } |
| 161 | + |
| 162 | + for _, tt := range tests { |
| 163 | + t.Run(tt.name, func(t *testing.T) { |
| 164 | + result := tt.input.NewFunction() |
| 165 | + if !result.Equal(tt.expected) { |
| 166 | + t.Errorf("got %v, want %v", result, tt.expected) |
| 167 | + } |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +## Code Style |
| 174 | + |
| 175 | +### Go Standards |
| 176 | + |
| 177 | +- Follow standard Go formatting (`go fmt`) |
| 178 | +- Use `go vet` to check for common issues |
| 179 | +- Follow Go naming conventions |
| 180 | +- Write clear, descriptive variable names |
| 181 | + |
| 182 | +### Documentation |
| 183 | + |
| 184 | +- **Public functions** must have godoc comments |
| 185 | +- **Complex algorithms** should have implementation comments |
| 186 | +- **Edge cases** should be documented |
| 187 | +- **Performance characteristics** should be noted where relevant |
| 188 | + |
| 189 | +### Example Documentation: |
| 190 | + |
| 191 | +```go |
| 192 | +// Add performs floating-point addition of two Float8 values. |
| 193 | +// It handles special cases including NaN propagation and zero addition. |
| 194 | +// The result follows IEEE 754 rounding rules for the E4M3FN format. |
| 195 | +// |
| 196 | +// Special cases: |
| 197 | +// - Add(NaN, x) = NaN for any x |
| 198 | +// - Add(x, NaN) = NaN for any x |
| 199 | +// - Add(+0, -0) = +0 |
| 200 | +// |
| 201 | +// Performance: O(1) with fast arithmetic enabled, otherwise involves |
| 202 | +// conversion to float32 and back. |
| 203 | +func (f Float8) Add(other Float8) Float8 { |
| 204 | + // Implementation... |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +## Submitting Changes |
| 209 | + |
| 210 | +### Pull Request Process |
| 211 | + |
| 212 | +1. **Create a feature branch**: |
| 213 | + ```bash |
| 214 | + git checkout -b feature/your-feature-name |
| 215 | + ``` |
| 216 | + |
| 217 | +2. **Make your changes** following the guidelines above |
| 218 | + |
| 219 | +3. **Test thoroughly**: |
| 220 | + ```bash |
| 221 | + go test ./... |
| 222 | + go test -race ./... |
| 223 | + ``` |
| 224 | + |
| 225 | +4. **Update documentation** if needed |
| 226 | + |
| 227 | +5. **Commit with clear messages**: |
| 228 | + ```bash |
| 229 | + git commit -m "Add NewFunction for mathematical operation X |
| 230 | + |
| 231 | + - Implements IEEE 754 compliant operation |
| 232 | + - Handles NaN and zero edge cases |
| 233 | + - Includes comprehensive tests and benchmarks" |
| 234 | + ``` |
| 235 | + |
| 236 | +6. **Push to your fork**: |
| 237 | + ```bash |
| 238 | + git push origin feature/your-feature-name |
| 239 | + ``` |
| 240 | + |
| 241 | +7. **Open a pull request** with: |
| 242 | + - Clear description of changes |
| 243 | + - Reference to related issues |
| 244 | + - Test results and benchmark comparisons |
| 245 | + |
| 246 | +### Pull Request Guidelines |
| 247 | + |
| 248 | +- **One feature per PR** - keep changes focused |
| 249 | +- **Include tests** for all new functionality |
| 250 | +- **Update documentation** as needed |
| 251 | +- **Maintain backward compatibility** unless discussed |
| 252 | +- **Follow the existing code style** |
| 253 | + |
| 254 | +## Issue Reporting |
| 255 | + |
| 256 | +### Bug Reports |
| 257 | + |
| 258 | +When reporting bugs, please include: |
| 259 | + |
| 260 | +- **Go version** and operating system |
| 261 | +- **Minimal reproduction case** |
| 262 | +- **Expected vs actual behavior** |
| 263 | +- **Stack trace** if applicable |
| 264 | + |
| 265 | +### Feature Requests |
| 266 | + |
| 267 | +For new features, please provide: |
| 268 | + |
| 269 | +- **Use case description** |
| 270 | +- **Proposed API design** |
| 271 | +- **Performance considerations** |
| 272 | +- **Compatibility impact** |
| 273 | + |
| 274 | +## Performance Considerations |
| 275 | + |
| 276 | +### Optimization Guidelines |
| 277 | + |
| 278 | +1. **Measure first** - use benchmarks to identify bottlenecks |
| 279 | +2. **Consider memory usage** - Float8 is designed for efficiency |
| 280 | +3. **Lookup tables** - balance speed vs memory for critical paths |
| 281 | +4. **Avoid allocations** in hot paths |
| 282 | +5. **Profile regularly** - use `go tool pprof` for analysis |
| 283 | + |
| 284 | +### Benchmarking |
| 285 | + |
| 286 | +Always include benchmarks for performance-critical changes: |
| 287 | + |
| 288 | +```go |
| 289 | +func BenchmarkNewFunction(b *testing.B) { |
| 290 | + f := FromFloat32(3.14) |
| 291 | + b.ResetTimer() |
| 292 | + |
| 293 | + for i := 0; i < b.N; i++ { |
| 294 | + _ = f.NewFunction() |
| 295 | + } |
| 296 | +} |
| 297 | +``` |
| 298 | + |
| 299 | +## Questions? |
| 300 | + |
| 301 | +If you have questions about contributing, please: |
| 302 | + |
| 303 | +1. Check existing issues and documentation |
| 304 | +2. Open a new issue for discussion |
| 305 | +3. Reach out to maintainers |
| 306 | + |
| 307 | +Thank you for contributing to float8! |
0 commit comments